-
Notifications
You must be signed in to change notification settings - Fork 15
/
BaseTestCase.php
81 lines (69 loc) · 2.48 KB
/
BaseTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace VariableAnalysis\Tests;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Config;
class BaseTestCase extends TestCase
{
const STANDARD_NAME = 'VariableAnalysis';
const REDECLARATION_ERROR_CODE = 'VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration';
const SELF_OUTSIDE_CLASS_ERROR_CODE = 'VariableAnalysis.CodeAnalysis.VariableAnalysis.SelfOutsideClass';
const STATIC_OUSIDE_CLASS_ERROR_CODE = 'VariableAnalysis.CodeAnalysis.VariableAnalysis.StaticOutsideClass';
const UNDEFINED_ERROR_CODE = 'VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable';
const UNSET_ERROR_CODE = 'VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable';
const UNUSED_ERROR_CODE = 'VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable';
public function prepareLocalFileForSniffs($fixtureFile)
{
$sniffFile = __DIR__ . '/../VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php';
$config = new Config();
$config->cache = false;
$config->standards = [self::STANDARD_NAME];
$config->ignored = [];
$sniffFiles = [realpath($sniffFile)];
$ruleset = new Ruleset($config);
$ruleset->registerSniffs($sniffFiles, [], []);
$ruleset->populateTokenListeners();
if (! file_exists($fixtureFile)) {
throw new \Exception('Fixture file does not exist: ' . $fixtureFile);
}
return new LocalFile($fixtureFile, $ruleset, $config);
}
public function getLineNumbersFromMessages(array $messages)
{
$lines = array_keys($messages);
sort($lines);
return $lines;
}
public function getWarningLineNumbersFromFile(LocalFile $phpcsFile)
{
return $this->getLineNumbersFromMessages($phpcsFile->getWarnings());
}
public function getErrorLineNumbersFromFile(LocalFile $phpcsFile)
{
return $this->getLineNumbersFromMessages($phpcsFile->getErrors());
}
public function getFixture($fixtureFilename)
{
return realpath(__DIR__ . '/VariableAnalysisSniff/fixtures/' . $fixtureFilename);
}
public function setSniffProperty($phpcsFile, $property, $value)
{
if (version_compare(Config::VERSION, '3.8.0', '>=')) {
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
$property,
[
'scope' => 'sniff',
'value' => $value,
]
);
} else {
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
$property,
$value
);
}
}
}