1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/Traits/ValidCodeAnalysisTestTrait.php

114 lines
3.6 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Traits;
use Psalm\Config;
use Psalm\Context;
2021-06-08 04:55:21 +02:00
use function is_int;
2021-12-03 21:07:25 +01:00
use function str_replace;
use function strlen;
2019-07-05 22:24:00 +02:00
use function strpos;
2021-12-03 21:07:25 +01:00
use function strtoupper;
use function substr;
2019-07-05 22:24:00 +02:00
use function version_compare;
2021-12-03 21:25:22 +01:00
use const PHP_OS;
2021-06-08 04:55:21 +02:00
use const PHP_VERSION;
2018-11-06 03:57:36 +01:00
trait ValidCodeAnalysisTestTrait
{
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[],php_version?:string}>
*/
abstract public function providerValidCodeParse(): iterable;
/**
2018-11-06 03:57:36 +01:00
* @dataProvider providerValidCodeParse
2017-05-27 02:16:18 +02:00
*
* @param string $code
2017-06-29 16:25:41 +02:00
* @param array<string, string> $assertions
* @param array<string|int, string> $error_levels
2017-05-27 02:16:18 +02:00
*
* @small
*/
public function testValidCode(
$code,
$assertions = [],
$error_levels = [],
string $php_version = '7.3'
): void {
$test_name = $this->getTestName();
2021-11-30 03:25:39 +01:00
if (strpos($test_name, 'PHP71-') !== false) {
2018-05-20 19:20:15 +02:00
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.1.');
}
2021-11-30 03:25:39 +01:00
} elseif (strpos($test_name, 'PHP72-') !== false) {
if (version_compare(PHP_VERSION, '7.2.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.2.');
}
} elseif (strpos($test_name, 'PHP73-') !== false) {
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.3.');
}
} elseif (strpos($test_name, 'PHP80-') !== false) {
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->markTestSkipped('Test case requires PHP 8.0.');
}
2022-02-22 22:41:53 +01:00
} elseif (strpos($test_name, 'PHP81-') !== false) {
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
$this->markTestSkipped('Test case requires PHP 8.1.');
}
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
foreach ($error_levels as $error_level_key => $error_level) {
if (is_int($error_level_key)) {
$issue_name = $error_level;
$error_level = Config::REPORT_SUPPRESS;
} else {
$issue_name = $error_level_key;
}
Config::getInstance()->setCustomErrorLevel($issue_name, $error_level);
}
2021-12-03 21:25:22 +01:00
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
2021-12-03 21:07:25 +01:00
$code = str_replace("\n", "\r\n", $code);
}
$context = new Context();
$this->project_analyzer->setPhpVersion($php_version, 'tests');
$codebase = $this->project_analyzer->getCodebase();
$codebase->config->visitPreloadedStubFiles($codebase);
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile($file_path, $code);
$this->analyzeFile($file_path, $context);
$actual_vars = [];
foreach ($assertions as $var => $_) {
$exact = false;
if ($var && strpos($var, '===') === strlen($var) - 3) {
$var = substr($var, 0, -3);
$exact = true;
}
if (isset($context->vars_in_scope[$var])) {
if ($exact) {
$actual_vars[$var . '==='] = $context->vars_in_scope[$var]->getId();
} else {
$actual_vars[$var] = (string)$context->vars_in_scope[$var];
}
}
}
$this->assertSame($assertions, $actual_vars);
}
}