From f26b9163e60f61e3e0ebefc158e07b10e245eea9 Mon Sep 17 00:00:00 2001 From: smoench Date: Tue, 24 Sep 2019 12:53:09 +0200 Subject: [PATCH] improve tests --- .gitignore | 1 + infection.json.dist | 14 +++ src/AstRunner/AstMap/AstInherit.php | 2 +- .../NikicPhpParser/NikicPhpParser.php | 2 +- src/AstRunner/AstRunner.php | 2 +- src/AstRunner/Event/PostCreateAstMapEvent.php | 14 +-- src/Subscriber/ConsoleSubscriber.php | 6 +- .../NikicPhpParser/NikicPhpParserTest.php | 41 +++++++ .../CacheableFileSubscriberTest.php | 41 +++++++ tests/Subscriber/ConsoleSubscriberTest.php | 106 ++++++++++++++++-- tests/Subscriber/ProgressSubscriberTest.php | 67 +++++++++-- 11 files changed, 261 insertions(+), 35 deletions(-) create mode 100644 infection.json.dist create mode 100644 tests/AstRunner/AstParser/NikicPhpParser/NikicPhpParserTest.php create mode 100644 tests/Subscriber/CacheableFileSubscriberTest.php diff --git a/.gitignore b/.gitignore index 7e388155d..aa937430a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ junit-report.xml .php_cs.cache .deptrac.cache .phpunit.result.cache +infection.log diff --git a/infection.json.dist b/infection.json.dist new file mode 100644 index 000000000..60a2a8402 --- /dev/null +++ b/infection.json.dist @@ -0,0 +1,14 @@ +{ + "timeout": 10, + "source": { + "directories": [ + "src" + ] + }, + "logs": { + "text": "infection.log" + }, + "mutators": { + "@default": true + } +} \ No newline at end of file diff --git a/src/AstRunner/AstMap/AstInherit.php b/src/AstRunner/AstMap/AstInherit.php index a8e582ef4..50d83d679 100644 --- a/src/AstRunner/AstMap/AstInherit.php +++ b/src/AstRunner/AstMap/AstInherit.php @@ -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, ' -> ').')'; diff --git a/src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php b/src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php index 1dde5db13..69f23422b 100644 --- a/src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php +++ b/src/AstRunner/AstParser/NikicPhpParser/NikicPhpParser.php @@ -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())) { diff --git a/src/AstRunner/AstRunner.php b/src/AstRunner/AstRunner.php index 668712594..544b41cf3 100644 --- a/src/AstRunner/AstRunner.php +++ b/src/AstRunner/AstRunner.php @@ -45,7 +45,7 @@ public function createAstMapByFiles(array $files): AstMap } } - $this->dispatcher->dispatch(new PostCreateAstMapEvent($astMap)); + $this->dispatcher->dispatch(new PostCreateAstMapEvent()); return $astMap; } diff --git a/src/AstRunner/Event/PostCreateAstMapEvent.php b/src/AstRunner/Event/PostCreateAstMapEvent.php index 7e64fa672..4c600ad04 100644 --- a/src/AstRunner/Event/PostCreateAstMapEvent.php +++ b/src/AstRunner/Event/PostCreateAstMapEvent.php @@ -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; - } } diff --git a/src/Subscriber/ConsoleSubscriber.php b/src/Subscriber/ConsoleSubscriber.php index b55dc2703..0d83ab40a 100644 --- a/src/Subscriber/ConsoleSubscriber.php +++ b/src/Subscriber/ConsoleSubscriber.php @@ -79,17 +79,17 @@ public function onPreDependencyEmit(PreEmitEvent $event): void ); } - public function onPostDependencyEmit(): void + public function onPostDependencyEmit(PostEmitEvent $event): void { $this->output->writeln('end emitting dependencies', OutputInterface::VERBOSITY_VERBOSE); } - public function onPreDependencyFlatten(): void + public function onPreDependencyFlatten(PreFlattenEvent $event): void { $this->output->writeln('start flatten dependencies', OutputInterface::VERBOSITY_VERBOSE); } - public function onPostDependencyFlatten(): void + public function onPostDependencyFlatten(PostFlattenEvent $event): void { $this->output->writeln('end flatten dependencies', OutputInterface::VERBOSITY_VERBOSE); } diff --git a/tests/AstRunner/AstParser/NikicPhpParser/NikicPhpParserTest.php b/tests/AstRunner/AstParser/NikicPhpParser/NikicPhpParserTest.php new file mode 100644 index 000000000..9061e649a --- /dev/null +++ b/tests/AstRunner/AstParser/NikicPhpParser/NikicPhpParserTest.php @@ -0,0 +1,41 @@ +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()); + } +} diff --git a/tests/Subscriber/CacheableFileSubscriberTest.php b/tests/Subscriber/CacheableFileSubscriberTest.php new file mode 100644 index 000000000..f93a86a55 --- /dev/null +++ b/tests/Subscriber/CacheableFileSubscriberTest.php @@ -0,0 +1,41 @@ + '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()); + } +} diff --git a/tests/Subscriber/ConsoleSubscriberTest.php b/tests/Subscriber/ConsoleSubscriberTest.php index cd965614b..6aa6499f2 100644 --- a/tests/Subscriber/ConsoleSubscriberTest.php +++ b/tests/Subscriber/ConsoleSubscriberTest.php @@ -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()); } } diff --git a/tests/Subscriber/ProgressSubscriberTest.php b/tests/Subscriber/ProgressSubscriberTest.php index c5cacb690..b72699c87 100644 --- a/tests/Subscriber/ProgressSubscriberTest.php +++ b/tests/Subscriber/ProgressSubscriberTest.php @@ -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 = <<---------------------------] 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; + + 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 = <<---------------------------] 0% + 1/1 [============================] 100% - $dispatcher->dispatch(new PreCreateAstMapEvent(9999999)); - $result = $formatter->fetch(); +OUT; - static::assertStringContainsString('9999999', $result); + static::assertSame($expectedOutput, $formatter->fetch()); } }