2017-04-25 05:45:02 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
use PHPUnit\Framework\TestCase as BaseTestCase;
|
2020-02-24 20:43:44 +01:00
|
|
|
use Psalm\Config;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2019-03-23 19:27:54 +01:00
|
|
|
use Psalm\Internal\Provider\Providers;
|
2020-08-23 16:32:07 +02:00
|
|
|
use Psalm\Internal\RuntimeCaches;
|
2018-11-12 16:57:05 +01:00
|
|
|
use Psalm\Tests\Internal\Provider;
|
2018-07-13 23:44:50 +02:00
|
|
|
use RuntimeException;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2021-06-08 04:55:21 +02:00
|
|
|
use function define;
|
|
|
|
use function defined;
|
|
|
|
use function getcwd;
|
|
|
|
use function ini_set;
|
|
|
|
use function method_exists;
|
|
|
|
|
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
class TestCase extends BaseTestCase
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2017-07-25 23:04:58 +02:00
|
|
|
/** @var string */
|
|
|
|
protected static $src_dir_path;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
/** @var ProjectAnalyzer */
|
2018-11-11 18:01:14 +01:00
|
|
|
protected $project_analyzer;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/** @var Provider\FakeFileProvider */
|
|
|
|
protected $file_provider;
|
|
|
|
|
2021-05-26 22:04:22 +02:00
|
|
|
/** @var Config */
|
|
|
|
protected $testConfig;
|
|
|
|
|
2019-05-17 00:36:36 +02:00
|
|
|
public static function setUpBeforeClass() : void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2018-01-09 15:21:54 +01:00
|
|
|
ini_set('memory_limit', '-1');
|
2018-04-25 03:02:07 +02:00
|
|
|
|
|
|
|
if (!defined('PSALM_VERSION')) {
|
2021-05-17 15:27:08 +02:00
|
|
|
define('PSALM_VERSION', '4.0.0');
|
2018-04-25 03:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined('PHP_PARSER_VERSION')) {
|
|
|
|
define('PHP_PARSER_VERSION', '4.0.0');
|
|
|
|
}
|
2018-04-24 13:19:25 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
parent::setUpBeforeClass();
|
2017-07-25 23:04:58 +02:00
|
|
|
self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:58:49 +01:00
|
|
|
protected function makeConfig() : Config
|
2020-02-24 20:43:44 +01:00
|
|
|
{
|
|
|
|
return new TestConfig();
|
|
|
|
}
|
|
|
|
|
2019-05-17 00:36:36 +02:00
|
|
|
public function setUp() : void
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2020-08-10 15:58:43 +02:00
|
|
|
|
2018-11-12 16:57:05 +01:00
|
|
|
$this->file_provider = new \Psalm\Tests\Internal\Provider\FakeFileProvider();
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2021-05-26 22:04:22 +02:00
|
|
|
$this->testConfig = $this->makeConfig();
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
$providers = new Providers(
|
|
|
|
$this->file_provider,
|
|
|
|
new Provider\FakeParserCacheProvider()
|
|
|
|
);
|
2018-01-21 19:38:51 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$this->project_analyzer = new ProjectAnalyzer(
|
2021-05-26 22:04:22 +02:00
|
|
|
$this->testConfig,
|
2019-06-09 18:37:28 +02:00
|
|
|
$providers
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2019-02-07 20:08:35 +01:00
|
|
|
|
2020-08-16 19:03:30 +02:00
|
|
|
$this->project_analyzer->setPhpVersion('7.4');
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-05-17 00:36:36 +02:00
|
|
|
public function tearDown() : void
|
|
|
|
{
|
2021-05-26 22:04:22 +02:00
|
|
|
unset($this->project_analyzer, $this->file_provider, $this->testConfig);
|
2020-08-23 16:32:07 +02:00
|
|
|
RuntimeCaches::clearAll();
|
2019-05-17 00:36:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $contents
|
|
|
|
*
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function addFile($file_path, $contents): void
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
2019-01-12 16:52:23 +01:00
|
|
|
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($file_path);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-01-21 16:22:04 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-21 19:38:51 +01:00
|
|
|
* @param string $file_path
|
2018-01-21 16:22:04 +01:00
|
|
|
*
|
|
|
|
*/
|
2020-11-17 19:23:20 +01:00
|
|
|
public function analyzeFile($file_path, \Psalm\Context $context, bool $track_unused_suppressions = true, bool $taint_flow_tracking = false): void
|
2018-01-21 16:22:04 +01:00
|
|
|
{
|
2018-11-11 18:01:14 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2020-10-30 00:41:10 +01:00
|
|
|
|
2020-11-17 19:23:20 +01:00
|
|
|
if ($taint_flow_tracking) {
|
|
|
|
$this->project_analyzer->trackTaintedInputs();
|
|
|
|
}
|
|
|
|
|
2018-02-04 00:52:35 +01:00
|
|
|
$codebase->addFilesToAnalyze([$file_path => $file_path]);
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$codebase->scanFiles();
|
|
|
|
|
2018-06-30 21:29:37 +02:00
|
|
|
$codebase->config->visitStubFiles($codebase);
|
|
|
|
|
2019-06-03 05:33:57 +02:00
|
|
|
if ($codebase->alter_code) {
|
|
|
|
$this->project_analyzer->interpretRefactors();
|
|
|
|
}
|
|
|
|
|
2019-08-18 22:59:56 +02:00
|
|
|
$this->project_analyzer->trackUnusedSuppressions();
|
2019-08-18 21:34:32 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_analyzer = new FileAnalyzer(
|
|
|
|
$this->project_analyzer,
|
2018-01-21 19:38:51 +01:00
|
|
|
$file_path,
|
|
|
|
$codebase->config->shortenFileName($file_path)
|
|
|
|
);
|
2018-11-11 18:01:14 +01:00
|
|
|
$file_analyzer->analyze($context);
|
2019-08-04 16:37:36 +02:00
|
|
|
|
2020-09-25 06:37:40 +02:00
|
|
|
if ($codebase->taint_flow_graph) {
|
|
|
|
$codebase->taint_flow_graph->connectSinksAndSources();
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
2019-08-18 20:27:50 +02:00
|
|
|
|
2019-08-18 21:34:32 +02:00
|
|
|
if ($track_unused_suppressions) {
|
2019-08-18 20:27:50 +02:00
|
|
|
\Psalm\IssueBuffer::processUnusedSuppressions($codebase->file_provider);
|
|
|
|
}
|
2018-01-21 16:22:04 +01:00
|
|
|
}
|
2018-07-23 01:29:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $withDataSet
|
2019-03-23 19:27:54 +01:00
|
|
|
*
|
2018-07-23 01:29:04 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
protected function getTestName($withDataSet = true): string
|
2018-07-13 23:44:50 +02:00
|
|
|
{
|
|
|
|
$name = parent::getName($withDataSet);
|
2019-06-07 12:51:34 +02:00
|
|
|
/**
|
|
|
|
* @psalm-suppress TypeDoesNotContainNull PHPUnit 8.2 made it non-nullable again
|
|
|
|
*/
|
2018-07-13 23:44:50 +02:00
|
|
|
if (null === $name) {
|
|
|
|
throw new RuntimeException('anonymous test - shouldn\'t happen');
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
|
2018-07-13 23:44:50 +02:00
|
|
|
return $name;
|
|
|
|
}
|
2020-04-05 23:25:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|