Skip to content

Commit d44a0ec

Browse files
authored
Classmap related cleanup, improve test data structure (#64)
1 parent 3ac5875 commit d44a0ec

33 files changed

+36
-88
lines changed

collision-detector.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"excludePaths": [
3-
"tests/data/builtin/native-symbols.php"
3+
"tests/data/not-autoloaded/builtin/native-symbols.php"
44
]
55
}

composer-dependency-analyser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
return (new Configuration())
77
->addPathToScan(__FILE__, true)
88
->addPathToScan(__DIR__ . '/bin', false)
9-
->ignoreErrorsOnPath(__DIR__ . '/tests/data', [ErrorType::UNKNOWN_CLASS]);
9+
->ignoreErrorsOnPath(__DIR__ . '/tests/data/not-autoloaded', [ErrorType::UNKNOWN_CLASS]);

composer.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@
4242
"ShipMonk\\ComposerDependencyAnalyser\\": "tests/"
4343
},
4444
"classmap": [
45-
"tests/vendor/",
46-
"tests/app/"
47-
],
48-
"exclude-from-classmap": [
49-
"tests/data/"
45+
"tests/data/autoloaded/"
5046
]
5147
},
5248
"bin": [

phpcs.xml.dist

-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
<file>src/</file>
1919
<file>tests/</file>
2020

21-
<exclude-pattern>tests/app/*</exclude-pattern>
2221
<exclude-pattern>tests/data/*</exclude-pattern>
23-
<exclude-pattern>tests/vendor/*</exclude-pattern>
24-
<exclude-pattern>tests/composer/*</exclude-pattern>
2522

2623
<config name="installed_paths" value="../../slevomat/coding-standard"/>
2724
<config name="php_version" value="70200"/>

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ parameters:
1313
- tests
1414
excludePaths:
1515
analyseAndScan:
16-
- tests/data/*
16+
- tests/data/not-autoloaded/*
1717
tmpDir: cache/phpstan/
1818
checkMissingCallableSignature: true
1919
checkUninitializedProperties: true

src/Analyser.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Analyser
6868
*
6969
* @var array<string, string>
7070
*/
71-
private $classmap;
71+
private $classmap = [];
7272

7373
/**
7474
* package name => is dev dependency
@@ -78,7 +78,6 @@ class Analyser
7878
private $composerJsonDependencies;
7979

8080
/**
81-
* @param array<string, string> $classmap className => filePath
8281
* @param array<string, bool> $composerJsonDependencies package name => is dev dependency
8382
* @throws InvalidPathException
8483
*/
@@ -87,14 +86,9 @@ public function __construct(
8786
ClassLoader $classLoader,
8887
Configuration $config,
8988
string $vendorDir,
90-
array $composerJsonDependencies,
91-
array $classmap = []
89+
array $composerJsonDependencies
9290
)
9391
{
94-
foreach ($classmap as $className => $filePath) {
95-
$this->classmap[$className] = $this->realPath($filePath);
96-
}
97-
9892
$this->stopwatch = $stopwatch;
9993
$this->classLoader = $classLoader;
10094
$this->config = $config;

tests/AnalyserTest.php

+12-48
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ class AnalyserTest extends TestCase
2424
*/
2525
public function test(callable $editConfig, AnalysisResult $expectedResult): void
2626
{
27-
$appDir = __DIR__ . '/app';
28-
$vendorDir = __DIR__ . '/vendor';
29-
$classmap = [
30-
'Regular\Package\Clazz' => $vendorDir . '/regular/package/Clazz.php',
31-
'Regular\Dead\Clazz' => $vendorDir . '/regular/dead/Clazz.php',
32-
'Shadow\Package\Clazz' => $vendorDir . '/shadow/package/Clazz.php',
33-
'Dev\Package\Clazz' => $vendorDir . '/dev/package/Clazz.php',
34-
'App\Clazz' => $appDir . '/Clazz.php',
35-
];
27+
$vendorDir = __DIR__ . '/data/autoloaded/vendor';
3628
$dependencies = [
3729
'regular/package' => false,
3830
'dev/package' => true,
@@ -48,8 +40,7 @@ public function test(callable $editConfig, AnalysisResult $expectedResult): void
4840
$this->getClassLoaderMock(),
4941
$config,
5042
$vendorDir,
51-
$dependencies,
52-
$classmap
43+
$dependencies
5344
);
5445
$result = $detector->run();
5546

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

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

459450
public function testNativeTypesNotReported(): void
460451
{
461-
$path = realpath(__DIR__ . '/data/builtin/native-symbols.php');
452+
$path = realpath(__DIR__ . '/data/not-autoloaded/builtin/native-symbols.php');
462453
self::assertNotFalse($path);
463454

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

486-
public function testAutoloadableClassNotInClassmap(): void
487-
{
488-
require __DIR__ . '/vendor/dev/package/Clazz.php';
489-
require __DIR__ . '/vendor/regular/package/Clazz.php';
490-
491-
$path = realpath(__DIR__ . '/data/autoloadable-no-classmap/usage.php');
492-
self::assertNotFalse($path);
493-
494-
$config = new Configuration();
495-
$config->addPathToScan($path, true);
496-
$config->addForceUsedSymbol('Regular\Package\Clazz');
497-
498-
$detector = new Analyser(
499-
$this->getStopwatchMock(),
500-
$this->getClassLoaderMock(),
501-
$config,
502-
__DIR__ . '/vendor',
503-
[
504-
'regular/package' => false,
505-
'dev/package' => true
506-
]
507-
);
508-
$result = $detector->run();
509-
510-
self::assertEquals($this->createAnalysisResult(1, []), $result);
511-
}
512-
513477
public function testNoMultipleScansOfTheSameFile(): void
514478
{
515-
$path = realpath(__DIR__ . '/data/analysis/unknown-classes.php');
479+
$path = realpath(__DIR__ . '/data/not-autoloaded/analysis/unknown-classes.php');
516480
self::assertNotFalse($path);
517481

518482
$config = new Configuration();
@@ -523,7 +487,7 @@ public function testNoMultipleScansOfTheSameFile(): void
523487
$this->getStopwatchMock(),
524488
$this->getClassLoaderMock(),
525489
$config,
526-
__DIR__ . '/vendor',
490+
__DIR__ . '/data/autoloaded/vendor',
527491
[]
528492
);
529493
$result = $detector->run();
@@ -538,8 +502,8 @@ public function testNoMultipleScansOfTheSameFile(): void
538502

539503
public function testDevPathInsideProdPath(): void
540504
{
541-
$prodPath = realpath(__DIR__ . '/data/dev-in-subdirectory');
542-
$devPath = realpath(__DIR__ . '/data/dev-in-subdirectory/dev');
505+
$prodPath = realpath(__DIR__ . '/data/not-autoloaded/dev-in-subdirectory');
506+
$devPath = realpath(__DIR__ . '/data/not-autoloaded/dev-in-subdirectory/dev');
543507
self::assertNotFalse($prodPath);
544508
self::assertNotFalse($devPath);
545509

@@ -551,7 +515,7 @@ public function testDevPathInsideProdPath(): void
551515
$this->getStopwatchMock(),
552516
$this->getClassLoaderMock(),
553517
$config,
554-
__DIR__ . '/vendor',
518+
__DIR__ . '/data/autoloaded/vendor',
555519
[
556520
'regular/package' => false,
557521
'dev/package' => true
@@ -564,7 +528,7 @@ public function testDevPathInsideProdPath(): void
564528

565529
public function testExplicitFileWithoutExtension(): void
566530
{
567-
$path = realpath(__DIR__ . '/data/file-without-extension/script');
531+
$path = realpath(__DIR__ . '/data/not-autoloaded/file-without-extension/script');
568532
self::assertNotFalse($path);
569533

570534
$config = new Configuration();
@@ -574,7 +538,7 @@ public function testExplicitFileWithoutExtension(): void
574538
$this->getStopwatchMock(),
575539
$this->getClassLoaderMock(),
576540
$config,
577-
__DIR__ . '/vendor',
541+
__DIR__ . '/data/autoloaded/vendor',
578542
[
579543
'dev/package' => true,
580544
]

tests/CliTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function validateArgvDataProvider(): iterable
4646
];
4747

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

5353
yield 'valid bool options' => [

tests/ComposerJsonTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ComposerJsonTest extends TestCase
1010

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

1515
self::assertSame(
1616
[
@@ -22,9 +22,9 @@ public function testComposerJson(): void
2222

2323
self::assertSame(
2424
[
25-
realpath(__DIR__ . '/composer/dir2/file1.php') => false,
26-
realpath(__DIR__ . '/composer/dir1') => false,
27-
realpath(__DIR__ . '/composer/dir2') => false,
25+
realpath(__DIR__ . '/data/not-autoloaded/composer/dir2/file1.php') => false,
26+
realpath(__DIR__ . '/data/not-autoloaded/composer/dir1') => false,
27+
realpath(__DIR__ . '/data/not-autoloaded/composer/dir2') => false,
2828
],
2929
$composerJson->autoloadPaths
3030
);

tests/ConfigurationTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public function testShouldIgnore(): void
2020
$configuration = new Configuration();
2121
$configuration->ignoreUnknownClasses(['Unknown\Clazz']);
2222
$configuration->ignoreErrors([ErrorType::UNUSED_DEPENDENCY, ErrorType::UNKNOWN_CLASS]);
23-
$configuration->ignoreErrorsOnPath(__DIR__ . '/app/../', [ErrorType::SHADOW_DEPENDENCY]);
23+
$configuration->ignoreErrorsOnPath(__DIR__ . '/data/../', [ErrorType::SHADOW_DEPENDENCY]);
2424
$configuration->ignoreErrorsOnPackage('my/package', [ErrorType::PROD_DEPENDENCY_ONLY_IN_DEV]);
25-
$configuration->ignoreErrorsOnPackageAndPath('vendor/package', __DIR__ . '/../tests/app', [ErrorType::DEV_DEPENDENCY_IN_PROD]);
25+
$configuration->ignoreErrorsOnPackageAndPath('vendor/package', __DIR__ . '/../tests/data', [ErrorType::DEV_DEPENDENCY_IN_PROD]);
2626

2727
$ignoreList = $configuration->getIgnoreList();
2828

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

5252
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__, null));
53-
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'app', null));
54-
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'app', 'wrong/package'));
55-
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', 'vendor/package'));
56-
self::assertTrue($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'app', 'vendor/package'));
53+
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', null));
54+
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', 'wrong/package'));
55+
self::assertFalse($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . '..', 'vendor/package'));
56+
self::assertTrue($ignoreList->shouldIgnoreError(ErrorType::DEV_DEPENDENCY_IN_PROD, __DIR__ . DIRECTORY_SEPARATOR . 'data', 'vendor/package'));
5757
}
5858

5959
public function testOverlappingUnusedIgnores(): void

tests/UsedSymbolExtractorTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function test(string $path, array $expectedUsages): void
2828
public function provideVariants(): iterable
2929
{
3030
yield 'use statements' => [
31-
__DIR__ . '/data/used-symbols/use-statements.php',
31+
__DIR__ . '/data/not-autoloaded/used-symbols/use-statements.php',
3232
[
3333
'PHPUnit\Framework\Exception' => [11],
3434
'PHPUnit\Framework\Warning' => [12],
@@ -42,7 +42,7 @@ public function provideVariants(): iterable
4242
];
4343

4444
yield 'various usages' => [
45-
__DIR__ . '/data/used-symbols/various-usages.php',
45+
__DIR__ . '/data/not-autoloaded/used-symbols/various-usages.php',
4646
[
4747
'DateTimeImmutable' => [12],
4848
'DateTimeInterface' => [12],
@@ -53,30 +53,30 @@ public function provideVariants(): iterable
5353
];
5454

5555
yield 'bracket namespace' => [
56-
__DIR__ . '/data/used-symbols/bracket-namespace.php',
56+
__DIR__ . '/data/not-autoloaded/used-symbols/bracket-namespace.php',
5757
[
5858
'DateTimeImmutable' => [5],
5959
'DateTime' => [11],
6060
]
6161
];
6262

6363
yield 'other symbols' => [
64-
__DIR__ . '/data/used-symbols/other-symbols.php',
64+
__DIR__ . '/data/not-autoloaded/used-symbols/other-symbols.php',
6565
[
6666
'DIRECTORY_SEPARATOR' => [9],
6767
'strlen' => [11],
6868
]
6969
];
7070

7171
yield 'relative namespace' => [
72-
__DIR__ . '/data/used-symbols/relative-namespace.php',
72+
__DIR__ . '/data/not-autoloaded/used-symbols/relative-namespace.php',
7373
[
7474
'DateTimeImmutable' => [10],
7575
]
7676
];
7777

7878
yield 'global namespace' => [
79-
__DIR__ . '/data/used-symbols/global-namespace.php',
79+
__DIR__ . '/data/not-autoloaded/used-symbols/global-namespace.php',
8080
[
8181
'DateTimeImmutable' => [3],
8282
]

tests/data/autoloadable-no-classmap/usage.php

-3
This file was deleted.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)