Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Sep 16, 2023
1 parent ebd6a34 commit eccc7e0
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ protected function setUp(): void
public function testLocate(): void
{
$files = [
$this->createFile('MyClassA', '<?php class MyClassA {}'),
$this->createFile('MyClassB', '<?php class MyClassB {}'),
__DIR__ . '/.',
__DIR__ . '/..',
$this->createFile('MyClassA', '<?php class MyClassA {}'),
$this->createFile('MyClassB', '<?php class MyClassB {}'),
];

$roots = $this->locate($files);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<?php class MyClassA {}'],
'MyClassB.php' => '<?php class MyClassB {}',
]);

$files = $this->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([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions test/fixtures/unknownSymbols/src/YetAnotherThing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Example\Library;

use Foo\Bar\Baz;

final class YetAnotherThing
{
public function baz(Baz $baz)
{
$collection = new \Doctrine\Common\Collections\ArrayCollection([]);

libxml_clear_errors();

filter_var(
$baz->value(),
FILTER_VALIDATE_URL
);
}
}

0 comments on commit eccc7e0

Please sign in to comment.