Skip to content

Commit

Permalink
Fix for goaop#332. Tests for console commands. Cleanup and tests upgr…
Browse files Browse the repository at this point in the history
…ade. (goaop#333)

Upgraded PHPUnit, migrated tests. Added tests for console commands. Cache warmer moved to separate class, fixing goaop#332.
  • Loading branch information
TheCelavi authored and lisachenko committed Jul 30, 2017
1 parent 6e2a0fe commit b6d6881
Show file tree
Hide file tree
Showing 29 changed files with 465 additions and 117 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor/
composer.lock
phpunit.xml
tests/Fixtures/project/var/cache/*
build/
19 changes: 14 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"require-dev": {
"symfony/console": "^2.7|^3.0",
"adlawson/vfs": "^0.12",
"phpunit/phpunit": "^4.8",
"doctrine/orm": "^2.5"
"phpunit/phpunit": "^5.7",
"doctrine/orm": "^2.5",
"symfony/filesystem": "^3.3",
"symfony/process": "^3.3"
},

"suggest": {
Expand All @@ -34,11 +36,18 @@
],

"autoload": {
"psr-4": {"Go\\": "src/"}
"psr-4": {
"Go\\": "src/"
}
},
"autoload-dev": {
"psr-0": {"Demo\\": "demos/"},
"psr-4": {"Go\\": "tests/Go/"}
"psr-0": {
"Demo\\": "demos/"
},
"psr-4": {
"Go\\": "tests/Go/",
"Go\\Tests\\TestProject\\": "tests/Fixtures/project/src/"
}
},

"minimum-stability": "stable",
Expand Down
9 changes: 9 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@
<group>performance</group>
</exclude>
</groups>


<logging>
<log type="coverage-html" target="./build/coverage/html"/>
<log type="coverage-xml" target="./build/coverage/xml"/>
<log type="coverage-clover" target="./build/logs/clover.xml"/>
<log type="coverage-crap4j" target="./build/logs/crap4j.xml"/>
<log type="junit" target="./build/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>
2 changes: 2 additions & 0 deletions src/Console/Command/BaseAspectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* Base command for all aspect commands
*
* @codeCoverageIgnore
*/
class BaseAspectCommand extends Command
{
Expand Down
62 changes: 5 additions & 57 deletions src/Console/Command/CacheWarmupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

namespace Go\Console\Command;

use Go\Instrument\ClassLoading\SourceTransformingLoader;
use Go\Instrument\FileSystem\Enumerator;
use Go\Instrument\Transformer\FilterInjectorTransformer;
use Go\Instrument\ClassLoading\CacheWarmer;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Console command for warming the cache
*
* @codeCoverageIgnore
*/
class CacheWarmupCommand extends BaseAspectCommand
{
Expand Down Expand Up @@ -47,59 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->loadAspectKernel($input, $output);

$options = $this->aspectKernel->getOptions();

if (empty($options['cacheDir'])) {
throw new \InvalidArgumentException("Cache warmer require the `cacheDir` options to be configured");
}

$enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
$iterator = $enumerator->enumerate();

$totalFiles = iterator_count($iterator);
$output->writeln("Total <info>{$totalFiles}</info> files to process.");
$iterator->rewind();

set_error_handler(function($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

$index = 0;
$errors = [];
foreach ($iterator as $file) {
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln("Processing file <info>{$file->getRealPath()}</info>");
}
$isSuccess = null;
try {
// This will trigger creation of cache
file_get_contents(
FilterInjectorTransformer::PHP_FILTER_READ .
SourceTransformingLoader::FILTER_IDENTIFIER .
"/resource=" . $file->getRealPath()
);
$isSuccess = true;
} catch (\Exception $e) {
$isSuccess = false;
$errors[$file->getRealPath()] = $e;
}
if ($output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
$output->write($isSuccess ? '.' : '<error>E</error>');
if (++$index % 50 == 0) {
$output->writeln("($index/$totalFiles)");
}
}
}

restore_error_handler();

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
foreach ($errors as $file=>$error) {
$message = "File {$file} is not processed correctly due to exception: {$error->getMessage()}";
$output->writeln($message);
}
}

$output->writeln("<info>Done</info>");
$warmer = new CacheWarmer($this->aspectKernel, $output);
$warmer->warmUp();
}
}
2 changes: 2 additions & 0 deletions src/Console/Command/DebugAdvisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

/**
* Console command to debug an advisors
*
* @codeCoverageIgnore
*/
class DebugAdvisorCommand extends BaseAspectCommand
{
Expand Down
2 changes: 2 additions & 0 deletions src/Console/Command/DebugAspectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

/**
* Console command for querying an information about aspects
*
* @codeCoverageIgnore
*/
class DebugAspectCommand extends BaseAspectCommand
{
Expand Down
104 changes: 104 additions & 0 deletions src/Instrument/ClassLoading/CacheWarmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*
* Go! AOP framework
*
* @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Instrument\ClassLoading;

use Go\Core\AspectKernel;
use Go\Instrument\FileSystem\Enumerator;
use Go\Instrument\Transformer\FilterInjectorTransformer;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Warms up the cache
*/
class CacheWarmer
{
/**
* @var AspectKernel
*/
protected $aspectKernel;

/**
* @var OutputInterface
*/
protected $output;

/**
* CacheWarmer constructor.
*
* @param AspectKernel $aspectKernel A kernel
* @param OutputInterface $output Optional output to log current status.
*/
public function __construct(AspectKernel $aspectKernel, OutputInterface $output = null)
{
$this->aspectKernel = $aspectKernel;
$this->output = $output !== null ? $output : new NullOutput();
}

/**
* Warms up cache
*/
public function warmUp()
{
$options = $this->aspectKernel->getOptions();

if (empty($options['cacheDir'])) {
throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured');
}

$enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
$iterator = $enumerator->enumerate();
$total = iterator_count($iterator);

$this->output->writeln(sprintf('Total <info>%s</info> files to process.', $total));
$this->output->writeln('');
$iterator->rewind();

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
});

$errors = [];
$displayException = \Closure::bind(function ($exception, $path) use (&$errors) {
$this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: %s', $path));
$errors[$path] = $exception->getMessage();
}, $this);

foreach ($iterator as $file) {
$path = $file->getRealPath();

try {
// This will trigger creation of cache
file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ .
SourceTransformingLoader::FILTER_IDENTIFIER .
'/resource=' . $path
);

$this->output->writeln(sprintf('<fg=green;options=bold>[OK]</>: <comment>%s</comment>', $path));
} catch (\Throwable $e) {
$displayException($e, $path);
} catch (\Exception $e) {
$displayException($e, $path);
}
}

restore_error_handler();

if ($this->output->isVerbose()) {
foreach ($errors as $path => $error) {
$this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: File "%s" is not processed correctly due to exception: "%s".', $path, $error));
}
}

$this->output->writeln('');
$this->output->writeln(sprintf('<fg=green;>[DONE]</>: Total processed %s, %s errors.', $total, count($errors)));
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/project/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php

include_once __DIR__ . '/../../../../vendor/autoload.php';

use Symfony\Component\Console\Application;
use Go\Console\Command\CacheWarmupCommand;
use Go\Console\Command\DebugAdvisorCommand;
use Go\Console\Command\DebugAspectCommand;

$application = new Application();

$application->addCommands([
new CacheWarmupCommand(),
new DebugAdvisorCommand(),
new DebugAspectCommand(),
]);

$application->run();
11 changes: 11 additions & 0 deletions tests/Fixtures/project/src/Annotation/Loggable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Go\Tests\TestProject\Annotation;

/**
* @Annotation
*/
class Loggable
{

}
16 changes: 16 additions & 0 deletions tests/Fixtures/project/src/Application/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Go\Tests\TestProject\Application;

use Go\Tests\TestProject\Annotation as Aop;

class Main
{
/**
* @Aop\Loggable()
*/
public function doSomething()
{

}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/project/src/ApplicationAspectKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Go\Tests\TestProject;

use Go\Core\AspectContainer;
use Go\Core\AspectKernel;
use Go\Tests\TestProject\Aspect\LoggingAspect;
use Psr\Log\NullLogger;

class ApplicationAspectKernel extends AspectKernel
{
/**
* Configure an AspectContainer with advisors, aspects and pointcuts
*
* @param AspectContainer $container
*
* @return void
*/
protected function configureAop(AspectContainer $container)
{
$container->registerAspect(new LoggingAspect(new NullLogger()));
}
}
35 changes: 35 additions & 0 deletions tests/Fixtures/project/src/Aspect/LoggingAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Go\Tests\TestProject\Aspect;

use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
use Psr\Log\LoggerInterface;

/**
* Application logging aspect
*/
class LoggingAspect implements Aspect
{
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* Writes a log info before method execution
*
* @param MethodInvocation $invocation
* @Before("@execution(Go\Tests\TestProject\Annotation\Loggable)")
*/
public function beforeMethod(MethodInvocation $invocation)
{
$this->logger->info($invocation, $invocation->getArguments());
}
}
Empty file.
15 changes: 15 additions & 0 deletions tests/Fixtures/project/web/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

include_once __DIR__ . '/../../../../vendor/autoload.php';

use Go\Tests\TestProject\ApplicationAspectKernel;

$applicationAspectKernel = ApplicationAspectKernel::getInstance();
$applicationAspectKernel->init(array(
'appDir' => __DIR__ . '/../',
'debug' => true,
'cacheDir' => __DIR__ . '/../var/cache/aspect',
'includePaths' => array(
__DIR__ . '/../src/'
)
));
Loading

0 comments on commit b6d6881

Please sign in to comment.