Skip to content

Commit

Permalink
Classmap related cleanup, improve test data structure (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Feb 16, 2024
1 parent 3ac5875 commit d44a0ec
Show file tree
Hide file tree
Showing 33 changed files with 36 additions and 88 deletions.
2 changes: 1 addition & 1 deletion collision-detector.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"excludePaths": [
"tests/data/builtin/native-symbols.php"
"tests/data/not-autoloaded/builtin/native-symbols.php"
]
}
2 changes: 1 addition & 1 deletion composer-dependency-analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
return (new Configuration())
->addPathToScan(__FILE__, true)
->addPathToScan(__DIR__ . '/bin', false)
->ignoreErrorsOnPath(__DIR__ . '/tests/data', [ErrorType::UNKNOWN_CLASS]);
->ignoreErrorsOnPath(__DIR__ . '/tests/data/not-autoloaded', [ErrorType::UNKNOWN_CLASS]);
6 changes: 1 addition & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@
"ShipMonk\\ComposerDependencyAnalyser\\": "tests/"
},
"classmap": [
"tests/vendor/",
"tests/app/"
],
"exclude-from-classmap": [
"tests/data/"
"tests/data/autoloaded/"
]
},
"bin": [
Expand Down
3 changes: 0 additions & 3 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
<file>src/</file>
<file>tests/</file>

<exclude-pattern>tests/app/*</exclude-pattern>
<exclude-pattern>tests/data/*</exclude-pattern>
<exclude-pattern>tests/vendor/*</exclude-pattern>
<exclude-pattern>tests/composer/*</exclude-pattern>

<config name="installed_paths" value="../../slevomat/coding-standard"/>
<config name="php_version" value="70200"/>
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ parameters:
- tests
excludePaths:
analyseAndScan:
- tests/data/*
- tests/data/not-autoloaded/*
tmpDir: cache/phpstan/
checkMissingCallableSignature: true
checkUninitializedProperties: true
Expand Down
10 changes: 2 additions & 8 deletions src/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Analyser
*
* @var array<string, string>
*/
private $classmap;
private $classmap = [];

/**
* package name => is dev dependency
Expand All @@ -78,7 +78,6 @@ class Analyser
private $composerJsonDependencies;

/**
* @param array<string, string> $classmap className => filePath
* @param array<string, bool> $composerJsonDependencies package name => is dev dependency
* @throws InvalidPathException
*/
Expand All @@ -87,14 +86,9 @@ public function __construct(
ClassLoader $classLoader,
Configuration $config,
string $vendorDir,
array $composerJsonDependencies,
array $classmap = []
array $composerJsonDependencies
)
{
foreach ($classmap as $className => $filePath) {
$this->classmap[$className] = $this->realPath($filePath);
}

$this->stopwatch = $stopwatch;
$this->classLoader = $classLoader;
$this->config = $config;
Expand Down
60 changes: 12 additions & 48 deletions tests/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ class AnalyserTest extends TestCase
*/
public function test(callable $editConfig, AnalysisResult $expectedResult): void
{
$appDir = __DIR__ . '/app';
$vendorDir = __DIR__ . '/vendor';
$classmap = [
'Regular\Package\Clazz' => $vendorDir . '/regular/package/Clazz.php',
'Regular\Dead\Clazz' => $vendorDir . '/regular/dead/Clazz.php',
'Shadow\Package\Clazz' => $vendorDir . '/shadow/package/Clazz.php',
'Dev\Package\Clazz' => $vendorDir . '/dev/package/Clazz.php',
'App\Clazz' => $appDir . '/Clazz.php',
];
$vendorDir = __DIR__ . '/data/autoloaded/vendor';
$dependencies = [
'regular/package' => false,
'dev/package' => true,
Expand All @@ -48,8 +40,7 @@ public function test(callable $editConfig, AnalysisResult $expectedResult): void
$this->getClassLoaderMock(),
$config,
$vendorDir,
$dependencies,
$classmap
$dependencies
);
$result = $detector->run();

Expand All @@ -61,8 +52,8 @@ public function test(callable $editConfig, AnalysisResult $expectedResult): void
*/
public function provideConfigs(): iterable
{
$variousUsagesPath = realpath(__DIR__ . '/data/analysis/various-usages.php');
$unknownClassesPath = realpath(__DIR__ . '/data/analysis/unknown-classes.php');
$variousUsagesPath = realpath(__DIR__ . '/data/not-autoloaded/analysis/various-usages.php');
$unknownClassesPath = realpath(__DIR__ . '/data/not-autoloaded/analysis/unknown-classes.php');

if ($unknownClassesPath === false || $variousUsagesPath === false) {
throw new LogicException('Unable to realpath data files');
Expand Down Expand Up @@ -458,7 +449,7 @@ private function createAnalysisResult(int $scannedFiles, array $args, array $unu

public function testNativeTypesNotReported(): void
{
$path = realpath(__DIR__ . '/data/builtin/native-symbols.php');
$path = realpath(__DIR__ . '/data/not-autoloaded/builtin/native-symbols.php');
self::assertNotFalse($path);

$config = new Configuration();
Expand All @@ -483,36 +474,9 @@ public function testNativeTypesNotReported(): void
]), $result);
}

public function testAutoloadableClassNotInClassmap(): void
{
require __DIR__ . '/vendor/dev/package/Clazz.php';
require __DIR__ . '/vendor/regular/package/Clazz.php';

$path = realpath(__DIR__ . '/data/autoloadable-no-classmap/usage.php');
self::assertNotFalse($path);

$config = new Configuration();
$config->addPathToScan($path, true);
$config->addForceUsedSymbol('Regular\Package\Clazz');

$detector = new Analyser(
$this->getStopwatchMock(),
$this->getClassLoaderMock(),
$config,
__DIR__ . '/vendor',
[
'regular/package' => false,
'dev/package' => true
]
);
$result = $detector->run();

self::assertEquals($this->createAnalysisResult(1, []), $result);
}

public function testNoMultipleScansOfTheSameFile(): void
{
$path = realpath(__DIR__ . '/data/analysis/unknown-classes.php');
$path = realpath(__DIR__ . '/data/not-autoloaded/analysis/unknown-classes.php');
self::assertNotFalse($path);

$config = new Configuration();
Expand All @@ -523,7 +487,7 @@ public function testNoMultipleScansOfTheSameFile(): void
$this->getStopwatchMock(),
$this->getClassLoaderMock(),
$config,
__DIR__ . '/vendor',
__DIR__ . '/data/autoloaded/vendor',
[]
);
$result = $detector->run();
Expand All @@ -538,8 +502,8 @@ public function testNoMultipleScansOfTheSameFile(): void

public function testDevPathInsideProdPath(): void
{
$prodPath = realpath(__DIR__ . '/data/dev-in-subdirectory');
$devPath = realpath(__DIR__ . '/data/dev-in-subdirectory/dev');
$prodPath = realpath(__DIR__ . '/data/not-autoloaded/dev-in-subdirectory');
$devPath = realpath(__DIR__ . '/data/not-autoloaded/dev-in-subdirectory/dev');
self::assertNotFalse($prodPath);
self::assertNotFalse($devPath);

Expand All @@ -551,7 +515,7 @@ public function testDevPathInsideProdPath(): void
$this->getStopwatchMock(),
$this->getClassLoaderMock(),
$config,
__DIR__ . '/vendor',
__DIR__ . '/data/autoloaded/vendor',
[
'regular/package' => false,
'dev/package' => true
Expand All @@ -564,7 +528,7 @@ public function testDevPathInsideProdPath(): void

public function testExplicitFileWithoutExtension(): void
{
$path = realpath(__DIR__ . '/data/file-without-extension/script');
$path = realpath(__DIR__ . '/data/not-autoloaded/file-without-extension/script');
self::assertNotFalse($path);

$config = new Configuration();
Expand All @@ -574,7 +538,7 @@ public function testExplicitFileWithoutExtension(): void
$this->getStopwatchMock(),
$this->getClassLoaderMock(),
$config,
__DIR__ . '/vendor',
__DIR__ . '/data/autoloaded/vendor',
[
'dev/package' => true,
]
Expand Down
4 changes: 2 additions & 2 deletions tests/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function validateArgvDataProvider(): iterable
];

yield 'path passed' => [
'Cannot pass paths (vendor) to analyse as arguments, use --config instead.',
['bin/script.php', 'vendor']
'Cannot pass paths (data) to analyse as arguments, use --config instead.',
['bin/script.php', 'data']
];

yield 'valid bool options' => [
Expand Down
8 changes: 4 additions & 4 deletions tests/ComposerJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ComposerJsonTest extends TestCase

public function testComposerJson(): void
{
$composerJson = new ComposerJson(__DIR__ . '/composer/sample.json');
$composerJson = new ComposerJson(__DIR__ . '/data/not-autoloaded/composer/sample.json');

self::assertSame(
[
Expand All @@ -22,9 +22,9 @@ public function testComposerJson(): void

self::assertSame(
[
realpath(__DIR__ . '/composer/dir2/file1.php') => false,
realpath(__DIR__ . '/composer/dir1') => false,
realpath(__DIR__ . '/composer/dir2') => false,
realpath(__DIR__ . '/data/not-autoloaded/composer/dir2/file1.php') => false,
realpath(__DIR__ . '/data/not-autoloaded/composer/dir1') => false,
realpath(__DIR__ . '/data/not-autoloaded/composer/dir2') => false,
],
$composerJson->autoloadPaths
);
Expand Down
12 changes: 6 additions & 6 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function testShouldIgnore(): void
$configuration = new Configuration();
$configuration->ignoreUnknownClasses(['Unknown\Clazz']);
$configuration->ignoreErrors([ErrorType::UNUSED_DEPENDENCY, ErrorType::UNKNOWN_CLASS]);
$configuration->ignoreErrorsOnPath(__DIR__ . '/app/../', [ErrorType::SHADOW_DEPENDENCY]);
$configuration->ignoreErrorsOnPath(__DIR__ . '/data/../', [ErrorType::SHADOW_DEPENDENCY]);
$configuration->ignoreErrorsOnPackage('my/package', [ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV]);
$configuration->ignoreErrorsOnPackageAndPath('vendor/package', __DIR__ . '/../tests/app', [ErrorType::DEV_DEPENDENCY_IN_PROD]);
$configuration->ignoreErrorsOnPackageAndPath('vendor/package', __DIR__ . '/../tests/data', [ErrorType::DEV_DEPENDENCY_IN_PROD]);

$ignoreList = $configuration->getIgnoreList();

Expand Down Expand Up @@ -50,10 +50,10 @@ public function testShouldIgnore(): void
self::assertTrue($ignoreList->shouldIgnoreError(ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV, __DIR__, 'my/package'));

self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__, null));
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'app', null));
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'app', 'wrong/package'));
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', 'vendor/package'));
self::assertTrue($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'app', 'vendor/package'));
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', null));
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', 'wrong/package'));
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . '..', 'vendor/package'));
self::assertTrue($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', 'vendor/package'));
}

public function testOverlappingUnusedIgnores(): void
Expand Down
12 changes: 6 additions & 6 deletions tests/UsedSymbolExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function test(string $path, array $expectedUsages): void
public function provideVariants(): iterable
{
yield 'use statements' => [
__DIR__ . '/data/used-symbols/use-statements.php',
__DIR__ . '/data/not-autoloaded/used-symbols/use-statements.php',
[
'PHPUnit\Framework\Exception' => [11],
'PHPUnit\Framework\Warning' => [12],
Expand All @@ -42,7 +42,7 @@ public function provideVariants(): iterable
];

yield 'various usages' => [
__DIR__ . '/data/used-symbols/various-usages.php',
__DIR__ . '/data/not-autoloaded/used-symbols/various-usages.php',
[
'DateTimeImmutable' => [12],
'DateTimeInterface' => [12],
Expand All @@ -53,30 +53,30 @@ public function provideVariants(): iterable
];

yield 'bracket namespace' => [
__DIR__ . '/data/used-symbols/bracket-namespace.php',
__DIR__ . '/data/not-autoloaded/used-symbols/bracket-namespace.php',
[
'DateTimeImmutable' => [5],
'DateTime' => [11],
]
];

yield 'other symbols' => [
__DIR__ . '/data/used-symbols/other-symbols.php',
__DIR__ . '/data/not-autoloaded/used-symbols/other-symbols.php',
[
'DIRECTORY_SEPARATOR' => [9],
'strlen' => [11],
]
];

yield 'relative namespace' => [
__DIR__ . '/data/used-symbols/relative-namespace.php',
__DIR__ . '/data/not-autoloaded/used-symbols/relative-namespace.php',
[
'DateTimeImmutable' => [10],
]
];

yield 'global namespace' => [
__DIR__ . '/data/used-symbols/global-namespace.php',
__DIR__ . '/data/not-autoloaded/used-symbols/global-namespace.php',
[
'DateTimeImmutable' => [3],
]
Expand Down
3 changes: 0 additions & 3 deletions tests/data/autoloadable-no-classmap/usage.php

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d44a0ec

Please sign in to comment.