1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 09:19:40 +01:00
psalm/tests/TestCase.php
Bruce Weirdan 1cf5153700
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 10:32:07 -04:00

179 lines
4.5 KiB
PHP

<?php
namespace Psalm\Tests;
use function define;
use function defined;
use const DIRECTORY_SEPARATOR;
use function getcwd;
use function ini_set;
use function method_exists;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Psalm\Config;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\Tests\Internal\Provider;
use RuntimeException;
class TestCase extends BaseTestCase
{
/** @var string */
protected static $src_dir_path;
/** @var ProjectAnalyzer */
protected $project_analyzer;
/** @var Provider\FakeFileProvider */
protected $file_provider;
/**
* @return void
*/
public static function setUpBeforeClass() : void
{
ini_set('memory_limit', '-1');
if (!defined('PSALM_VERSION')) {
define('PSALM_VERSION', '2.0.0');
}
if (!defined('PHP_PARSER_VERSION')) {
define('PHP_PARSER_VERSION', '4.0.0');
}
parent::setUpBeforeClass();
self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
}
/**
* @return Config
*/
protected function makeConfig() : Config
{
return new TestConfig();
}
/**
* @return void
*/
public function setUp() : void
{
parent::setUp();
RuntimeCaches::clearAll();
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
$config = $this->makeConfig();
$providers = new Providers(
$this->file_provider,
new Provider\FakeParserCacheProvider()
);
$this->project_analyzer = new ProjectAnalyzer(
$config,
$providers
);
$this->project_analyzer->setPhpVersion('7.4');
}
public function tearDown() : void
{
RuntimeCaches::clearAll();
}
/**
* @param string $file_path
* @param string $contents
*
* @return void
*/
public function addFile($file_path, $contents)
{
$this->file_provider->registerFile($file_path, $contents);
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($file_path);
}
/**
* @param string $file_path
* @param \Psalm\Context $context
*
* @return void
*/
public function analyzeFile($file_path, \Psalm\Context $context, bool $track_unused_suppressions = true)
{
$codebase = $this->project_analyzer->getCodebase();
$codebase->addFilesToAnalyze([$file_path => $file_path]);
$codebase->scanFiles();
$codebase->config->visitStubFiles($codebase);
if ($codebase->alter_code) {
$this->project_analyzer->interpretRefactors();
}
$this->project_analyzer->trackUnusedSuppressions();
$file_analyzer = new FileAnalyzer(
$this->project_analyzer,
$file_path,
$codebase->config->shortenFileName($file_path)
);
$file_analyzer->analyze($context);
if ($codebase->taint) {
$codebase->taint->connectSinksAndSources();
}
if ($track_unused_suppressions) {
\Psalm\IssueBuffer::processUnusedSuppressions($codebase->file_provider);
}
}
/**
* @param bool $withDataSet
*
* @return string
*/
protected function getTestName($withDataSet = true)
{
$name = parent::getName($withDataSet);
/**
* @psalm-suppress TypeDoesNotContainNull PHPUnit 8.2 made it non-nullable again
*/
if (null === $name) {
throw new RuntimeException('anonymous test - shouldn\'t happen');
}
return $name;
}
/**
* Compatibility alias
*/
public function expectExceptionMessageRegExp(string $regexp): void
{
if (method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches($regexp);
} else {
/** @psalm-suppress UndefinedMethod */
parent::expectExceptionMessageRegExp($regexp);
}
}
public static function assertRegExp(string $pattern, string $string, string $message = ''): void
{
if (method_exists(self::class, 'assertMatchesRegularExpression')) {
self::assertMatchesRegularExpression($pattern, $string, $message);
} else {
parent::assertRegExp($pattern, $string, $message);
}
}
}