1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00
psalm/tests/EndToEnd/PsalmEndToEndTest.php

233 lines
7.5 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\EndToEnd;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
2019-07-05 22:24:00 +02:00
use function closedir;
use function copy;
use function file_get_contents;
use function file_put_contents;
use function getcwd;
2019-07-05 22:24:00 +02:00
use function is_dir;
use function is_string;
2019-07-05 22:24:00 +02:00
use function mkdir;
use function opendir;
use function preg_replace;
use function readdir;
use function rmdir;
2019-07-05 22:24:00 +02:00
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
2020-08-23 16:32:07 +02:00
use const PHP_VERSION_ID;
/**
* 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
{
use PsalmRunnerTrait;
/** @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)) {
throw new \Exception('Couldn\'t get working directory');
}
mkdir(self::$tmpDir . '/src');
copy(__DIR__ . '/../fixtures/DummyProjectWithErrors/composer.json', self::$tmpDir . '/composer.json');
(new Process(['composer', 'install', '--no-plugins'], self::$tmpDir))->mustRun();
}
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'
);
parent::setUp();
}
public function tearDown(): void
{
if (\file_exists(self::$tmpDir . '/cache')) {
self::recursiveRemoveDirectory(self::$tmpDir . '/cache');
}
parent::tearDown();
}
public function testHelpReturnsMessage(): void
{
$this->assertStringContainsString('Usage:', $this->runPsalm(['--help'], self::$tmpDir)['STDOUT']);
}
public function testInit(): void
{
$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');
}
public function testAlter(): void
{
$this->runPsalmInit();
$this->assertStringContainsString(
'No errors found!',
$this->runPsalm(['--alter', '--issues=all'], self::$tmpDir, false, true)['STDOUT']
);
$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([], self::$tmpDir)['CODE']);
}
public function testPsalm(): void
{
2020-02-17 22:33:28 +01:00
$this->runPsalmInit(1);
$result = $this->runPsalm([], self::$tmpDir, true);
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
$this->assertStringContainsString('2 errors', $result['STDOUT']);
$this->assertSame(1, $result['CODE']);
}
public function testPsalmDiff(): void
{
Test parallelization (#4045) * Run tests in random order Being able to run tests in any order is a pre-requisite for being able to run them in parallel. * Reset type coverage between tests, fix affected tests * Reset parser and lexer between test runs and on php version change Previously lexer was reset, but parser kept the reference to the old one, and reference to the parser was kept by StatementsProvider. This resulted in order-dependent tests - if the parser was first initialized with phpVersion set to 7.4 then arrow functions worked fine, but were failing when the parser was initially constructed with settings for 7.3 This can be demonstrated on current master by upgrading to nikic/php-parser:4.9 and running: ``` vendor/bin/phpunit --no-coverage --filter="inferredArgArrowFunction" tests/ClosureTest.php ``` Now all tests using PHP 7.4 features must set the PHP version accordingly. * Marked more tests using 7.4 syntax * Reset newline-between-annotation flag between tests * Resolve real paths before passing them to checkPaths When checkPaths is called from psalm.php the paths are resolved, so we just mimicking SUT behaviour here. * Restore newline-between-annotations in DocCommentTest * Tweak Appveyor caches * Tweak TravisCI caches * Tweak CircleCI caches * Run tests in parallel Use `vendor/bin/paratest` instead of `vendor/bin/phpunit` * Use default paratest runner on Windows WrapperRunner is not supported on Windows. * TRAVIS_TAG could be empty * Restore appveyor conditional caching
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
$this->runPsalmInit(1);
$result = $this->runPsalm(['--diff', '-m'], self::$tmpDir, true);
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
$this->assertStringContainsString('2 errors', $result['STDOUT']);
$this->assertStringContainsString('E', $result['STDERR']);
$this->assertSame(1, $result['CODE']);
$result = $this->runPsalm(['--diff', '-m'], self::$tmpDir, true);
$this->assertStringContainsString('InvalidReturnType', $result['STDOUT']);
$this->assertStringContainsString('InvalidReturnStatement', $result['STDOUT']);
$this->assertStringContainsString('2 errors', $result['STDOUT']);
$this->assertStringNotContainsString('E', $result['STDERR']);
$this->assertSame(1, $result['CODE']);
2020-06-06 16:34:49 +02:00
@unlink(self::$tmpDir . '/composer.lock');
}
public function testTainting(): void
{
$this->runPsalmInit(1);
$result = $this->runPsalm(['--taint-analysis'], self::$tmpDir, true);
$this->assertStringContainsString('TaintedHtml', $result['STDOUT']);
$this->assertStringContainsString('1 errors', $result['STDOUT']);
$this->assertSame(1, $result['CODE']);
}
public function testTaintingWithoutInit(): void
{
2020-11-21 01:07:48 +01:00
$result = $this->runPsalm(['--taint-analysis'], self::$tmpDir, true, false);
$this->assertStringContainsString('TaintedHtml', $result['STDOUT']);
$this->assertStringContainsString('1 errors', $result['STDOUT']);
$this->assertSame(1, $result['CODE']);
}
public function testLegacyConfigWithoutresolveFromConfigFile(): void
{
2020-02-17 22:33:28 +01:00
$this->runPsalmInit(1);
$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);
$this->assertEquals(1, $count);
file_put_contents(self::$tmpDir . '/src/psalm.xml', $psalmXmlContent);
$process = new Process(['php', $this->psalm, '--config=src/psalm.xml'], self::$tmpDir);
$process->run();
$this->assertSame(1, $process->getExitCode());
$this->assertStringContainsString('InvalidReturnType', $process->getOutput());
}
/**
* @return array{STDOUT: string, STDERR: string, CODE: int|null}
*/
2020-02-17 22:33:28 +01:00
private function runPsalmInit(?int $level = null): array
{
2020-02-17 22:33:28 +01:00
$args = ['--init'];
if ($level) {
$args[] = 'src';
$args[] = (string) $level;
}
$ret = $this->runPsalm($args, self::$tmpDir, false, false);
$psalm_config_contents = file_get_contents(self::$tmpDir . '/psalm.xml');
$psalm_config_contents = \str_replace(
'errorLevel="1"',
'errorLevel="1" cacheDirectory="' . self::$tmpDir . '/cache"',
$psalm_config_contents
);
file_put_contents(self::$tmpDir . '/psalm.xml', $psalm_config_contents);
return $ret;
}
/** 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 !== '..')) {
$full = $src . '/' . $file;
if (is_dir($full)) {
self::recursiveRemoveDirectory($full);
} else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}
}