2019-06-16 21:56:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests\EndToEnd;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_merge;
|
2019-06-16 21:56:00 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function closedir;
|
|
|
|
use function copy;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function getcwd;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function is_dir;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function is_string;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function mkdir;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function opendir;
|
2019-07-05 22:24:00 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function readdir;
|
|
|
|
use function rmdir;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
use function sys_get_temp_dir;
|
|
|
|
use function tempnam;
|
|
|
|
use function unlink;
|
2019-07-06 18:21:39 +02:00
|
|
|
use function file_get_contents;
|
|
|
|
use function file_put_contents;
|
|
|
|
use function preg_replace;
|
2019-06-16 21:56:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests some of the most important use cases of the psalm and psalter commands, by launching a new
|
|
|
|
* process as if invoked by a real user.
|
|
|
|
*
|
|
|
|
* This is primarily intended to test the code in `psalm`, `src/psalm.php` and related files.
|
|
|
|
*/
|
|
|
|
class PsalmEndToEndTest extends TestCase
|
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
private $psalm = __DIR__ . '/../../psalm';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $psalter = __DIR__ . '/../../psalter';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private static $tmpDir;
|
|
|
|
|
|
|
|
public static function setUpBeforeClass(): void
|
|
|
|
{
|
|
|
|
self::$tmpDir = tempnam(sys_get_temp_dir(), 'PsalmEndToEndTest_');
|
|
|
|
unlink(self::$tmpDir);
|
|
|
|
mkdir(self::$tmpDir);
|
|
|
|
|
|
|
|
$getcwd = getcwd();
|
2019-07-05 22:24:00 +02:00
|
|
|
if (!is_string($getcwd)) {
|
2019-06-16 21:56:00 +02:00
|
|
|
throw new \Exception('Couldn\'t get working directory');
|
|
|
|
}
|
|
|
|
|
2019-06-26 02:38:44 +02:00
|
|
|
mkdir(self::$tmpDir . '/src');
|
2019-06-16 21:56:00 +02:00
|
|
|
|
2019-06-26 02:38:44 +02:00
|
|
|
copy(__DIR__ . '/../fixtures/DummyProjectWithErrors/composer.json', self::$tmpDir . '/composer.json');
|
2019-06-16 21:56:00 +02:00
|
|
|
|
2019-10-02 02:26:55 +02:00
|
|
|
(new Process(['composer', 'install', '--no-plugins'], self::$tmpDir))->mustRun();
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass(): void
|
|
|
|
{
|
|
|
|
self::recursiveRemoveDirectory(self::$tmpDir);
|
|
|
|
parent::tearDownAfterClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
2019-06-26 02:55:30 +02:00
|
|
|
@unlink(self::$tmpDir . '/psalm.xml');
|
|
|
|
copy(__DIR__ . '/../fixtures/DummyProjectWithErrors/src/FileWithErrors.php', self::$tmpDir . '/src/FileWithErrors.php');
|
2019-06-16 21:56:00 +02:00
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHelpReturnsMessage(): void
|
|
|
|
{
|
|
|
|
$this->assertStringContainsString('Usage:', $this->runPsalm(['--help'])['STDOUT']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testVersion(): void
|
|
|
|
{
|
2019-07-06 18:21:39 +02:00
|
|
|
$this->assertStringStartsWith('Psalm 3', $this->runPsalm(['--version'], false, false)['STDOUT']);
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInit(): void
|
|
|
|
{
|
2019-07-06 18:21:39 +02:00
|
|
|
$this->assertStringStartsWith('Config file created', $this->runPsalmInit()['STDOUT']);
|
2019-06-26 02:55:30 +02:00
|
|
|
$this->assertFileExists(self::$tmpDir . '/psalm.xml');
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAlter(): void
|
|
|
|
{
|
2019-07-06 18:21:39 +02:00
|
|
|
$this->runPsalmInit();
|
2019-06-16 21:56:00 +02:00
|
|
|
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
'No errors found!',
|
2019-07-07 14:55:53 +02:00
|
|
|
$this->runPsalm(['--alter', '--issues=all'], false, true)['STDOUT']
|
2019-06-16 21:56:00 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(0, $this->runPsalm([])['CODE']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPsalter(): void
|
|
|
|
{
|
2019-07-06 18:21:39 +02:00
|
|
|
$this->runPsalmInit();
|
2019-07-07 14:55:53 +02:00
|
|
|
(new Process(['php', $this->psalter, '--alter', '--issues=InvalidReturnType'], self::$tmpDir))->mustRun();
|
2019-06-16 21:56:00 +02:00
|
|
|
$this->assertSame(0, $this->runPsalm([])['CODE']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPsalm(): void
|
|
|
|
{
|
2019-07-06 18:21:39 +02:00
|
|
|
$this->runPsalmInit();
|
2019-06-16 21:56:00 +02:00
|
|
|
$result = $this->runPsalm([], true);
|
|
|
|
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('2 errors', $result['STDOUT']);
|
|
|
|
$this->assertSame(1, $result['CODE']);
|
|
|
|
}
|
|
|
|
|
2019-07-06 18:21:39 +02:00
|
|
|
public function testLegacyConfigWithoutresolveFromConfigFile(): void
|
|
|
|
{
|
|
|
|
$this->runPsalmInit();
|
|
|
|
$psalmXmlContent = file_get_contents(self::$tmpDir . '/psalm.xml');
|
|
|
|
$count = 0;
|
|
|
|
$psalmXmlContent = preg_replace('/resolveFromConfigFile="true"/', '', $psalmXmlContent, -1, $count);
|
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
|
|
|
|
file_put_contents(self::$tmpDir . '/src/psalm.xml', $psalmXmlContent);
|
|
|
|
|
2019-07-07 14:55:53 +02:00
|
|
|
$process = new Process(['php', $this->psalm, '--config=src/psalm.xml'], self::$tmpDir);
|
2019-07-06 18:21:39 +02:00
|
|
|
$process->run();
|
|
|
|
$this->assertSame(1, $process->getExitCode());
|
|
|
|
$this->assertStringContainsString('InvalidReturnType', $process->getOutput());
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:56:00 +02:00
|
|
|
/**
|
|
|
|
* @param array<string> $args
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-06-16 21:56:00 +02:00
|
|
|
* @return array{STDOUT: string, STDERR: string, CODE: int|null}
|
|
|
|
*/
|
2019-07-06 18:21:39 +02:00
|
|
|
private function runPsalm(array $args, bool $shouldFail = false, bool $relyOnConfigDir = true): array
|
2019-06-16 21:56:00 +02:00
|
|
|
{
|
2019-07-06 18:21:39 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-07-07 14:55:53 +02:00
|
|
|
// Windows doesn't read shabangs, so to allow this to work on windows we run `php psalm` rather than just `psalm`.
|
|
|
|
|
2019-07-06 18:21:39 +02:00
|
|
|
if ($relyOnConfigDir) {
|
2019-07-07 14:55:53 +02:00
|
|
|
$process = new Process(array_merge(['php', $this->psalm, '-c=' . self::$tmpDir . '/psalm.xml'], $args), null);
|
2019-07-06 18:21:39 +02:00
|
|
|
} else {
|
2019-07-07 14:55:53 +02:00
|
|
|
$process = new Process(array_merge(['php', $this->psalm], $args), self::$tmpDir);
|
2019-07-06 18:21:39 +02:00
|
|
|
}
|
2019-06-16 21:56:00 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
if (!$shouldFail) {
|
2019-06-16 21:56:00 +02:00
|
|
|
$process->mustRun();
|
|
|
|
} else {
|
|
|
|
$process->run();
|
|
|
|
$this->assertGreaterThan(0, $process->getExitCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'STDOUT' => $process->getOutput(),
|
|
|
|
'STDERR' => $process->getErrorOutput(),
|
2019-07-05 22:24:00 +02:00
|
|
|
'CODE' => $process->getExitCode(),
|
2019-06-16 21:56:00 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-07-06 18:21:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array{STDOUT: string, STDERR: string, CODE: int|null}
|
|
|
|
*/
|
|
|
|
private function runPsalmInit(): array
|
|
|
|
{
|
|
|
|
return $this->runPsalm(['--init'], false, false);
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:56:00 +02:00
|
|
|
/** from comment by itay at itgoldman dot com at
|
|
|
|
* https://www.php.net/manual/en/function.rmdir.php#117354
|
|
|
|
*/
|
|
|
|
private static function recursiveRemoveDirectory(string $src): void
|
|
|
|
{
|
|
|
|
$dir = opendir($src);
|
2019-07-05 22:24:00 +02:00
|
|
|
while (false !== ($file = readdir($dir))) {
|
|
|
|
if (($file != '.') && ($file != '..')) {
|
2019-06-16 21:56:00 +02:00
|
|
|
$full = $src . '/' . $file;
|
|
|
|
if (is_dir($full)) {
|
|
|
|
self::recursiveRemoveDirectory($full);
|
|
|
|
} else {
|
|
|
|
unlink($full);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($dir);
|
|
|
|
rmdir($src);
|
|
|
|
}
|
|
|
|
}
|