|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\Baseline\Integration; |
| 4 | + |
| 5 | +use ShipMonk\PHPStan\Baseline\BinTestCase; |
| 6 | +use function array_map; |
| 7 | +use function file_put_contents; |
| 8 | +use function glob; |
| 9 | +use function is_dir; |
| 10 | +use function mkdir; |
| 11 | + |
| 12 | +final class IntegrationTest extends BinTestCase |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * @dataProvider provideExtension |
| 17 | + */ |
| 18 | + public function testResultCache(string $extension): void |
| 19 | + { |
| 20 | + $emptyConfig = $extension === 'php' ? '<?php return [];' : ''; |
| 21 | + $baselinesDir = 'cache/integration-test/baselines'; |
| 22 | + $baselinesDirAbs = __DIR__ . '/../../' . $baselinesDir; |
| 23 | + |
| 24 | + if (!is_dir($baselinesDirAbs)) { |
| 25 | + mkdir($baselinesDirAbs, 0777, true); |
| 26 | + } |
| 27 | + |
| 28 | + array_map('unlink', glob($baselinesDirAbs . '/*')); // @phpstan-ignore argument.type |
| 29 | + |
| 30 | + // ensure dummy loader is present |
| 31 | + file_put_contents($baselinesDirAbs . "/loader.$extension", $emptyConfig); |
| 32 | + |
| 33 | + $cwd = __DIR__; |
| 34 | + $phpstan = '../../vendor/bin/phpstan'; |
| 35 | + $split = '../../bin/split-phpstan-baseline'; |
| 36 | + |
| 37 | + $this->runCommand("$phpstan clear-result-cache -c $extension.neon", $cwd, 0); |
| 38 | + $this->runCommand("$phpstan analyse -vv -c $extension.neon --generate-baseline=../../$baselinesDir/loader.$extension", $cwd, 0, null, 'Result cache is saved.'); |
| 39 | + $this->runCommand("php $split ../../$baselinesDir/loader.$extension", $cwd, 0, 'Writing baseline file'); |
| 40 | + $this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 0, null, 'Result cache restored. 0 files will be reanalysed.'); |
| 41 | + |
| 42 | + // cache should invalidate by editing the baseline |
| 43 | + file_put_contents($baselinesDirAbs . "/method.notFound.$extension", $emptyConfig); |
| 44 | + |
| 45 | + $this->runCommand("$phpstan analyse -vv -c $extension.neon", $cwd, 1, 'Call to an undefined method DateTime::invalid()'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return iterable<array{string}> |
| 50 | + */ |
| 51 | + public function provideExtension(): iterable |
| 52 | + { |
| 53 | + yield 'Neon' => ['neon']; |
| 54 | + yield 'PHP' => ['php']; |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments