1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

qa: restructure UnusedVariableTest

This aligns with most recent tests which do consume `InvalidCodeAnalysisTestTrait` and `ValidCodeAnalysisTestTrait`.

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
This commit is contained in:
Maximilian Bösing 2023-04-10 00:53:33 +02:00
parent 763116fab3
commit 803786e44e
No known key found for this signature in database
GPG Key ID: 9A8988C93CEC81A3

View File

@ -2,97 +2,22 @@
namespace Psalm\Tests;
use Psalm\Config;
use Psalm\Context;
use Psalm\Exception\CodeException;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
use function preg_quote;
use function strpos;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
use const DIRECTORY_SEPARATOR;
class UnusedVariableTest extends TestCase
{
protected ProjectAnalyzer $project_analyzer;
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
public function setUp(): void
{
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
$this->project_analyzer = new ProjectAnalyzer(
new TestConfig(),
new Providers(
$this->file_provider,
new FakeParserCacheProvider(),
),
);
parent::setUp();
$this->project_analyzer->getCodebase()->reportUnusedVariables();
}
/**
* @dataProvider providerValidCodeParse
* @param array<string> $ignored_issues
*/
public function testValidCode(string $code, array $ignored_issues = [], string $php_version = '7.4'): void
{
$test_name = $this->getTestName();
if (strpos($test_name, 'SKIPPED-') !== false) {
$this->markTestSkipped('Skipped due to a bug.');
}
$this->project_analyzer->setPhpVersion($php_version, 'tests');
$file_path = self::$src_dir_path . 'somefile.php';
$this->addFile(
$file_path,
$code,
);
foreach ($ignored_issues as $error_level) {
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
}
$this->analyzeFile($file_path, new Context());
}
/**
* @dataProvider providerInvalidCodeParse
* @param array<string> $ignored_issues
*/
public function testInvalidCode(string $code, string $error_message, array $ignored_issues = []): void
{
if (strpos($this->getTestName(), 'SKIPPED-') !== false) {
$this->markTestSkipped();
}
$this->expectException(CodeException::class);
$this->expectExceptionMessageMatches('/\b' . preg_quote($error_message, '/') . '\b/');
$this->project_analyzer->setPhpVersion('7.4', 'tests');
$file_path = self::$src_dir_path . 'somefile.php';
foreach ($ignored_issues as $error_level) {
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
}
$this->addFile(
$file_path,
$code,
);
$this->analyzeFile($file_path, new Context());
}
/**
* @return array<string, array{code:string,ignored_issues?:list<string>,php_version?:string}>
*/
@ -137,6 +62,7 @@ class UnusedVariableTest extends TestCase
$f = new $d($e);
return $a . implode(",", $b) . $c[0] . get_class($f);
}',
'assertions' => [],
'ignored_issues' => [
'PossiblyUndefinedVariable',
'MixedArrayAccess',
@ -539,6 +465,7 @@ class UnusedVariableTest extends TestCase
return $ret;
}',
'assertions' => [],
'ignored_issues' => [
'MixedAssignment',
'MixedMethodCall',
@ -2410,6 +2337,7 @@ class UnusedVariableTest extends TestCase
}
}
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
],