Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve tests #267

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ junit-report.xml
.php_cs.cache
.deptrac.cache
.phpunit.result.cache
infection.log
14 changes: 14 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"timeout": 10,
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "infection.log"
},
"mutators": {
"@default": true
}
}
2 changes: 1 addition & 1 deletion src/AstRunner/AstMap/AstInherit.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __toString(): string

$buffer = '';
foreach ($this->path as $v) {
$buffer = ((string) $v).' -> '.$buffer;
$buffer = $v.' -> '.$buffer;
}

return $description.' (path: '.rtrim($buffer, ' -> ').')';
Expand Down
2 changes: 1 addition & 1 deletion src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function parse($data): AstFileReference
{
/** @var \SplFileInfo $data */
if (!$this->supports($data)) {
throw new \LogicException('parser not supported');
throw new \LogicException('data not supported');
}

if (null !== $fileReference = $this->cache->get($data->getRealPath())) {
Expand Down
2 changes: 1 addition & 1 deletion src/AstRunner/AstRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function createAstMapByFiles(array $files): AstMap
}
}

$this->dispatcher->dispatch(new PostCreateAstMapEvent($astMap));
$this->dispatcher->dispatch(new PostCreateAstMapEvent());

return $astMap;
}
Expand Down
14 changes: 1 addition & 13 deletions src/AstRunner/Event/PostCreateAstMapEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,8 @@

namespace SensioLabs\Deptrac\AstRunner\Event;

use SensioLabs\Deptrac\AstRunner\AstMap;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

class PostCreateAstMapEvent extends Event
{
private $astMap;

public function __construct(AstMap $astMap)
{
$this->astMap = $astMap;
}

public function getAstMap(): AstMap
{
return $this->astMap;
}
}
6 changes: 3 additions & 3 deletions src/Subscriber/ConsoleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ public function onPreDependencyEmit(PreEmitEvent $event): void
);
}

public function onPostDependencyEmit(): void
public function onPostDependencyEmit(PostEmitEvent $event): void
{
$this->output->writeln('<info>end emitting dependencies</info>', OutputInterface::VERBOSITY_VERBOSE);
}

public function onPreDependencyFlatten(): void
public function onPreDependencyFlatten(PreFlattenEvent $event): void
{
$this->output->writeln('<info>start flatten dependencies</info>', OutputInterface::VERBOSITY_VERBOSE);
}

public function onPostDependencyFlatten(): void
public function onPostDependencyFlatten(PostFlattenEvent $event): void
{
$this->output->writeln('<info>end flatten dependencies</info>', OutputInterface::VERBOSITY_VERBOSE);
}
Expand Down
41 changes: 41 additions & 0 deletions tests/AstRunner/AstParser/NikicPhpParser/NikicPhpParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\AstRunner\AstParser\AstFileReferenceCache;
use SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser\FileParser;
use SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser\NikicPhpParser;

class NikicPhpParserTest extends TestCase
{
/** @var NikicPhpParser */
private $parser;

protected function setUp(): void
{
parent::setUp();

$this->parser = new NikicPhpParser(
$this->createMock(FileParser::class),
$this->createMock(AstFileReferenceCache::class)
);
}

public function testSupport(): void
{
static::assertTrue($this->parser->supports(new \SplFileInfo('foo.php')));
static::assertTrue($this->parser->supports(new \SplFileInfo('FOO.PHP')));
static::assertFalse($this->parser->supports(new \SplFileInfo('FOO.html')));
static::assertFalse($this->parser->supports(new \stdClass()));
}

public function testParseWithInvalidData(): void
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('data not supported');
$this->parser->parse(new \stdClass());
}
}
41 changes: 41 additions & 0 deletions tests/Subscriber/CacheableFileSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Tests\SensioLabs\Deptrac\Subscriber;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\AstRunner\AstParser\AstFileReferenceFileCache;
use SensioLabs\Deptrac\AstRunner\Event\PostCreateAstMapEvent;
use SensioLabs\Deptrac\AstRunner\Event\PreCreateAstMapEvent;
use SensioLabs\Deptrac\Subscriber\CacheableFileSubscriber;

class CacheableFileSubscriberTest extends TestCase
{
public function testSubscribedEvents(): void
{
static::assertSame(
[
PreCreateAstMapEvent::class => 'onPreCreateAstMapEvent',
PostCreateAstMapEvent::class => 'onPostCreateAstMapEvent',
],
CacheableFileSubscriber::getSubscribedEvents()
);
}

public function testOnPreCreateAstMapEvent(): void
{
$cache = $this->createMock(AstFileReferenceFileCache::class);
$cache->expects(static::once())->method('load');

(new CacheableFileSubscriber($cache))->onPreCreateAstMapEvent(new PreCreateAstMapEvent(1));
}

public function testOnPostCreateAstMapEvent(): void
{
$cache = $this->createMock(AstFileReferenceFileCache::class);
$cache->expects(static::once())->method('write');

(new CacheableFileSubscriber($cache))->onPostCreateAstMapEvent(new PostCreateAstMapEvent());
}
}
106 changes: 99 additions & 7 deletions tests/Subscriber/ConsoleSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,116 @@
namespace Tests\SensioLabs\Deptrac\Subscriber;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\AstRunner\Event\AstFileAnalyzedEvent;
use SensioLabs\Deptrac\AstRunner\Event\AstFileSyntaxErrorEvent;
use SensioLabs\Deptrac\AstRunner\Event\PostCreateAstMapEvent;
use SensioLabs\Deptrac\AstRunner\Event\PreCreateAstMapEvent;
use SensioLabs\Deptrac\Dependency\Event\PostEmitEvent;
use SensioLabs\Deptrac\Dependency\Event\PostFlattenEvent;
use SensioLabs\Deptrac\Dependency\Event\PreEmitEvent;
use SensioLabs\Deptrac\Dependency\Event\PreFlattenEvent;
use SensioLabs\Deptrac\Subscriber\ConsoleSubscriber;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class ConsoleSubscriberTest extends TestCase
{
public function testSubscribedEvents(): void
{
static::assertSame(
[
PreCreateAstMapEvent::class => 'onPreCreateAstMapEvent',
PostCreateAstMapEvent::class => 'onPostCreateAstMapEvent',
AstFileAnalyzedEvent::class => 'onAstFileAnalyzedEvent',
AstFileSyntaxErrorEvent::class => 'onAstFileSyntaxErrorEvent',
PreEmitEvent::class => 'onPreDependencyEmit',
PostEmitEvent::class => 'onPostDependencyEmit',
PreFlattenEvent::class => 'onPreDependencyFlatten',
PostFlattenEvent::class => 'onPostDependencyFlatten',
],
ConsoleSubscriber::getSubscribedEvents()
);
}

public function testOnPreCreateAstMapEventWithVerboseVerbosity(): void
{
$dispatcher = new EventDispatcher();
$formatter = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onPreCreateAstMapEvent(new PreCreateAstMapEvent(9999999));

static::assertSame("Start to create an AstMap for 9999999 Files.\n", $output->fetch());
}

public function testOnPostCreateAstMapEventWithVerboseVerbosity(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onPostCreateAstMapEvent(new PostCreateAstMapEvent());

static::assertSame("AstMap created.\n", $output->fetch());
}

public function testOnAstFileAnalyzedEventWithVerboseVerbosity(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onAstFileAnalyzedEvent(new AstFileAnalyzedEvent(new \SplFileInfo('foo.php')));

static::assertSame("Parsing File foo.php\n", $output->fetch());
}

public function testOnAstFileSyntaxErrorEvent(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onAstFileSyntaxErrorEvent(
new AstFileSyntaxErrorEvent(new \SplFileInfo('foo.php'), 'Invalid')
);

$dispatcher->addSubscriber(new ConsoleSubscriber($formatter));
static::assertSame("\nSyntax Error on File foo.php\nInvalid\n\n", $output->fetch());
}

public function testOnPreDependencyEmit(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onPreDependencyEmit(new PreEmitEvent('emitter-name'));

static::assertSame("start emitting dependencies \"emitter-name\"\n", $output->fetch());
}

public function testOnPostDependencyEmit(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onPostDependencyEmit(new PostEmitEvent());

static::assertSame("end emitting dependencies\n", $output->fetch());
}

public function testOnPreDependencyFlatten(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$subscriber = new ConsoleSubscriber($output);
$subscriber->onPreDependencyFlatten(new PreFlattenEvent());

static::assertSame("start flatten dependencies\n", $output->fetch());
}

public function testOnPostDependencyFlatten(): void
{
$output = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);

$dispatcher->dispatch(new PreCreateAstMapEvent(9999999));
$result = $formatter->fetch();
$subscriber = new ConsoleSubscriber($output);
$subscriber->onPostDependencyFlatten(new PostFlattenEvent());

static::assertStringContainsString('9999999', $result);
static::assertSame("end flatten dependencies\n", $output->fetch());
}
}
67 changes: 58 additions & 9 deletions tests/Subscriber/ProgressSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,73 @@
namespace Tests\SensioLabs\Deptrac\Subscriber;

use PHPUnit\Framework\TestCase;
use SensioLabs\Deptrac\AstRunner\Event\AstFileAnalyzedEvent;
use SensioLabs\Deptrac\AstRunner\Event\PostCreateAstMapEvent;
use SensioLabs\Deptrac\AstRunner\Event\PreCreateAstMapEvent;
use SensioLabs\Deptrac\Subscriber\ProgressSubscriber;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class ProgressSubscriberTest extends TestCase
{
public function testOnPreCreateAstMapEventWithVerboseVerbosity(): void
public function testSubscribedEvents(): void
{
$dispatcher = new EventDispatcher();
$formatter = new BufferedOutput(OutputInterface::VERBOSITY_VERBOSE);
static::assertSame(
[
PreCreateAstMapEvent::class => 'onPreCreateAstMapEvent',
PostCreateAstMapEvent::class => ['onPostCreateAstMapEvent', 1],
AstFileAnalyzedEvent::class => 'onAstFileAnalyzedEvent',
],
ProgressSubscriber::getSubscribedEvents()
);
}

public function testProgress(): void
{
$formatter = new BufferedOutput();
$subscriber = new ProgressSubscriber($formatter);

$subscriber->onPreCreateAstMapEvent(new PreCreateAstMapEvent(1));
$subscriber->onAstFileAnalyzedEvent(new AstFileAnalyzedEvent(new \SplFileInfo('foo.php')));
$subscriber->onPostCreateAstMapEvent(new PostCreateAstMapEvent());

$expectedOutput = <<<OUT
0/1 [>---------------------------] 0%
1/1 [============================] 100%

OUT;

static::assertSame($expectedOutput, $formatter->fetch());
}

public function testOnAstFileAnalyzedEvent(): void
{
$formatter = new BufferedOutput();
$subscriber = new ProgressSubscriber($formatter);

$subscriber->onAstFileAnalyzedEvent(new AstFileAnalyzedEvent(new \SplFileInfo('foo.php')));

$expectedOutput = <<<OUT

1 [->--------------------------]
OUT;

static::assertSame($expectedOutput, $formatter->fetch());
}

public function testOnPostCreateAstMapEvent(): void
{
$formatter = new BufferedOutput();
$subscriber = new ProgressSubscriber($formatter);

$subscriber->onPreCreateAstMapEvent(new PreCreateAstMapEvent(1));
$subscriber->onPostCreateAstMapEvent(new PostCreateAstMapEvent());

$dispatcher->addSubscriber(new ProgressSubscriber($formatter));
$expectedOutput = <<<OUT
0/1 [>---------------------------] 0%
1/1 [============================] 100%

$dispatcher->dispatch(new PreCreateAstMapEvent(9999999));
$result = $formatter->fetch();
OUT;

static::assertStringContainsString('9999999', $result);
static::assertSame($expectedOutput, $formatter->fetch());
}
}