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

197 lines
5.6 KiB
PHP
Raw Normal View History

2022-02-18 18:29:09 +01:00
<?php
namespace Psalm\Tests;
use Amp\PHPUnit\AsyncTestCase as BaseAsyncTestCase;
use Psalm\Config;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\Internal\Type\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\IssueBuffer;
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
use Psalm\Type\Union;
use Throwable;
use function array_filter;
use function count;
use function define;
use function defined;
use function getcwd;
use function ini_set;
use function is_string;
use const ARRAY_FILTER_USE_KEY;
use const DIRECTORY_SEPARATOR;
class AsyncTestCase extends BaseAsyncTestCase
{
2022-12-20 22:25:04 +01:00
protected static string $src_dir_path;
protected ProjectAnalyzer $project_analyzer;
protected FakeFileProvider $file_provider;
protected Config $testConfig;
2022-02-18 18:29:09 +01:00
public static function setUpBeforeClass(): void
{
ini_set('memory_limit', '-1');
if (!defined('PSALM_VERSION')) {
2022-12-20 22:25:04 +01:00
define('PSALM_VERSION', '5.0.0');
2022-02-18 18:29:09 +01:00
}
if (!defined('PHP_PARSER_VERSION')) {
2022-12-20 22:25:04 +01:00
define('PHP_PARSER_VERSION', '5.0.0');
2022-02-18 18:29:09 +01:00
}
parent::setUpBeforeClass();
self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
}
protected function makeConfig(): Config
{
return new TestConfig();
}
public function setUp(): void
{
parent::setUp();
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
$this->testConfig = $this->makeConfig();
$providers = new Providers(
$this->file_provider,
2022-12-20 22:26:25 +01:00
new FakeParserCacheProvider(),
2022-02-18 18:29:09 +01:00
);
$this->project_analyzer = new ProjectAnalyzer(
$this->testConfig,
2022-12-20 22:26:25 +01:00
$providers,
2022-02-18 18:29:09 +01:00
);
$this->project_analyzer->setPhpVersion('7.4', 'tests');
}
public function tearDown(): void
{
unset($this->project_analyzer, $this->file_provider, $this->testConfig);
RuntimeCaches::clearAll();
}
2022-12-20 22:26:25 +01:00
public function addFile(string $file_path, string $contents): void
2022-02-18 18:29:09 +01:00
{
$this->file_provider->registerFile($file_path, $contents);
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($file_path);
}
/**
2022-04-28 20:20:51 +02:00
* @psalm-suppress UnusedMethod
2022-02-18 18:29:09 +01:00
*/
2022-12-20 22:26:25 +01:00
public function analyzeFile(string $file_path, Context $context, bool $track_unused_suppressions = true, bool $taint_flow_tracking = false): void
2022-02-18 18:29:09 +01:00
{
$codebase = $this->project_analyzer->getCodebase();
if ($taint_flow_tracking) {
$this->project_analyzer->trackTaintedInputs();
}
$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,
2022-12-20 22:26:25 +01:00
$codebase->config->shortenFileName($file_path),
2022-02-18 18:29:09 +01:00
);
$file_analyzer->analyze($context);
if ($codebase->taint_flow_graph) {
$codebase->taint_flow_graph->connectSinksAndSources();
}
if ($track_unused_suppressions) {
IssueBuffer::processUnusedSuppressions($codebase->file_provider);
}
}
/**
2022-04-28 20:20:51 +02:00
* @psalm-suppress UnusedMethod
2022-02-18 18:29:09 +01:00
*/
2022-12-20 22:26:25 +01:00
protected function getTestName(bool $withDataSet = true): string
2022-02-18 18:29:09 +01:00
{
return $this->getName($withDataSet);
}
2022-04-28 20:28:59 +02:00
/**
* @psalm-suppress UnusedMethod
*/
2022-02-18 18:29:09 +01: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);
}
2022-04-28 20:28:59 +02:00
/**
* @psalm-suppress UnusedMethod
*/
2022-02-18 18:29:09 +01: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);
2022-02-18 18:29:09 +01:00
$validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY);
self::assertTrue(count($array) === count($validKeys), $message);
}
2022-04-28 20:28:59 +02:00
/**
* @psalm-suppress UnusedMethod
*/
2022-02-18 18:29:09 +01:00
public static function assertArrayValuesAreArrays(array $array, string $message = ''): void
{
$validValues = array_filter($array, 'is_array');
self::assertTrue(count($array) === count($validValues), $message);
}
2022-04-28 20:28:59 +02:00
/**
* @psalm-suppress UnusedMethod
*/
2022-02-18 18:29:09 +01:00
public static function assertArrayValuesAreStrings(array $array, string $message = ''): void
{
$validValues = array_filter($array, 'is_string');
self::assertTrue(count($array) === count($validValues), $message);
}
2022-04-28 20:28:59 +02:00
/**
* @psalm-suppress UnusedMethod
*/
2022-02-18 18:29:09 +01: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);
}
}
}