Skip to content

Commit c530f2f

Browse files
xificurkclaude
andcommitted
Replace StubFilesExtensionLoaderTest with ResultCacheNoReflectionTest
Test that BetterReflection is not initialized when ResultCacheManager constructs the ResultCache, which is the real-world code path that exercises StubFilesExtensionLoader::getFiles(). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e668664 commit c530f2f

4 files changed

Lines changed: 46 additions & 30 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PHPStan\Analyser\ResultCache\ResultCacheManagerFactory;
6+
use PHPStan\BetterReflection\Identifier\Identifier;
7+
use PHPStan\BetterReflection\Identifier\IdentifierType;
8+
use PHPStan\BetterReflection\Reflector\Reflector;
9+
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
10+
use PHPStan\Command\Output;
11+
use PHPStan\Testing\PHPStanTestCase;
12+
use PHPUnit\Framework\AssertionFailedError;
13+
14+
final class ResultCacheNoReflectionTest extends PHPStanTestCase
15+
{
16+
17+
public function testRestoreDoesNotUseSourceLocator(): void
18+
{
19+
$factory = self::getContainer()->getByType(ResultCacheManagerFactory::class);
20+
$manager = $factory->create([]);
21+
$manager->restore([], true, false, null, $this->createMock(Output::class));
22+
23+
// Sanity check: verify ThrowingSourceLocator is actually wired up.
24+
// If restore() had triggered the source locator, the test would have already failed above.
25+
$this->expectException(AssertionFailedError::class);
26+
$this->expectExceptionMessage('must not be called during result cache construction');
27+
$sourceLocator = self::getContainer()->getByType(SourceLocator::class);
28+
$sourceLocator->locateIdentifier(
29+
$this->createMock(Reflector::class),
30+
new Identifier('stdClass', new IdentifierType()),
31+
);
32+
}
33+
34+
public static function getAdditionalConfigFiles(): array
35+
{
36+
return [
37+
__DIR__ . '/../../extension.neon',
38+
__DIR__ . '/result-cache-no-reflection-test.neon',
39+
];
40+
}
41+
42+
}

tests/Stubs/Symfony/ThrowingSourceLocator.php renamed to tests/Analyser/ThrowingSourceLocator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types = 1);
22

3-
namespace PHPStan\Stubs\Symfony;
3+
namespace PHPStan\Analyser;
44

55
use PHPStan\BetterReflection\Identifier\Identifier;
66
use PHPStan\BetterReflection\Identifier\IdentifierType;
@@ -14,12 +14,12 @@ final class ThrowingSourceLocator implements SourceLocator
1414

1515
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
1616
{
17-
Assert::fail('SourceLocator::locateIdentifier must not be called during getFiles()');
17+
Assert::fail('SourceLocator::locateIdentifier must not be called during result cache construction');
1818
}
1919

2020
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
2121
{
22-
Assert::fail('SourceLocator::locateIdentifiersByType must not be called during getFiles()');
22+
Assert::fail('SourceLocator::locateIdentifiersByType must not be called during result cache construction');
2323
}
2424

2525
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
services:
22
betterReflectionSourceLocator:
33
class: PHPStan\BetterReflection\SourceLocator\Type\SourceLocator
4-
factory: PHPStan\Stubs\Symfony\ThrowingSourceLocator
4+
factory: PHPStan\Analyser\ThrowingSourceLocator
55
autowired: false

tests/Stubs/Symfony/StubFilesExtensionLoaderTest.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)