2019-06-16 21:56:00 +02:00
|
|
|
<?php
|
2020-09-26 19:31:38 +02:00
|
|
|
|
2019-06-16 21:56:00 +02:00
|
|
|
namespace Psalm\Tests\EndToEnd;
|
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
use Exception;
|
2020-09-26 19:31:38 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function closedir;
|
|
|
|
use function copy;
|
2021-12-03 21:07:25 +01:00
|
|
|
use function file_exists;
|
2020-09-26 19:31:38 +02:00
|
|
|
use function file_get_contents;
|
|
|
|
use function file_put_contents;
|
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;
|
2020-09-26 19:31:38 +02:00
|
|
|
use function preg_replace;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function readdir;
|
|
|
|
use function rmdir;
|
2021-12-03 21:07:25 +01:00
|
|
|
use function str_replace;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function sys_get_temp_dir;
|
|
|
|
use function tempnam;
|
|
|
|
use function unlink;
|
2020-09-26 19:31:38 +02:00
|
|
|
|
2020-08-23 16:32:07 +02:00
|
|
|
use const PHP_VERSION_ID;
|
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
|
|
|
|
{
|
2019-11-30 06:09:07 +01:00
|
|
|
use PsalmRunnerTrait;
|
2019-06-16 21:56:00 +02:00
|
|
|
|
|
|
|
/** @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)) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new Exception('Couldn\'t get working directory');
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-01-02 06:06:16 +01:00
|
|
|
$process = new Process(['composer', 'install', '--no-plugins'], self::$tmpDir, null, null, 120);
|
|
|
|
$process->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');
|
2020-09-26 19:31:38 +02:00
|
|
|
copy(
|
|
|
|
__DIR__ . '/../fixtures/DummyProjectWithErrors/src/FileWithErrors.php',
|
|
|
|
self::$tmpDir . '/src/FileWithErrors.php'
|
|
|
|
);
|
2019-06-16 21:56:00 +02:00
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:09:38 +02:00
|
|
|
public function tearDown(): void
|
|
|
|
{
|
2021-12-03 21:07:25 +01:00
|
|
|
if (file_exists(self::$tmpDir . '/cache')) {
|
2020-06-05 18:09:38 +02:00
|
|
|
self::recursiveRemoveDirectory(self::$tmpDir . '/cache');
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:56:00 +02:00
|
|
|
public function testHelpReturnsMessage(): void
|
|
|
|
{
|
2019-11-30 06:09:07 +01:00
|
|
|
$this->assertStringContainsString('Usage:', $this->runPsalm(['--help'], self::$tmpDir)['STDOUT']);
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInit(): void
|
|
|
|
{
|
2020-09-26 19:31:38 +02:00
|
|
|
$this->assertStringStartsWith(
|
|
|
|
'Calculating best config level based on project files',
|
|
|
|
$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-11-30 06:09:07 +01:00
|
|
|
$this->runPsalm(['--alter', '--issues=all'], self::$tmpDir, false, true)['STDOUT']
|
2019-06-16 21:56:00 +02:00
|
|
|
);
|
|
|
|
|
2019-11-30 06:09:07 +01:00
|
|
|
$this->assertSame(0, $this->runPsalm([], self::$tmpDir)['CODE']);
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-30 06:09:07 +01:00
|
|
|
$this->assertSame(0, $this->runPsalm([], self::$tmpDir)['CODE']);
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPsalm(): void
|
|
|
|
{
|
2020-02-17 22:33:28 +01:00
|
|
|
$this->runPsalmInit(1);
|
2019-11-30 06:09:07 +01:00
|
|
|
$result = $this->runPsalm([], self::$tmpDir, true);
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->assertStringContainsString(
|
|
|
|
'Target PHP version: 7.1 (inferred from composer.json)',
|
|
|
|
$result['STDERR']
|
|
|
|
);
|
2021-03-23 00:37:49 +01:00
|
|
|
$this->assertStringContainsString('UnusedParam', $result['STDOUT']);
|
2019-06-16 21:56:00 +02:00
|
|
|
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
|
2021-03-23 00:37:49 +01:00
|
|
|
$this->assertStringContainsString('3 errors', $result['STDOUT']);
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $result['CODE']);
|
2019-06-16 21:56:00 +02:00
|
|
|
}
|
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
public function testPsalmWithPHPVersionOverride(): void
|
|
|
|
{
|
|
|
|
$this->runPsalmInit(1);
|
|
|
|
$result = $this->runPsalm(['--php-version=8.0'], self::$tmpDir, true);
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
'Target PHP version: 8.0 (set by CLI argument)',
|
|
|
|
$result['STDERR']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPsalmWithPHPVersionFromConfig(): void
|
|
|
|
{
|
|
|
|
$this->runPsalmInit(1, '7.4');
|
|
|
|
$result = $this->runPsalm([], self::$tmpDir, true);
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
'Target PHP version: 7.4 (set by config file)',
|
|
|
|
$result['STDERR']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:09:38 +02:00
|
|
|
public function testPsalmDiff(): void
|
|
|
|
{
|
2020-08-23 16:32:07 +02:00
|
|
|
if (PHP_VERSION_ID < 70400) {
|
|
|
|
$this->markTestSkipped('Only works on 7.4+');
|
|
|
|
}
|
2020-06-06 16:34:49 +02:00
|
|
|
|
2020-06-06 17:23:05 +02:00
|
|
|
copy(__DIR__ . '/../fixtures/DummyProjectWithErrors/diff_composer.lock', self::$tmpDir . '/composer.lock');
|
2020-06-06 16:34:49 +02:00
|
|
|
|
2020-06-05 18:09:38 +02:00
|
|
|
$this->runPsalmInit(1);
|
|
|
|
$result = $this->runPsalm(['--diff', '-m'], self::$tmpDir, true);
|
2021-03-23 00:37:49 +01:00
|
|
|
$this->assertStringContainsString('UnusedParam', $result['STDOUT']);
|
2020-06-05 18:09:38 +02:00
|
|
|
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
|
2021-03-23 00:37:49 +01:00
|
|
|
$this->assertStringContainsString('3 errors', $result['STDOUT']);
|
2020-06-05 18:09:38 +02:00
|
|
|
$this->assertStringContainsString('E', $result['STDERR']);
|
|
|
|
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $result['CODE']);
|
2020-06-05 18:09:38 +02:00
|
|
|
|
|
|
|
$result = $this->runPsalm(['--diff', '-m'], self::$tmpDir, true);
|
|
|
|
|
2021-03-23 00:37:49 +01:00
|
|
|
$this->assertStringContainsString('UnusedParam', $result['STDOUT']);
|
2020-06-05 18:09:38 +02:00
|
|
|
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
|
2021-03-23 00:37:49 +01:00
|
|
|
$this->assertStringContainsString('3 errors', $result['STDOUT']);
|
2020-06-05 18:09:38 +02:00
|
|
|
$this->assertStringNotContainsString('E', $result['STDERR']);
|
|
|
|
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $result['CODE']);
|
2020-06-06 16:34:49 +02:00
|
|
|
|
|
|
|
@unlink(self::$tmpDir . '/composer.lock');
|
2020-06-05 18:09:38 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:31:38 +02:00
|
|
|
public function testTainting(): void
|
|
|
|
{
|
|
|
|
$this->runPsalmInit(1);
|
|
|
|
$result = $this->runPsalm(['--taint-analysis'], self::$tmpDir, true);
|
|
|
|
|
2020-11-17 18:44:31 +01:00
|
|
|
$this->assertStringContainsString('TaintedHtml', $result['STDOUT']);
|
2021-06-11 00:07:41 +02:00
|
|
|
$this->assertStringContainsString('TaintedTextWithQuotes', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('2 errors', $result['STDOUT']);
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $result['CODE']);
|
2020-09-26 19:31:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 01:02:44 +01:00
|
|
|
public function testTaintingWithoutInit(): void
|
|
|
|
{
|
2020-11-21 01:07:48 +01:00
|
|
|
$result = $this->runPsalm(['--taint-analysis'], self::$tmpDir, true, false);
|
2020-11-21 01:02:44 +01:00
|
|
|
|
|
|
|
$this->assertStringContainsString('TaintedHtml', $result['STDOUT']);
|
2021-06-11 00:07:41 +02:00
|
|
|
$this->assertStringContainsString('TaintedTextWithQuotes', $result['STDOUT']);
|
|
|
|
$this->assertStringContainsString('2 errors', $result['STDOUT']);
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $result['CODE']);
|
2020-11-21 01:02:44 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 22:04:15 +01:00
|
|
|
public function testTaintGraphDumping(): void
|
|
|
|
{
|
|
|
|
$this->runPsalmInit(1);
|
|
|
|
$result = $this->runPsalm(
|
|
|
|
[
|
|
|
|
'--taint-analysis',
|
|
|
|
'--dump-taint-graph='.self::$tmpDir.'/taints.dot',
|
|
|
|
],
|
|
|
|
self::$tmpDir,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $result['CODE']);
|
2021-01-22 22:04:15 +01:00
|
|
|
$this->assertFileEquals(
|
|
|
|
__DIR__ . '/../fixtures/expected_taint_graph.dot',
|
|
|
|
self::$tmpDir.'/taints.dot'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-06 18:21:39 +02:00
|
|
|
public function testLegacyConfigWithoutresolveFromConfigFile(): void
|
|
|
|
{
|
2020-02-17 22:33:28 +01:00
|
|
|
$this->runPsalmInit(1);
|
2019-07-06 18:21:39 +02:00
|
|
|
$psalmXmlContent = file_get_contents(self::$tmpDir . '/psalm.xml');
|
|
|
|
$count = 0;
|
2020-10-05 16:14:39 +02:00
|
|
|
$psalmXmlContent = preg_replace('/resolveFromConfigFile="true"/', 'resolveFromConfigFile="false"', $psalmXmlContent, -1, $count);
|
2019-07-06 18:21:39 +02:00
|
|
|
$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();
|
2021-01-24 19:30:35 +01:00
|
|
|
$this->assertSame(2, $process->getExitCode());
|
2019-07-06 18:21:39 +02:00
|
|
|
$this->assertStringContainsString('InvalidReturnType', $process->getOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array{STDOUT: string, STDERR: string, CODE: int|null}
|
|
|
|
*/
|
2021-11-27 01:06:33 +01:00
|
|
|
private function runPsalmInit(?int $level = null, ?string $php_version = null): array
|
2019-07-06 18:21:39 +02:00
|
|
|
{
|
2020-02-17 22:33:28 +01:00
|
|
|
$args = ['--init'];
|
|
|
|
|
|
|
|
if ($level) {
|
|
|
|
$args[] = 'src';
|
|
|
|
$args[] = (string) $level;
|
|
|
|
}
|
2020-06-05 18:09:38 +02:00
|
|
|
|
|
|
|
$ret = $this->runPsalm($args, self::$tmpDir, false, false);
|
|
|
|
|
|
|
|
$psalm_config_contents = file_get_contents(self::$tmpDir . '/psalm.xml');
|
2021-12-03 21:07:25 +01:00
|
|
|
$psalm_config_contents = str_replace(
|
2020-06-05 18:09:38 +02:00
|
|
|
'errorLevel="1"',
|
2021-11-27 01:06:33 +01:00
|
|
|
'errorLevel="1" '
|
|
|
|
. 'cacheDirectory="' . self::$tmpDir . '/cache" '
|
|
|
|
. ($php_version ? ('phpVersion="' . $php_version . '"') : ''),
|
2020-06-05 18:09:38 +02:00
|
|
|
$psalm_config_contents
|
|
|
|
);
|
|
|
|
file_put_contents(self::$tmpDir . '/psalm.xml', $psalm_config_contents);
|
|
|
|
|
|
|
|
return $ret;
|
2019-07-06 18:21:39 +02:00
|
|
|
}
|
|
|
|
|
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))) {
|
2020-09-20 00:26:51 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|