2017-04-25 05:45:02 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests\Traits;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
trait InvalidCodeAnalysisTestTrait
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
abstract public function providerInvalidCodeParse();
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
/**
|
2018-11-06 03:57:36 +01:00
|
|
|
* @dataProvider providerInvalidCodeParse
|
2017-11-28 06:25:21 +01:00
|
|
|
* @small
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @param string $code
|
|
|
|
* @param string $error_message
|
2018-11-01 22:03:08 +01:00
|
|
|
* @param array<int|string, string> $error_levels
|
2017-05-27 02:16:18 +02:00
|
|
|
* @param bool $strict_mode
|
|
|
|
*
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidCode($code, $error_message, $error_levels = [], $strict_mode = false)
|
|
|
|
{
|
2019-01-22 00:01:15 +01:00
|
|
|
if (strpos($test_name, 'PHP7-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'PHP71-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.1.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($strict_mode) {
|
|
|
|
Config::getInstance()->strict_binary_operands = true;
|
|
|
|
}
|
|
|
|
|
2018-11-01 22:03:08 +01:00
|
|
|
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);
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->expectException('\Psalm\Exception\CodeException');
|
2019-01-12 16:52:23 +01:00
|
|
|
$this->expectExceptionMessageRegExp('/\b' . preg_quote($error_message, '/') . '\b/');
|
2017-04-25 05:45:02 +02:00
|
|
|
|
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, new Context());
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|