2017-04-24 23:45:02 -04:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests\Traits;
|
|
|
|
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2017-06-29 10:25:41 -04:00
|
|
|
use Psalm\Type\Union;
|
2017-04-24 23:45:02 -04:00
|
|
|
|
|
|
|
trait FileCheckerValidCodeParseTestTrait
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function providerFileCheckerValidCodeParse();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerFileCheckerValidCodeParse
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2017-04-24 23:45:02 -04:00
|
|
|
* @param string $code
|
2017-06-29 10:25:41 -04:00
|
|
|
* @param array<string, string> $assertions
|
2017-04-24 23:45:02 -04:00
|
|
|
* @param array<string> $error_levels
|
2017-06-29 10:25:41 -04:00
|
|
|
* @param array<string, Union> $scope_vars
|
2017-05-26 20:16:18 -04:00
|
|
|
*
|
2017-04-24 23:45:02 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidCode($code, $assertions = [], $error_levels = [], $scope_vars = [])
|
|
|
|
{
|
|
|
|
$test_name = $this->getName();
|
|
|
|
if (strpos($test_name, 'PHP7-') !== false) {
|
|
|
|
if (version_compare(PHP_VERSION, '7.0.0dev', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 7.');
|
2017-05-24 22:07:49 -04:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($error_levels as $error_level) {
|
|
|
|
Config::getInstance()->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
foreach ($scope_vars as $var => $value) {
|
|
|
|
$context->vars_in_scope[$var] = $value;
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:11:02 -04:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
$code
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
2017-04-24 23:45:02 -04:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
|
2017-06-29 10:22:49 -04:00
|
|
|
foreach ($assertions as $var => $expected) {
|
|
|
|
$this->assertSame($expected, (string)$context->vars_in_scope[$var]);
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|