1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 22:01:48 +01:00
psalm/tests/Traits/InvalidCodeAnalysisTestTrait.php

55 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Traits;
use Psalm\Config;
use Psalm\Context;
2018-11-05 21:57:36 -05:00
trait InvalidCodeAnalysisTestTrait
{
/**
* @return array
*/
2018-11-05 21:57:36 -05:00
abstract public function providerInvalidCodeParse();
/**
2018-11-05 21:57:36 -05:00
* @dataProvider providerInvalidCodeParse
* @small
2017-05-26 20:16:18 -04:00
*
* @param string $code
* @param string $error_message
* @param array<int|string, string> $error_levels
2017-05-26 20:16:18 -04:00
* @param bool $strict_mode
*
* @return void
*/
public function testInvalidCode($code, $error_message, $error_levels = [], $strict_mode = false)
{
if (strpos($this->getTestName(), 'SKIPPED-') !== false) {
$this->markTestSkipped();
}
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->expectException('\Psalm\Exception\CodeException');
$this->expectExceptionMessageRegexp('/\b' . preg_quote($error_message, '/') . '\b/');
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile($file_path, $code);
$this->analyzeFile($file_path, new Context());
}
}