Skip to content

Commit ea49c59

Browse files
authored
IntegrationTest: ensure result cache is working (#36)
1 parent 5df79b9 commit ea49c59

File tree

7 files changed

+145
-59
lines changed

7 files changed

+145
-59
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ parameters:
1515
- tests
1616
excludePaths:
1717
analyseAndScan:
18+
- tests/Integration/src
1819
- tests/*/data/*
1920
tmpDir: cache/phpstan/
2021
checkMissingCallableSignature: true

tests/BinTestCase.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonk\PHPStan\Baseline;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use function fclose;
7+
use function proc_close;
8+
use function proc_open;
9+
use function stream_get_contents;
10+
11+
abstract class BinTestCase extends TestCase
12+
{
13+
14+
protected function runCommand(
15+
string $command,
16+
string $cwd,
17+
int $expectedExitCode,
18+
?string $expectedOutputContains = null,
19+
?string $expectedErrorContains = null
20+
): void
21+
{
22+
$desc = [
23+
['pipe', 'r'],
24+
['pipe', 'w'],
25+
['pipe', 'w'],
26+
];
27+
28+
$procHandle = proc_open($command, $desc, $pipes, $cwd);
29+
self::assertNotFalse($procHandle);
30+
31+
/** @var list<resource> $pipes */
32+
$output = stream_get_contents($pipes[1]); // @phpstan-ignore offsetAccess.notFound
33+
$errorOutput = stream_get_contents($pipes[2]); // @phpstan-ignore offsetAccess.notFound
34+
self::assertNotFalse($output);
35+
self::assertNotFalse($errorOutput);
36+
37+
foreach ($pipes as $pipe) {
38+
fclose($pipe);
39+
}
40+
41+
$extraInfo = "Output was:\n" . $output . "\nError was:\n" . $errorOutput . "\n";
42+
43+
if ($expectedOutputContains !== null) {
44+
self::assertStringContainsString(
45+
$expectedOutputContains,
46+
$output,
47+
$extraInfo,
48+
);
49+
}
50+
51+
if ($expectedErrorContains !== null) {
52+
self::assertStringContainsString(
53+
$expectedErrorContains,
54+
$errorOutput,
55+
$extraInfo,
56+
);
57+
}
58+
59+
$exitCode = proc_close($procHandle);
60+
self::assertSame(
61+
$expectedExitCode,
62+
$exitCode,
63+
$extraInfo,
64+
);
65+
}
66+
67+
}

tests/Integration/IntegrationTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

tests/Integration/neon.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: max
3+
tmpDir: ../../cache/integration-test/phpstan-cache-neon
4+
paths:
5+
- src
6+
7+
includes:
8+
- ../../cache/integration-test/baselines/loader.neon

tests/Integration/php.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: max
3+
tmpDir: ../../cache/integration-test/phpstan-cache-php
4+
paths:
5+
- src
6+
7+
includes:
8+
- ../../cache/integration-test/baselines/loader.php

tests/Integration/src/dummy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php declare(strict_types = 1);
2+
3+
(new DateTime())->invalid();

tests/SplitterTest.php

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
namespace ShipMonk\PHPStan\Baseline;
44

55
use Nette\Neon\Neon;
6-
use PHPUnit\Framework\TestCase;
7-
use function fclose;
86
use function file_put_contents;
97
use function mkdir;
10-
use function proc_close;
11-
use function proc_open;
12-
use function stream_get_contents;
138
use function sys_get_temp_dir;
149
use function uniqid;
1510
use function var_export;
1611

17-
final class SplitterTest extends TestCase
12+
final class SplitterTest extends BinTestCase
1813
{
1914

2015
public function testBinaryWithNeon(): void
@@ -104,57 +99,4 @@ private function getSampleErrors(): array
10499
];
105100
}
106101

107-
private function runCommand(
108-
string $command,
109-
string $cwd,
110-
int $expectedExitCode,
111-
?string $expectedOutputContains = null,
112-
?string $expectedErrorContains = null
113-
): void
114-
{
115-
$desc = [
116-
['pipe', 'r'],
117-
['pipe', 'w'],
118-
['pipe', 'w'],
119-
];
120-
121-
$procHandle = proc_open($command, $desc, $pipes, $cwd);
122-
self::assertNotFalse($procHandle);
123-
124-
/** @var list<resource> $pipes */
125-
$output = stream_get_contents($pipes[1]); // @phpstan-ignore offsetAccess.notFound
126-
$errorOutput = stream_get_contents($pipes[2]); // @phpstan-ignore offsetAccess.notFound
127-
self::assertNotFalse($output);
128-
self::assertNotFalse($errorOutput);
129-
130-
foreach ($pipes as $pipe) {
131-
fclose($pipe);
132-
}
133-
134-
$extraInfo = "Output was:\n" . $output . "\nError was:\n" . $errorOutput . "\n";
135-
136-
$exitCode = proc_close($procHandle);
137-
self::assertSame(
138-
$expectedExitCode,
139-
$exitCode,
140-
$extraInfo,
141-
);
142-
143-
if ($expectedOutputContains !== null) {
144-
self::assertStringContainsString(
145-
$expectedOutputContains,
146-
$output,
147-
$extraInfo,
148-
);
149-
}
150-
151-
if ($expectedErrorContains !== null) {
152-
self::assertStringContainsString(
153-
$expectedErrorContains,
154-
$errorOutput,
155-
$extraInfo,
156-
);
157-
}
158-
}
159-
160102
}

0 commit comments

Comments
 (0)