From eccc7e0dd157c0d620887926839db4dd1860adcd Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sat, 16 Sep 2023 12:32:48 +0100 Subject: [PATCH] Increase test coverage --- .../LocateComposerPackageSourceFiles.php | 1 + .../ASTLocator/LocateASTFromFilesTest.php | 4 +- .../DefinedExtensionsResolverTest.php | 41 +++++++++++++++---- .../LocateComposerPackageSourceFilesTest.php | 15 +++++++ .../LocateFilesByGlobPatternTest.php | 2 +- .../DefinedSymbolCollectorTest.php | 4 +- .../NodeVisitor/UsedSymbolCollectorTest.php | 30 +++++++++++++- .../LocateUsedSymbolsFromASTRootsTest.php | 5 ++- .../unknownSymbols/src/YetAnotherThing.php | 22 ++++++++++ 9 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/unknownSymbols/src/YetAnotherThing.php diff --git a/src/ComposerRequireChecker/FileLocator/LocateComposerPackageSourceFiles.php b/src/ComposerRequireChecker/FileLocator/LocateComposerPackageSourceFiles.php index 7cfae3c7..89692697 100644 --- a/src/ComposerRequireChecker/FileLocator/LocateComposerPackageSourceFiles.php +++ b/src/ComposerRequireChecker/FileLocator/LocateComposerPackageSourceFiles.php @@ -100,6 +100,7 @@ function (string $sourceDir) use ($packageDir) { private function normalizePath(string $path): string { + // @infection-ignore-all UnwrapStrReplace False positive on Linux; this guards against Windows paths. return str_replace('\\', '/', $path); } diff --git a/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php b/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php index ffa3995a..6ac8947c 100644 --- a/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php +++ b/test/ComposerRequireCheckerTest/ASTLocator/LocateASTFromFilesTest.php @@ -35,10 +35,10 @@ protected function setUp(): void public function testLocate(): void { $files = [ - $this->createFile('MyClassA', 'createFile('MyClassB', 'createFile('MyClassA', 'createFile('MyClassB', 'locate($files); diff --git a/test/ComposerRequireCheckerTest/DefinedExtensionsResolver/DefinedExtensionsResolverTest.php b/test/ComposerRequireCheckerTest/DefinedExtensionsResolver/DefinedExtensionsResolverTest.php index 3b7197a7..fc62ce81 100644 --- a/test/ComposerRequireCheckerTest/DefinedExtensionsResolver/DefinedExtensionsResolverTest.php +++ b/test/ComposerRequireCheckerTest/DefinedExtensionsResolver/DefinedExtensionsResolverTest.php @@ -9,8 +9,6 @@ use org\bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; -use function reset; - /** @covers \ComposerRequireChecker\DefinedExtensionsResolver\DefinedExtensionsResolver */ final class DefinedExtensionsResolverTest extends TestCase { @@ -40,10 +38,10 @@ public function testCoreExtensions(): void ->setContent('{"require":{"php":"^7.0"}}') ->url(); - $extensions = ($this->resolver)($composerJson, ['ext-foo' => '*']); + $extensions = ($this->resolver)($composerJson, ['foo']); $this->assertCount(1, $extensions); - $this->assertSame('*', reset($extensions)); + $this->assertContains('foo', $extensions); } public function testCoreExtensionsIn64Bit(): void @@ -52,10 +50,10 @@ public function testCoreExtensionsIn64Bit(): void ->setContent('{"require":{"php-64bit":"^7.0"}}') ->url(); - $extensions = ($this->resolver)($composerJson, ['ext-foo' => '*']); + $extensions = ($this->resolver)($composerJson, ['foo']); $this->assertCount(1, $extensions); - $this->assertSame('*', reset($extensions)); + $this->assertContains('foo', $extensions); } public function testExtensionsAreReturned(): void @@ -64,10 +62,39 @@ public function testExtensionsAreReturned(): void ->setContent('{"require":{"ext-zip":"*","ext-curl":"*"}}') ->url(); - $extensions = ($this->resolver)($composerJson); + $extensions = ($this->resolver)($composerJson, ['foo']); $this->assertCount(2, $extensions); $this->assertContains('zip', $extensions); $this->assertContains('curl', $extensions); + $this->assertNotContains('foo', $extensions); + } + + public function testExtensionsAreAddedWhenBothCoreAndExtensionsRequired(): void + { + $composerJson = vfsStream::newFile('composer.json')->at($this->root) + ->setContent('{"require":{"php":"~8.2.0","ext-zip":"*","ext-curl":"*"}}') + ->url(); + + $extensions = ($this->resolver)($composerJson, ['foo']); + + $this->assertCount(3, $extensions); + $this->assertContains('foo', $extensions); + $this->assertContains('curl', $extensions); + $this->assertContains('zip', $extensions); + } + + public function testExtensionsFoundWhenAfterOtherPackages(): void + { + $composerJson = vfsStream::newFile('composer.json')->at($this->root) + ->setContent('{"require":{"maglnet/composer-require-checker":"*","php":"~8.2.0","ext-zip":"*","ext-curl":"*"}}') + ->url(); + + $extensions = ($this->resolver)($composerJson, ['foo']); + + $this->assertCount(3, $extensions); + $this->assertContains('foo', $extensions); + $this->assertContains('curl', $extensions); + $this->assertContains('zip', $extensions); } } diff --git a/test/ComposerRequireCheckerTest/FileLocator/LocateComposerPackageSourceFilesTest.php b/test/ComposerRequireCheckerTest/FileLocator/LocateComposerPackageSourceFilesTest.php index 3cef6918..1f876019 100644 --- a/test/ComposerRequireCheckerTest/FileLocator/LocateComposerPackageSourceFilesTest.php +++ b/test/ComposerRequireCheckerTest/FileLocator/LocateComposerPackageSourceFilesTest.php @@ -47,6 +47,21 @@ public function testFromClassmap(): void $this->assertContains($this->root->getChild('MyClassB.php')->url(), $files); } + public function testFromClassmapWithStrangePaths(): void + { + vfsStream::create([ + 'composer.json' => '{"autoload": {"classmap": ["/src/MyClassA.php", "MyClassB.php"]}}', + 'src' => ['MyClassA.php' => ' 'files($this->root->getChild('composer.json')->url()); + + $this->assertCount(2, $files); + $this->assertContains($this->root->getChild('src/MyClassA.php')->url(), $files); + $this->assertContains($this->root->getChild('MyClassB.php')->url(), $files); + } + public function testFromFiles(): void { vfsStream::create([ diff --git a/test/ComposerRequireCheckerTest/FileLocator/LocateFilesByGlobPatternTest.php b/test/ComposerRequireCheckerTest/FileLocator/LocateFilesByGlobPatternTest.php index ba8047e1..e8c577b7 100644 --- a/test/ComposerRequireCheckerTest/FileLocator/LocateFilesByGlobPatternTest.php +++ b/test/ComposerRequireCheckerTest/FileLocator/LocateFilesByGlobPatternTest.php @@ -45,7 +45,7 @@ public function testGlobPattern(): void ], ]); - $files = $this->files(['bin/console*.php'], $this->root->url()); + $files = $this->files(['bin/console*.php'], $this->root->url() . '/'); self::assertCount(2, $files); self::assertContains($this->root->getChild('bin/console.php')->url(), $files); self::assertContains($this->root->getChild('bin/console123.php')->url(), $files); diff --git a/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorTest.php b/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorTest.php index c76c4bfe..dbb146f1 100644 --- a/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorTest.php +++ b/test/ComposerRequireCheckerTest/NodeVisitor/DefinedSymbolCollectorTest.php @@ -39,8 +39,10 @@ protected function setUp(): void public function testExceptionWhenNoNamespaceDefined(): void { + $node = new Class_('gedöns'); + $message = 'Given node of type "' . Class_::class . '" (defined at line -1) does not have an assigned "namespacedName" property: did you pass it through a name resolver visitor?'; $this->expectException(UnexpectedValueException::class); - $node = new Class_('gedöns'); + $this->expectExceptionMessage($message); $this->collector->enterNode($node); } diff --git a/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php b/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php index acfee962..92528420 100644 --- a/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php +++ b/test/ComposerRequireCheckerTest/NodeVisitor/UsedSymbolCollectorTest.php @@ -65,6 +65,18 @@ public function testExtendingInterface(): void $this->assertContains('Baz', $symbols); } + public function testInterfaceWithoutExtends(): void + { + $node = new Interface_('Foo'); + $node->extends = [null]; + + $this->visitor->enterNode($node); + + $symbols = $this->visitor->getCollectedSymbols(); + $this->assertCount(0, $symbols); + $this->assertSame([], $symbols); + } + public function testImplements(): void { $node = new Class_('Foo'); @@ -216,6 +228,19 @@ public function testFunctionReturnType(): void $this->assertContains('Bar', $symbols); } + public function testFunctionReturnTypeWithIdentifier(): void + { + $functionName = new Name('foo'); + $node = new Function_($functionName); + $node->returnType = new Identifier('Bar'); + + $this->visitor->enterNode($node); + + $symbols = $this->visitor->getCollectedSymbols(); + $this->assertCount(1, $symbols); + $this->assertContains('Bar', $symbols); + } + public function testMethodReturnType(): void { $functionName = new Name('foo'); @@ -267,8 +292,9 @@ public function testTraitUseVisibilityAdaptation(): void public function testTraitUsePrecedenceAdaptation(): void { - $traitUseAdaption = new Precedence(new Name('Bar'), 'testMethod', [new Name('Baz')]); - $traitUse = new TraitUse([new Name('Foo')], [$traitUseAdaption]); + $traitUseAdaption2 = new Precedence(new Name('Bar'), 'testMethod', [new Name('Baz')]); + $traitUseAdaption = new Alias(null, 'testMethod2', Class_::MODIFIER_PUBLIC, null); + $traitUse = new TraitUse([new Name('Foo')], [$traitUseAdaption, $traitUseAdaption2]); $this->visitor->enterNode($traitUse); diff --git a/test/ComposerRequireCheckerTest/UsedSymbolsLocator/LocateUsedSymbolsFromASTRootsTest.php b/test/ComposerRequireCheckerTest/UsedSymbolsLocator/LocateUsedSymbolsFromASTRootsTest.php index 8a609c23..99d75ca1 100644 --- a/test/ComposerRequireCheckerTest/UsedSymbolsLocator/LocateUsedSymbolsFromASTRootsTest.php +++ b/test/ComposerRequireCheckerTest/UsedSymbolsLocator/LocateUsedSymbolsFromASTRootsTest.php @@ -58,9 +58,10 @@ public function testInvokeReturnsSymbolsSorted(): void $parser = $parserFactory->create(ParserFactory::PREFER_PHP7); - $ast = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/unknownSymbols/src/OtherThing.php')); + $ast1 = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/unknownSymbols/src/OtherThing.php')); + $ast2 = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/unknownSymbols/src/YetAnotherThing.php')); - $symbols = $this->locate([$ast]); + $symbols = $this->locate([$ast1, $ast2]); $this->assertSame($expectedSymbols, $symbols); } diff --git a/test/fixtures/unknownSymbols/src/YetAnotherThing.php b/test/fixtures/unknownSymbols/src/YetAnotherThing.php new file mode 100644 index 00000000..0ae33142 --- /dev/null +++ b/test/fixtures/unknownSymbols/src/YetAnotherThing.php @@ -0,0 +1,22 @@ +value(), + FILTER_VALIDATE_URL + ); + } +}