Skip to content

Commit

Permalink
Refactoring tests to enable testing PHPStan implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkusebauch committed Mar 10, 2024
1 parent fb82f79 commit 59a7e2b
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 177 deletions.
2 changes: 2 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use Qossmic\Deptrac\Core\Ast\Parser\ParserInterface;
use Qossmic\Deptrac\Core\Ast\Parser\PhpStanParser\PhpStanContainerDecorator;
use Qossmic\Deptrac\Core\Ast\Parser\PhpStanParser\PhpStanParser;
use Qossmic\Deptrac\Core\Ast\Parser\PhpStanParser\PhpStanTypeResolver;
use Qossmic\Deptrac\Core\Dependency\DependencyResolver;
use Qossmic\Deptrac\Core\Dependency\Emitter\ClassDependencyEmitter;
use Qossmic\Deptrac\Core\Dependency\Emitter\ClassSuperglobalDependencyEmitter;
Expand Down Expand Up @@ -186,6 +187,7 @@
]);
$services->alias(ParserInterface::class, PhpStanParser::class);
$services->set(NikicTypeResolver::class);
$services->set(PhpStanTypeResolver::class);
$services
->set(AnonymousClassExtractor::class)
->tag('reference_extractors');
Expand Down
19 changes: 0 additions & 19 deletions tests/Core/Ast/ArrayAssertionTrait.php

This file was deleted.

98 changes: 69 additions & 29 deletions tests/Core/Ast/AstMapFlattenGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeToken;
use Qossmic\Deptrac\Core\Ast\Parser\Cache\AstFileReferenceInMemoryCache;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\ClassExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\ClassMethodExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\InterfaceExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\NewExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\PropertyExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\Extractors\VariableExtractor;
use Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser;
use Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicTypeResolver;
use Qossmic\Deptrac\Core\Ast\Parser\ParserInterface;
use Qossmic\Deptrac\Core\Ast\Parser\PhpStanParser\PhpStanContainerDecorator;
use Qossmic\Deptrac\Core\Ast\Parser\PhpStanParser\PhpStanParser;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Stopwatch\Stopwatch;
Expand All @@ -45,32 +52,47 @@

final class AstMapFlattenGeneratorTest extends TestCase
{
use ArrayAssertionTrait;

private TraceableEventDispatcher $eventDispatcher;
private AstLoader $astLoader;

protected function setUp(): void
{
parent::setUp();
$parser = $this->providedData()[0];

$this->eventDispatcher = new TraceableEventDispatcher(
new EventDispatcher(),
new Stopwatch()
);
$this->astLoader = new AstLoader(
new NikicPhpParser(
(new ParserFactory())->create(ParserFactory::ONLY_PHP7, new Lexer()),
new AstFileReferenceInMemoryCache(),
[
new ClassExtractor(),
new InterfaceExtractor(),
]
),
$parser,
$this->eventDispatcher
);
}

/**
* @return list<array{ParserInterface}>
*/
public static function createParser(): array
{
$phpStanContainer = new PhpStanContainerDecorator('', []);
$extractors = [
new ClassExtractor(),
new InterfaceExtractor(),
];
$cache = new AstFileReferenceInMemoryCache();
$parser = new NikicPhpParser(
(new ParserFactory())->create(ParserFactory::ONLY_PHP7, new Lexer()), $cache, $extractors
);

$phpstanParser = new PhpStanParser($phpStanContainer, $cache, $extractors);

return [
'Nikic Parser' => [$parser],
'PHPStan Parser' => [$phpstanParser],
];
}

protected function tearDown(): void
{
parent::tearDown();
Expand All @@ -96,6 +118,9 @@ private function getInheritedInherits(string $class, AstMap $astMap): array
return $inherits;
}

/**
* @dataProvider createParser
*/
public function testBasicInheritance(): void
{
$expectedEvents = [
Expand All @@ -109,30 +134,30 @@ public function testBasicInheritance(): void
$dispatchedEvents = $this->eventDispatcher->getOrphanedEvents();
self::assertSame($expectedEvents, $dispatchedEvents);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(FixtureBasicInheritanceA::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(FixtureBasicInheritanceB::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
['Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceA::6 (Extends) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceB::7 (Extends))'],
$this->getInheritedInherits(FixtureBasicInheritanceC::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceA::6 (Extends) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceC::8 (Extends) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceB::7 (Extends))',
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceB::7 (Extends) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceC::8 (Extends))',
],
$this->getInheritedInherits(FixtureBasicInheritanceD::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceA::6 (Extends) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceD::9 (Extends) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceC::8 (Extends) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceB::7 (Extends))',
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceB::7 (Extends) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceD::9 (Extends) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceC::8 (Extends))',
Expand All @@ -142,6 +167,9 @@ public function testBasicInheritance(): void
);
}

/**
* @dataProvider createParser
*/
public function testBasicInheritanceInterfaces(): void
{
$expectedEvents = [
Expand All @@ -155,30 +183,30 @@ public function testBasicInheritanceInterfaces(): void
$dispatchedEvents = $this->eventDispatcher->getOrphanedEvents();
self::assertSame($expectedEvents, $dispatchedEvents);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(FixtureBasicInheritanceInterfaceA::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(FixtureBasicInheritanceInterfaceB::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
['Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceA::6 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceB::7 (Implements))'],
$this->getInheritedInherits(FixtureBasicInheritanceInterfaceC::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceA::6 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceC::8 (Implements) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceB::7 (Implements))',
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceB::7 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceC::8 (Implements))',
],
$this->getInheritedInherits(FixtureBasicInheritanceInterfaceD::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceA::6 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceD::9 (Implements) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceC::8 (Implements) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceB::7 (Implements))',
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceB::7 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceD::9 (Implements) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\FixtureBasicInheritanceInterfaceC::8 (Implements))',
Expand All @@ -188,6 +216,9 @@ public function testBasicInheritanceInterfaces(): void
);
}

/**
* @dataProvider createParser
*/
public function testBasicMultipleInheritanceInterfaces(): void
{
$expectedEvents = [
Expand All @@ -201,30 +232,30 @@ public function testBasicMultipleInheritanceInterfaces(): void
$dispatchedEvents = $this->eventDispatcher->getOrphanedEvents();
self::assertSame($expectedEvents, $dispatchedEvents);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(MultipleInteritanceA1::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(MultipleInteritanceA2::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(MultipleInteritanceA::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA1::7 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA::8 (Implements))',
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA2::7 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA::8 (Implements))',
],
$this->getInheritedInherits(MultipleInteritanceB::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA1::7 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceB::9 (Implements) -> Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA::8 (Implements))',
'Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceA1::8 (Implements) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\MultipleInteritanceB::9 (Implements))',
Expand All @@ -235,6 +266,9 @@ public function testBasicMultipleInheritanceInterfaces(): void
);
}

/**
* @dataProvider createParser
*/
public function testBasicMultipleInheritanceWithNoise(): void
{
$expectedEvents = [
Expand All @@ -248,23 +282,26 @@ public function testBasicMultipleInheritanceWithNoise(): void
$dispatchedEvents = $this->eventDispatcher->getOrphanedEvents();
self::assertSame($expectedEvents, $dispatchedEvents);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(FixtureBasicInheritanceWithNoiseA::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
[],
$this->getInheritedInherits(FixtureBasicInheritanceWithNoiseB::class, $astMap)
);

self::assertArrayValuesEquals(
self::assertEqualsCanonicalizing(
['Tests\Qossmic\Deptrac\Core\Ast\Fixtures\BasicInheritance\FixtureBasicInheritanceWithNoiseA::18 (Extends) (path: Tests\Qossmic\Deptrac\Core\Ast\Fixtures\BasicInheritance\FixtureBasicInheritanceWithNoiseB::19 (Extends))'],
$this->getInheritedInherits(FixtureBasicInheritanceWithNoiseC::class, $astMap)
);
}

public function testSkipsErrorsAndDisptachesErrorEventAndReturnsEmptyAstMap(): void
/**
* @dataProvider createParser
*/
public function testSkipsErrorsAndDispatchesErrorEventAndReturnsEmptyAstMap(): void
{
$expectedEvents = [
PreCreateAstMapEvent::class,
Expand All @@ -286,6 +323,9 @@ public function testSkipsErrorsAndDisptachesErrorEventAndReturnsEmptyAstMap(): v
self::assertSame($expectedEvents, $dispatchedEvents);
}

/**
* @dataProvider createParser
*/
public function testThrowsOtherExceptions(): void
{
$parser = $this->createMock(ParserInterface::class);
Expand Down
Loading

0 comments on commit 59a7e2b

Please sign in to comment.