diff --git a/tests/EndToEnd/PsalmEndToEndTest.php b/tests/EndToEnd/PsalmEndToEndTest.php index 02a5939d9..c2fca4d8a 100644 --- a/tests/EndToEnd/PsalmEndToEndTest.php +++ b/tests/EndToEnd/PsalmEndToEndTest.php @@ -29,11 +29,7 @@ use function preg_replace; */ class PsalmEndToEndTest extends TestCase { - /** @var string */ - private $psalm = __DIR__ . '/../../psalm'; - - /** @var string */ - private $psalter = __DIR__ . '/../../psalter'; + use PsalmRunnerTrait; /** @var string */ private static $tmpDir; @@ -71,12 +67,12 @@ class PsalmEndToEndTest extends TestCase public function testHelpReturnsMessage(): void { - $this->assertStringContainsString('Usage:', $this->runPsalm(['--help'])['STDOUT']); + $this->assertStringContainsString('Usage:', $this->runPsalm(['--help'], self::$tmpDir)['STDOUT']); } public function testVersion(): void { - $this->assertStringStartsWith('Psalm 3', $this->runPsalm(['--version'], false, false)['STDOUT']); + $this->assertStringStartsWith('Psalm 3', $this->runPsalm(['--version'], self::$tmpDir, false, false)['STDOUT']); } public function testInit(): void @@ -91,23 +87,23 @@ class PsalmEndToEndTest extends TestCase $this->assertStringContainsString( 'No errors found!', - $this->runPsalm(['--alter', '--issues=all'], false, true)['STDOUT'] + $this->runPsalm(['--alter', '--issues=all'], self::$tmpDir, false, true)['STDOUT'] ); - $this->assertSame(0, $this->runPsalm([])['CODE']); + $this->assertSame(0, $this->runPsalm([], self::$tmpDir)['CODE']); } public function testPsalter(): void { $this->runPsalmInit(); (new Process(['php', $this->psalter, '--alter', '--issues=InvalidReturnType'], self::$tmpDir))->mustRun(); - $this->assertSame(0, $this->runPsalm([])['CODE']); + $this->assertSame(0, $this->runPsalm([], self::$tmpDir)['CODE']); } public function testPsalm(): void { $this->runPsalmInit(); - $result = $this->runPsalm([], true); + $result = $this->runPsalm([], self::$tmpDir, true); $this->assertStringContainsString('InvalidReturnType', $result['STDOUT']); $this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']); $this->assertStringContainsString('2 errors', $result['STDOUT']); @@ -130,45 +126,12 @@ class PsalmEndToEndTest extends TestCase $this->assertStringContainsString('InvalidReturnType', $process->getOutput()); } - /** - * @param array $args - * - * @return array{STDOUT: string, STDERR: string, CODE: int|null} - */ - private function runPsalm(array $args, bool $shouldFail = false, bool $relyOnConfigDir = true): array - { - // As config files all contain `resolveFromConfigFile="true"` Psalm shouldn't need to be run from the same - // directory that the code being analysed exists in. - - // Windows doesn't read shabangs, so to allow this to work on windows we run `php psalm` rather than just `psalm`. - - if ($relyOnConfigDir) { - $process = new Process(array_merge(['php', $this->psalm, '-c=' . self::$tmpDir . '/psalm.xml'], $args), null); - } else { - $process = new Process(array_merge(['php', $this->psalm], $args), self::$tmpDir); - } - - if (!$shouldFail) { - $process->mustRun(); - } else { - $process->run(); - $this->assertGreaterThan(0, $process->getExitCode()); - } - - return [ - 'STDOUT' => $process->getOutput(), - 'STDERR' => $process->getErrorOutput(), - 'CODE' => $process->getExitCode(), - ]; - } - - /** * @return array{STDOUT: string, STDERR: string, CODE: int|null} */ private function runPsalmInit(): array { - return $this->runPsalm(['--init'], false, false); + return $this->runPsalm(['--init'], self::$tmpDir, false, false); } /** from comment by itay at itgoldman dot com at diff --git a/tests/EndToEnd/PsalmRunnerTrait.php b/tests/EndToEnd/PsalmRunnerTrait.php new file mode 100644 index 000000000..e20cfa9b6 --- /dev/null +++ b/tests/EndToEnd/PsalmRunnerTrait.php @@ -0,0 +1,54 @@ + $args + * + * @return array{STDOUT: string, STDERR: string, CODE: int|null} + */ + private function runPsalm( + array $args, + string $workingDir, + bool $shouldFail = false, + bool $relyOnConfigDir = true + ): array { + // As config files all contain `resolveFromConfigFile="true"` Psalm + // shouldn't need to be run from the same directory that the code being + // analysed exists in. + + // Windows doesn't read shabangs, so to allow this to work on windows + // we run `php psalm` rather than just `psalm`. + + if ($relyOnConfigDir) { + $process = new Process(array_merge(['php', $this->psalm, '-c=' . $workingDir . '/psalm.xml'], $args), null); + } else { + $process = new Process(array_merge(['php', $this->psalm], $args), $workingDir); + } + + if (!$shouldFail) { + $process->mustRun(); + } else { + $process->run(); + $this->assertGreaterThan(0, $process->getExitCode()); + } + + return [ + 'STDOUT' => $process->getOutput(), + 'STDERR' => $process->getErrorOutput(), + 'CODE' => $process->getExitCode(), + ]; + } +} diff --git a/tests/EndToEnd/SuicidalAutoloaderTest.php b/tests/EndToEnd/SuicidalAutoloaderTest.php new file mode 100644 index 000000000..06affdf2e --- /dev/null +++ b/tests/EndToEnd/SuicidalAutoloaderTest.php @@ -0,0 +1,15 @@ +runPsalm([], __DIR__ . '/' . '../fixtures/SuicidalAutoloader/'); + } +} diff --git a/tests/fixtures/SuicidalAutoloader/autoloader.php b/tests/fixtures/SuicidalAutoloader/autoloader.php new file mode 100644 index 000000000..353c3a45f --- /dev/null +++ b/tests/fixtures/SuicidalAutoloader/autoloader.php @@ -0,0 +1,6 @@ +__toString() . "\n\n" . $ex->getTraceAsString() . "\n\n"; + exit(70); +}); diff --git a/tests/fixtures/SuicidalAutoloader/file.php b/tests/fixtures/SuicidalAutoloader/file.php new file mode 100644 index 000000000..b3d9bbc7f --- /dev/null +++ b/tests/fixtures/SuicidalAutoloader/file.php @@ -0,0 +1 @@ + + + + + +