1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 08:49:52 +01:00
psalm/tests/Traits/InvalidCodeAnalysisTestTrait.php

97 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Traits;
use Psalm\Config;
use Psalm\Context;
2021-12-03 20:29:06 +01:00
use Psalm\Exception\CodeException;
2021-06-08 04:55:21 +02:00
use function preg_quote;
2021-12-03 21:07:25 +01:00
use function str_replace;
use function strpos;
2021-12-03 21:07:25 +01:00
use function strtoupper;
use function substr;
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;
/**
* @psalm-type DeprecatedDataProviderArrayNotation = array{
* code: string,
* error_message: string,
* ignored_issues?: list<string>,
* php_version?: string
* }
* @psalm-type NamedArgumentsDataProviderArrayNotation = array{
* code: string,
* error_message: string,
* error_levels?: list<string>,
* php_version?: string
* }
*/
2018-11-06 03:57:36 +01:00
trait InvalidCodeAnalysisTestTrait
{
/**
2023-01-19 00:25:50 +01:00
* @return iterable<
* string,
* DeprecatedDataProviderArrayNotation|NamedArgumentsDataProviderArrayNotation
2023-01-19 00:25:50 +01:00
* >
*/
abstract public function providerInvalidCodeParse(): iterable;
/**
2018-11-06 03:57:36 +01:00
* @dataProvider providerInvalidCodeParse
* @small
2022-01-13 20:38:17 +01:00
* @param list<string> $error_levels
*/
public function testInvalidCode(
string $code,
string $error_message,
array $error_levels = [],
string $php_version = '7.4'
): void {
2019-01-22 00:13:17 +01:00
$test_name = $this->getTestName();
if (strpos($test_name, 'PHP80-') !== false) {
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->markTestSkipped('Test case requires PHP 8.0.');
2019-01-22 00:01:15 +01:00
}
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
// sanity check - do we have a PHP tag?
if (strpos($code, '<?php') === false) {
$this->fail('Test case must have a <?php tag');
}
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);
}
2022-01-13 20:38:17 +01:00
foreach ($error_levels as $error_level) {
$issue_name = $error_level;
$error_level = Config::REPORT_SUPPRESS;
Config::getInstance()->setCustomErrorLevel($issue_name, $error_level);
}
$this->project_analyzer->setPhpVersion($php_version, 'tests');
2020-08-15 20:21:24 +02:00
$file_path = self::$src_dir_path . 'somefile.php';
// $error_message = preg_replace('/ src[\/\\\\]somefile\.php/', ' src/somefile.php', $error_message);
2021-12-03 20:29:06 +01:00
$this->expectException(CodeException::class);
2020-04-03 20:56:11 +02:00
2022-01-19 19:29:16 +01:00
$this->expectExceptionMessageMatches('/\b' . preg_quote($error_message, '/') . '\b/');
$codebase = $this->project_analyzer->getCodebase();
$codebase->enterServerMode();
$codebase->config->visitPreloadedStubFiles($codebase);
$this->addFile($file_path, $code);
$this->analyzeFile($file_path, new Context());
}
}