2017-04-25 05:45:02 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
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;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Context;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\FileAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-07-02 01:10:21 +02:00
|
|
|
use Psalm\Internal\Provider\FakeFileProvider;
|
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;
|
2021-08-08 10:39:54 +02:00
|
|
|
use Psalm\Internal\Type\TypeParser;
|
|
|
|
use Psalm\Internal\Type\TypeTokenizer;
|
2023-12-12 07:51:21 +01:00
|
|
|
use Psalm\Internal\VersionUtils;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\IssueBuffer;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
|
2021-08-08 10:39:54 +02:00
|
|
|
use Psalm\Type\Union;
|
|
|
|
use Throwable;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
use function array_filter;
|
|
|
|
use function count;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function define;
|
|
|
|
use function defined;
|
|
|
|
use function getcwd;
|
2024-02-13 09:38:13 +01:00
|
|
|
use function implode;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function ini_set;
|
2021-08-08 10:39:54 +02:00
|
|
|
use function is_string;
|
2024-02-13 09:38:13 +01:00
|
|
|
use function preg_match;
|
|
|
|
use function preg_quote;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
use const ARRAY_FILTER_USE_KEY;
|
2021-06-08 04:55:21 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-12-15 12:18:33 +01:00
|
|
|
class TestCase extends BaseTestCase
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
2022-12-16 19:58:47 +01:00
|
|
|
protected static string $src_dir_path;
|
2017-07-25 23:04:58 +02:00
|
|
|
|
2023-11-15 09:45:56 +01:00
|
|
|
/**
|
|
|
|
* caused by phpunit using setUp() instead of __construct
|
|
|
|
* could perhaps use psalm-plugin-phpunit once https://github.com/psalm/psalm-plugin-phpunit/issues/129
|
|
|
|
* to remove this suppression
|
|
|
|
*
|
|
|
|
* @psalm-suppress PropertyNotSetInConstructor
|
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
protected ProjectAnalyzer $project_analyzer;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2023-11-15 09:45:56 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress PropertyNotSetInConstructor
|
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
protected FakeFileProvider $file_provider;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2023-11-15 09:45:56 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress PropertyNotSetInConstructor
|
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
protected Config $testConfig;
|
2021-05-26 22:04:22 +02:00
|
|
|
|
2021-12-05 18:51:26 +01: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')) {
|
2023-12-12 07:51:21 +01:00
|
|
|
define('PSALM_VERSION', VersionUtils::getPsalmVersion());
|
2018-04-25 03:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined('PHP_PARSER_VERSION')) {
|
2023-12-12 07:51:21 +01:00
|
|
|
define('PHP_PARSER_VERSION', VersionUtils::getPhpParserVersion());
|
2018-04-25 03:02:07 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
protected function makeConfig(): Config
|
2020-02-24 20:43:44 +01:00
|
|
|
{
|
|
|
|
return new TestConfig();
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01: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
|
|
|
|
2021-07-02 01:10:21 +02:00
|
|
|
$this->file_provider = new 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,
|
2022-12-18 17:15:15 +01:00
|
|
|
new FakeParserCacheProvider(),
|
2018-09-28 22:18:45 +02:00
|
|
|
);
|
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,
|
2022-12-18 17:15:15 +01:00
|
|
|
$providers,
|
2017-07-25 22:11:02 +02:00
|
|
|
);
|
2019-02-07 20:08:35 +01:00
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->project_analyzer->setPhpVersion('7.4', 'tests');
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function tearDown(): void
|
2019-05-17 00:36:36 +02:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
public function addFile(string $file_path, string $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
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
public function addStubFile(string $file_path, string $contents): void
|
2022-04-27 07:42:37 +02:00
|
|
|
{
|
|
|
|
$this->file_provider->registerFile($file_path, $contents);
|
|
|
|
$this->project_analyzer->getConfig()->addStubFile($file_path);
|
|
|
|
}
|
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
public function analyzeFile(string $file_path, 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,
|
2022-12-18 17:15:15 +01:00
|
|
|
$codebase->config->shortenFileName($file_path),
|
2018-01-21 19:38:51 +01:00
|
|
|
);
|
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) {
|
2021-12-03 20:11:20 +01:00
|
|
|
IssueBuffer::processUnusedSuppressions($codebase->file_provider);
|
2019-08-18 20:27:50 +02:00
|
|
|
}
|
2018-01-21 16:22:04 +01:00
|
|
|
}
|
2018-07-23 01:29:04 +02:00
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
protected function getTestName(bool $withDataSet = true): string
|
2018-07-13 23:44:50 +02:00
|
|
|
{
|
2021-12-27 19:59:11 +01:00
|
|
|
return $this->getName($withDataSet);
|
2018-07-13 23:44:50 +02:00
|
|
|
}
|
2020-04-05 23:25:00 +02:00
|
|
|
|
2024-02-14 16:42:44 +01:00
|
|
|
public static function assertHasIssue(string $expected, string $message = ''): void
|
2024-02-13 09:38:13 +01:00
|
|
|
{
|
2024-02-14 16:26:42 +01:00
|
|
|
$issue_messages = [];
|
2024-02-13 09:38:13 +01:00
|
|
|
$res = false;
|
|
|
|
$issues = IssueBuffer::getIssuesData();
|
|
|
|
foreach ($issues as $file_issues) {
|
|
|
|
foreach ($file_issues as $issue) {
|
2024-02-14 16:26:42 +01:00
|
|
|
$full_issue_message = $issue->type . ' - ' . $issue->file_name . ':' . $issue->line_from . ':' . $issue->column_from . ' - ' . $issue->message;
|
|
|
|
$issue_messages[] = $full_issue_message;
|
|
|
|
if (preg_match('/\b' . preg_quote($expected, '/') . '\b/', $full_issue_message)) {
|
2024-02-13 09:38:13 +01:00
|
|
|
$res = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$message) {
|
2024-02-14 07:57:24 +01:00
|
|
|
$message = "Failed asserting that issue with \"$expected\" was emitted.";
|
2024-02-14 16:26:42 +01:00
|
|
|
if (count($issue_messages)) {
|
|
|
|
$message .= "\n" . 'Other issues reported:' . "\n - " . implode("\n - ", $issue_messages);
|
2024-02-13 09:38:13 +01:00
|
|
|
} else {
|
2024-02-14 07:57:24 +01:00
|
|
|
$message .= ' No issues reported.';
|
2024-02-13 09:38:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self::assertTrue($res, $message);
|
|
|
|
}
|
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayKeysAreStrings(array $array, string $message = ''): void
|
|
|
|
{
|
|
|
|
$validKeys = array_filter($array, 'is_string', ARRAY_FILTER_USE_KEY);
|
|
|
|
self::assertTrue(count($array) === count($validKeys), $message);
|
|
|
|
}
|
2021-11-27 01:06:33 +01:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void
|
|
|
|
{
|
2023-10-21 20:45:09 +02:00
|
|
|
$isZeroOrString = /** @param mixed $key */ static fn($key): bool => $key === 0 || is_string($key);
|
2021-08-08 10:39:54 +02:00
|
|
|
$validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY);
|
|
|
|
self::assertTrue(count($array) === count($validKeys), $message);
|
|
|
|
}
|
2021-11-27 01:06:33 +01:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayValuesAreArrays(array $array, string $message = ''): void
|
|
|
|
{
|
|
|
|
$validValues = array_filter($array, 'is_array');
|
|
|
|
self::assertTrue(count($array) === count($validValues), $message);
|
|
|
|
}
|
2021-11-27 01:06:33 +01:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertArrayValuesAreStrings(array $array, string $message = ''): void
|
|
|
|
{
|
|
|
|
$validValues = array_filter($array, 'is_string');
|
|
|
|
self::assertTrue(count($array) === count($validValues), $message);
|
|
|
|
}
|
2021-11-27 01:06:33 +01:00
|
|
|
|
2021-08-08 10:39:54 +02:00
|
|
|
public static function assertStringIsParsableType(string $type, string $message = ''): void
|
|
|
|
{
|
|
|
|
if ($type === '') {
|
|
|
|
// Ignore empty types for now, as these are quite common for pecl libraries
|
|
|
|
self::assertTrue(true);
|
|
|
|
} else {
|
|
|
|
$union = null;
|
|
|
|
try {
|
|
|
|
$tokens = TypeTokenizer::tokenize($type);
|
|
|
|
$union = TypeParser::parseTokens($tokens);
|
|
|
|
} catch (Throwable $_e) {
|
|
|
|
}
|
|
|
|
self::assertInstanceOf(Union::class, $union, $message);
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|