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\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;
|
2019-06-26 22:52:29 +02:00
|
|
|
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;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function substr;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function version_compare;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
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
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
/**
|
2021-11-15 04:56:09 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[],php_version?:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
abstract public function providerValidCodeParse(): iterable;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @dataProvider providerValidCodeParse
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $code
|
2017-06-29 16:25:41 +02:00
|
|
|
* @param array<string, string> $assertions
|
2017-09-14 05:57:11 +02:00
|
|
|
* @param array<string|int, string> $error_levels
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-11-28 06:25:21 +01:00
|
|
|
* @small
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2019-02-07 18:25:57 +01:00
|
|
|
public function testValidCode(
|
|
|
|
$code,
|
|
|
|
$assertions = [],
|
|
|
|
$error_levels = [],
|
|
|
|
string $php_version = '7.3'
|
2021-11-26 20:59:41 +01:00
|
|
|
): void {
|
2018-07-13 23:44:50 +02:00
|
|
|
$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.');
|
2020-10-12 17:35:14 +02:00
|
|
|
}
|
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.');
|
|
|
|
}
|
2020-10-12 17:35:14 +02:00
|
|
|
} elseif (strpos($test_name, 'PHP80-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 8.0.');
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
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.');
|
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:57:11 +02:00
|
|
|
foreach ($error_levels as $error_level_key => $error_level) {
|
2017-09-16 18:45:11 +02:00
|
|
|
if (is_int($error_level_key)) {
|
2017-09-14 05:57:11 +02:00
|
|
|
$issue_name = $error_level;
|
|
|
|
$error_level = Config::REPORT_SUPPRESS;
|
|
|
|
} else {
|
|
|
|
$issue_name = $error_level_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::getInstance()->setCustomErrorLevel($issue_name, $error_level);
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2020-03-10 17:52:18 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$context = new Context();
|
2019-02-07 18:25:57 +01:00
|
|
|
|
2021-11-27 01:06:33 +01:00
|
|
|
$this->project_analyzer->setPhpVersion($php_version, 'tests');
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2020-10-30 00:41:10 +01:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
|
|
|
$codebase->config->visitPreloadedStubFiles($codebase);
|
|
|
|
|
2018-01-21 16:22:04 +01:00
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
2018-01-21 19:38:51 +01:00
|
|
|
$this->addFile($file_path, $code);
|
2018-01-21 16:22:04 +01:00
|
|
|
$this->analyzeFile($file_path, $context);
|
2017-04-25 05:45:02 +02:00
|
|
|
|
2017-08-15 01:30:11 +02:00
|
|
|
$actual_vars = [];
|
|
|
|
foreach ($assertions as $var => $_) {
|
2019-03-07 20:56:18 +01:00
|
|
|
$exact = false;
|
|
|
|
|
|
|
|
if ($var && strpos($var, '===') === strlen($var) - 3) {
|
|
|
|
$var = substr($var, 0, -3);
|
|
|
|
$exact = true;
|
|
|
|
}
|
|
|
|
|
2017-11-20 05:19:49 +01:00
|
|
|
if (isset($context->vars_in_scope[$var])) {
|
2019-03-07 20:56:18 +01:00
|
|
|
if ($exact) {
|
|
|
|
$actual_vars[$var . '==='] = $context->vars_in_scope[$var]->getId();
|
|
|
|
} else {
|
|
|
|
$actual_vars[$var] = (string)$context->vars_in_scope[$var];
|
|
|
|
}
|
2017-11-20 05:19:49 +01:00
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-08-15 01:30:11 +02:00
|
|
|
|
|
|
|
$this->assertSame($assertions, $actual_vars);
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|