1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/tests/FileManipulation/FileManipulationTestCase.php

107 lines
3.0 KiB
PHP
Raw Normal View History

2019-05-02 18:15:38 +02:00
<?php
2019-05-02 18:15:38 +02:00
namespace Psalm\Tests\FileManipulation;
use Psalm\Context;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Provider\Providers;
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 Psalm\Internal\RuntimeCaches;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
2021-12-03 20:11:20 +01:00
use Psalm\Tests\TestCase;
2019-05-02 18:15:38 +02:00
use Psalm\Tests\TestConfig;
2021-06-08 04:55:21 +02:00
use function strpos;
2019-05-02 18:15:38 +02:00
2021-12-03 20:11:20 +01:00
abstract class FileManipulationTestCase extends TestCase
2019-05-02 18:15:38 +02:00
{
2021-12-04 03:37:19 +01:00
/** @var ProjectAnalyzer */
2019-05-02 18:15:38 +02:00
protected $project_analyzer;
public function setUp(): void
2019-05-02 18:15:38 +02:00
{
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
RuntimeCaches::clearAll();
2019-05-02 18:15:38 +02:00
$this->file_provider = new FakeFileProvider();
2019-05-02 18:15:38 +02:00
}
/**
* @dataProvider providerValidCodeParse
*
* @param string $input_code
* @param string $output_code
* @param string $php_version
* @param string[] $issues_to_fix
* @param bool $safe_types
*
*/
public function testValidCode(
$input_code,
$output_code,
$php_version,
array $issues_to_fix,
$safe_types,
bool $allow_backwards_incompatible_changes = true
): void {
2019-05-02 18:15:38 +02:00
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$config = new TestConfig();
2021-12-03 20:11:20 +01:00
$this->project_analyzer = new ProjectAnalyzer(
2019-05-02 18:15:38 +02:00
$config,
2021-12-03 20:11:20 +01:00
new Providers(
2019-05-02 18:15:38 +02:00
$this->file_provider,
2021-12-04 21:55:53 +01:00
new FakeParserCacheProvider()
2019-05-02 18:15:38 +02:00
)
);
if (empty($issues_to_fix)) {
$config->addPluginPath('examples/plugins/ClassUnqualifier.php');
$config->initializePlugins($this->project_analyzer);
}
$context = new Context();
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile(
$file_path,
$input_code
);
$this->project_analyzer->setPhpVersion($php_version, 'tests');
2019-05-02 18:15:38 +02:00
$keyed_issues_to_fix = [];
foreach ($issues_to_fix as $issue) {
$keyed_issues_to_fix[$issue] = true;
}
$this->project_analyzer->setIssuesToFix($keyed_issues_to_fix);
$this->project_analyzer->alterCodeAfterCompletion(
false,
$safe_types
);
$this->project_analyzer->getCodebase()->allow_backwards_incompatible_changes = $allow_backwards_incompatible_changes;
2019-05-02 18:15:38 +02:00
if (strpos(static::class, 'Unused') || strpos(static::class, 'Unnecessary')) {
$this->project_analyzer->getCodebase()->reportUnusedCode();
}
2019-05-02 18:15:38 +02:00
$this->analyzeFile($file_path, $context);
$this->project_analyzer->consolidateAnalyzedData();
2019-05-02 18:15:38 +02:00
$this->project_analyzer->getCodebase()->analyzer->updateFile($file_path, false);
$this->assertSame($output_code, $this->project_analyzer->getCodebase()->getFileContents($file_path));
}
/**
* @return array<string,array{string,string,string,string[],bool}>
*/
abstract public function providerValidCodeParse(): array;
2019-05-02 18:15:38 +02:00
}