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

91 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Traits;
2019-07-05 22:24:00 +02:00
use function is_int;
use const PHP_VERSION;
use function preg_quote;
use Psalm\Config;
use Psalm\Context;
use function strpos;
use function version_compare;
2018-11-06 03:57:36 +01:00
trait InvalidCodeAnalysisTestTrait
{
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
abstract public function providerInvalidCodeParse(): iterable;
/**
2018-11-06 03:57:36 +01:00
* @dataProvider providerInvalidCodeParse
* @small
2017-05-27 02:16:18 +02:00
*
* @param string $code
* @param string $error_message
* @param array<int|string, string> $error_levels
2017-05-27 02:16:18 +02:00
* @param bool $strict_mode
*
* @return void
*/
public function testInvalidCode(
$code,
$error_message,
$error_levels = [],
$strict_mode = false,
string $php_version = '7.3'
) {
2019-01-22 00:13:17 +01:00
$test_name = $this->getTestName();
if (strpos($test_name, 'PHP71-') !== false) {
2019-01-22 00:01:15 +01:00
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('Test case requires PHP 7.1.');
return;
}
} elseif (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
return;
}
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
if (\strtoupper(\substr(\PHP_OS, 0, 3)) === 'WIN') {
2020-03-10 18:03:51 +01:00
$code = \str_replace("\n", "\r\n", $code);
}
if ($strict_mode) {
Config::getInstance()->strict_binary_operands = true;
}
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);
}
$this->project_analyzer->setPhpVersion($php_version);
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);
2019-02-27 22:00:44 +01:00
$this->expectException(\Psalm\Exception\CodeException::class);
2020-04-03 20:56:11 +02:00
if (\method_exists($this, 'expectExceptionMessageMatches')) {
$this->expectExceptionMessageMatches('/\b' . preg_quote($error_message, '/') . '\b/');
} else {
$this->expectExceptionMessageRegExp('/\b' . preg_quote($error_message, '/') . '\b/');
}
$this->addFile($file_path, $code);
$this->analyzeFile($file_path, new Context());
}
}