-
Notifications
You must be signed in to change notification settings - Fork 653
/
TestCase.php
197 lines (159 loc) · 5.5 KB
/
TestCase.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Psalm\Tests;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Psalm\Config;
use Psalm\Context;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\FakeFileProvider;
use Psalm\Internal\Provider\Providers;
use Psalm\Internal\RuntimeCaches;
use Psalm\Internal\Type\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\IssueBuffer;
use Psalm\Tests\Internal\Provider\FakeParserCacheProvider;
use Psalm\Type\Union;
use Throwable;
use function array_filter;
use function count;
use function define;
use function defined;
use function getcwd;
use function ini_set;
use function is_string;
use const ARRAY_FILTER_USE_KEY;
use const DIRECTORY_SEPARATOR;
class TestCase extends BaseTestCase
{
/** @var string */
protected static $src_dir_path;
/** @var ProjectAnalyzer */
protected $project_analyzer;
/** @var FakeFileProvider */
protected $file_provider;
/** @var Config */
protected $testConfig;
public static function setUpBeforeClass(): void
{
ini_set('memory_limit', '-1');
if (!defined('PSALM_VERSION')) {
define('PSALM_VERSION', '4.0.0');
}
if (!defined('PHP_PARSER_VERSION')) {
define('PHP_PARSER_VERSION', '4.0.0');
}
parent::setUpBeforeClass();
self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;
}
protected function makeConfig(): Config
{
return new TestConfig();
}
public function setUp(): void
{
parent::setUp();
RuntimeCaches::clearAll();
$this->file_provider = new FakeFileProvider();
$this->testConfig = $this->makeConfig();
$providers = new Providers(
$this->file_provider,
new FakeParserCacheProvider()
);
$this->project_analyzer = new ProjectAnalyzer(
$this->testConfig,
$providers
);
$this->project_analyzer->setPhpVersion('7.4', 'tests');
}
public function tearDown(): void
{
unset($this->project_analyzer, $this->file_provider, $this->testConfig);
RuntimeCaches::clearAll();
}
/**
* @param string $file_path
* @param string $contents
*
*/
public function addFile($file_path, $contents): void
{
$this->file_provider->registerFile($file_path, $contents);
$this->project_analyzer->getCodebase()->scanner->addFileToShallowScan($file_path);
}
/**
* @param string $file_path
*
*/
public function analyzeFile($file_path, Context $context, bool $track_unused_suppressions = true, bool $taint_flow_tracking = false): void
{
$codebase = $this->project_analyzer->getCodebase();
if ($taint_flow_tracking) {
$this->project_analyzer->trackTaintedInputs();
}
$codebase->addFilesToAnalyze([$file_path => $file_path]);
$codebase->scanFiles();
$codebase->config->visitStubFiles($codebase);
if ($codebase->alter_code) {
$this->project_analyzer->interpretRefactors();
}
$this->project_analyzer->trackUnusedSuppressions();
$file_analyzer = new FileAnalyzer(
$this->project_analyzer,
$file_path,
$codebase->config->shortenFileName($file_path)
);
$file_analyzer->analyze($context);
if ($codebase->taint_flow_graph) {
$codebase->taint_flow_graph->connectSinksAndSources();
}
if ($track_unused_suppressions) {
IssueBuffer::processUnusedSuppressions($codebase->file_provider);
}
}
/**
* @param bool $withDataSet
*
*/
protected function getTestName($withDataSet = true): string
{
return $this->getName($withDataSet);
}
public static function assertArrayKeysAreStrings(array $array, string $message = ''): void
{
$validKeys = array_filter($array, 'is_string', ARRAY_FILTER_USE_KEY);
self::assertTrue(count($array) === count($validKeys), $message);
}
public static function assertArrayKeysAreZeroOrString(array $array, string $message = ''): void
{
$isZeroOrString = /** @param mixed $key */ function ($key): bool {
return $key === 0 || is_string($key);
};
$validKeys = array_filter($array, $isZeroOrString, ARRAY_FILTER_USE_KEY);
self::assertTrue(count($array) === count($validKeys), $message);
}
public static function assertArrayValuesAreArrays(array $array, string $message = ''): void
{
$validValues = array_filter($array, 'is_array');
self::assertTrue(count($array) === count($validValues), $message);
}
public static function assertArrayValuesAreStrings(array $array, string $message = ''): void
{
$validValues = array_filter($array, 'is_string');
self::assertTrue(count($array) === count($validValues), $message);
}
public static function assertStringIsParsableType(string $type, string $message = ''): void
{
if ($type === '') {
// Ignore empty types for now, as these are quite common for pecl libraries
self::assertTrue(true);
} else {
$union = null;
try {
$tokens = TypeTokenizer::tokenize($type);
$union = TypeParser::parseTokens($tokens);
} catch (Throwable $_e) {
}
self::assertInstanceOf(Union::class, $union, $message);
}
}
}