2017-04-25 05:45:02 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests\Traits;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2017-06-29 16:25:41 +02:00
|
|
|
use Psalm\Type\Union;
|
2017-04-25 05:45:02 +02:00
|
|
|
|
|
|
|
trait FileCheckerValidCodeParseTestTrait
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function providerFileCheckerValidCodeParse();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerFileCheckerValidCodeParse
|
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-06-29 16:25:41 +02:00
|
|
|
* @param array<string, Union> $scope_vars
|
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
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidCode($code, $assertions = [], $error_levels = [], $scope_vars = [])
|
|
|
|
{
|
2018-07-13 23:44:50 +02:00
|
|
|
$test_name = $this->getTestName();
|
2017-04-25 05:45:02 +02:00
|
|
|
if (strpos($test_name, 'PHP7-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.');
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2018-05-20 19:20:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'PHP71-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.1.');
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
foreach ($scope_vars as $var => $value) {
|
|
|
|
$context->vars_in_scope[$var] = $value;
|
|
|
|
}
|
|
|
|
|
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 => $_) {
|
2017-11-20 05:19:49 +01:00
|
|
|
if (isset($context->vars_in_scope[$var])) {
|
|
|
|
$actual_vars[$var] = (string)$context->vars_in_scope[$var];
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|