2017-04-24 23:45:02 -04:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2017-04-24 23:45:02 -04:00
|
|
|
namespace Psalm\Tests\Traits;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2021-12-03 20:29:06 +01:00
|
|
|
use Psalm\Exception\CodeException;
|
2021-06-08 05:55:21 +03:00
|
|
|
|
|
|
|
use function preg_quote;
|
2021-12-03 21:07:25 +01:00
|
|
|
use function str_replace;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strpos;
|
2021-12-03 21:07:25 +01:00
|
|
|
use function strtoupper;
|
|
|
|
use function substr;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function version_compare;
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2021-12-03 21:25:22 +01:00
|
|
|
use const PHP_OS;
|
2021-06-08 05:55:21 +03:00
|
|
|
use const PHP_VERSION;
|
|
|
|
|
2023-04-03 23:05:02 +02:00
|
|
|
/**
|
|
|
|
* @psalm-type DeprecatedDataProviderArrayNotation = array{
|
|
|
|
* code: string,
|
|
|
|
* error_message: string,
|
|
|
|
* ignored_issues?: list<string>,
|
|
|
|
* php_version?: string
|
|
|
|
* }
|
|
|
|
* @psalm-type NamedArgumentsDataProviderArrayNotation = array{
|
|
|
|
* code: string,
|
|
|
|
* error_message: string,
|
|
|
|
* error_levels?: list<string>,
|
|
|
|
* php_version?: string
|
|
|
|
* }
|
|
|
|
*/
|
2018-11-05 21:57:36 -05:00
|
|
|
trait InvalidCodeAnalysisTestTrait
|
2017-04-24 23:45:02 -04:00
|
|
|
{
|
|
|
|
/**
|
2023-01-18 18:25:50 -05:00
|
|
|
* @return iterable<
|
|
|
|
* string,
|
2023-04-03 23:05:02 +02:00
|
|
|
* DeprecatedDataProviderArrayNotation|NamedArgumentsDataProviderArrayNotation
|
2023-01-18 18:25:50 -05:00
|
|
|
* >
|
2017-04-24 23:45:02 -04:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
abstract public function providerInvalidCodeParse(): iterable;
|
2017-04-24 23:45:02 -04:00
|
|
|
|
|
|
|
/**
|
2018-11-05 21:57:36 -05:00
|
|
|
* @dataProvider providerInvalidCodeParse
|
2017-11-28 00:25:21 -05:00
|
|
|
* @small
|
2022-01-13 14:38:17 -05:00
|
|
|
* @param list<string> $error_levels
|
2017-04-24 23:45:02 -04:00
|
|
|
*/
|
2019-02-07 12:25:57 -05:00
|
|
|
public function testInvalidCode(
|
2022-01-13 13:49:37 -05:00
|
|
|
string $code,
|
|
|
|
string $error_message,
|
|
|
|
array $error_levels = [],
|
2023-04-10 02:03:13 +02:00
|
|
|
string $php_version = '7.4'
|
2021-11-26 20:59:41 +01:00
|
|
|
): void {
|
2019-01-21 18:13:17 -05:00
|
|
|
$test_name = $this->getTestName();
|
2022-01-04 11:40:55 +02:00
|
|
|
if (strpos($test_name, 'PHP80-') !== false) {
|
2020-10-12 11:35:14 -04:00
|
|
|
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
|
|
|
|
$this->markTestSkipped('Test case requires PHP 8.0.');
|
2019-01-21 18:01:15 -05:00
|
|
|
}
|
|
|
|
} elseif (strpos($test_name, 'SKIPPED-') !== false) {
|
|
|
|
$this->markTestSkipped('Skipped due to a bug.');
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
2023-03-03 02:50:11 -04:00
|
|
|
|
|
|
|
// sanity check - do we have a PHP tag?
|
|
|
|
if (strpos($code, '<?php') === false) {
|
|
|
|
$this->fail('Test case must have a <?php tag');
|
|
|
|
}
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2021-12-03 21:25:22 +01:00
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
2021-12-03 21:07:25 +01:00
|
|
|
$code = str_replace("\n", "\r\n", $code);
|
2020-03-10 12:52:18 -04:00
|
|
|
}
|
|
|
|
|
2022-01-13 14:38:17 -05:00
|
|
|
foreach ($error_levels as $error_level) {
|
|
|
|
$issue_name = $error_level;
|
|
|
|
$error_level = Config::REPORT_SUPPRESS;
|
2018-11-01 17:03:08 -04:00
|
|
|
|
|
|
|
Config::getInstance()->setCustomErrorLevel($issue_name, $error_level);
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:06:33 +02:00
|
|
|
$this->project_analyzer->setPhpVersion($php_version, 'tests');
|
2019-02-07 12:25:57 -05:00
|
|
|
|
2020-08-15 14:21:24 -04:00
|
|
|
$file_path = self::$src_dir_path . 'somefile.php';
|
|
|
|
|
2020-08-22 17:00:36 +03:00
|
|
|
// $error_message = preg_replace('/ src[\/\\\\]somefile\.php/', ' src/somefile.php', $error_message);
|
2019-03-25 23:10:57 +00:00
|
|
|
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2020-04-03 14:56:11 -04:00
|
|
|
|
2022-01-19 19:29:16 +01:00
|
|
|
$this->expectExceptionMessageMatches('/\b' . preg_quote($error_message, '/') . '\b/');
|
2017-04-24 23:45:02 -04:00
|
|
|
|
2020-10-29 19:41:10 -04:00
|
|
|
$codebase = $this->project_analyzer->getCodebase();
|
2023-02-19 19:51:51 +13:00
|
|
|
$codebase->enterServerMode();
|
2020-10-29 19:41:10 -04:00
|
|
|
$codebase->config->visitPreloadedStubFiles($codebase);
|
|
|
|
|
2018-01-21 13:38:51 -05:00
|
|
|
$this->addFile($file_path, $code);
|
2018-01-21 10:22:04 -05:00
|
|
|
$this->analyzeFile($file_path, new Context());
|
2017-04-24 23:45:02 -04:00
|
|
|
}
|
|
|
|
}
|