-
Notifications
You must be signed in to change notification settings - Fork 51
/
AcceptanceTestCase.php
290 lines (244 loc) · 7.32 KB
/
AcceptanceTestCase.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace tests\PHPCTags\Acceptance;
use Exception;
use PHPUnit_Framework_TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
abstract class AcceptanceTestCase extends PHPUnit_Framework_TestCase
{
const FORMAT = "<name>\t<file>\t/^<line content>$/;\"\t<short kind>\tline:<line number>\t<scope>\t<access>";
const KIND_TRAIT = 't';
const KIND_CLASS = 'c';
const KIND_METHOD = 'm';
const KIND_FUNCTION = 'f';
const KIND_PROPERTY = 'p';
const KIND_CONSTANT = 'd';
const KIND_VARIABLE = 'v';
const KIND_INTERFACE = 'i';
const KIND_NAMESPACE = 'n';
/**
* @var string
*/
private $testDir;
/**
* @var string
*/
private $tagsFileContent;
protected function setUp()
{
$this->testDir = __DIR__ . '/../../.test_fs';
if (!file_exists($this->testDir)) {
mkdir($this->testDir);
}
$this->testDir = realpath($this->testDir);
$this->emptyTestDir();
}
/**
* @param string $filename
* @param string $content
*
* @return void
*/
protected function givenSourceFile($filename, $content)
{
$filename = $this->testDir . DIRECTORY_SEPARATOR . $filename;
file_put_contents($filename, $content);
}
protected function givenDirectory($dirname)
{
$dirname = $this->testDir . DIRECTORY_SEPARATOR . $dirname;
mkdir($dirname);
}
protected function givenMode($file, $mode)
{
$file = $this->testDir . DIRECTORY_SEPARATOR . $file;
chmod($file, $mode);
}
/**
* @return void
*/
protected function runPHPCtags(array $params = array())
{
$entryPoint = realpath(__DIR__ . '/../../bootstrap.php');
$params = implode(' ', $params);
exec("php \"$entryPoint\" --recurse=yes $params -f - {$this->testDir}", $output);
$this->tagsFileContent = $output;
}
protected function runPHPCtagsWithKinds($kindString)
{
$this->runPHPCtags(array("--kinds=$kindString"));
}
/**
* @param string $patterns
*
* @return void
*/
protected function runPHPCtagsWithExcludes(array $patterns)
{
$entryPoint = realpath(__DIR__ . '/../../bootstrap.php');
$excludes = implode(
' ',
array_map(function ($pattern) {
return '--exclude=\'' . $pattern . '\'';
}, $patterns)
);
exec("php \"$entryPoint\" --recurse=yes -f - $excludes {$this->testDir}", $output);
$this->tagsFileContent = $output;
}
/**
* @return void
*/
protected function assertTagsFileHeaderIsCorrect()
{
$expectedHeader = array(
"!_TAG_FILE_FORMAT\t2\t/extended format; --format=1 will not append ;\" to lines/",
"!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/",
"!_TAG_PROGRAM_AUTHOR\ttechlivezheng\t/techlivezheng@gmail.com/",
"!_TAG_PROGRAM_NAME\tphpctags\t//",
"!_TAG_PROGRAM_URL\thttps://github.com/techlivezheng/phpctags\t/official site/",
"!_TAG_PROGRAM_VERSION\t0.6.0\t//",
);
$realHeader = array_splice($this->tagsFileContent, 0, count($expectedHeader));
$this->assertEquals($expectedHeader, $realHeader, 'Tags file header is incorrect');
}
/**
* @return void
*/
protected function assertNumberOfTagsInTagsFileIs($count)
{
$this->assertCount(
$count,
$this->tagsFileContent,
'Tags file contains the wrong number of tags'
);
}
/**
* @param string $filename
* @param string $name
* @param string $kind
* @param int $line
* @param string $scope
* @param string $access
*
* @return void
*/
protected function assertTagsFileContainsTag(
$filename,
$name,
$kind,
$line,
$scope = '',
$access = ''
) {
$this->assertContains(
$this->createTagLine($filename, $name, $kind, $line, $scope, $access),
$this->tagsFileContent,
"Tag file content:\n" . print_r($this->tagsFileContent, true)
);
}
/**
* @param string $filename
*
* @return void
*/
public function assertTagsFileContainsNoTagsFromFile($filename)
{
$filename = $this->testDir . DIRECTORY_SEPARATOR . $filename;
$tags = array_filter(
$this->tagsFileContent,
function ($line) use ($filename) {
$fields = explode("\t", $line);
return $fields[1] === $filename;
}
);
$this->assertEmpty($tags, "Tags for $filename were found in tag file.");
}
/**
* @return void
*/
private function emptyTestDir()
{
if (empty($this->testDir)) {
throw \RuntimeException('Test directory does not exist');
}
foreach (glob($this->testDir . DIRECTORY_SEPARATOR . '*') as $file) {
chmod($file, 0755);
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$this->testDir,
RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
if ($fileinfo->isDir()) {
rmdir($fileinfo->getRealPath());
} else {
unlink($fileinfo->getRealPath());
}
}
}
/**
* @param string $filename
* @param string $name
* @param string $kind
* @param int $line
* @param string $scope
* @param string $access
*
* @return string
*/
private function createTagLine($filename, $name, $kind, $line, $scope, $access)
{
$kinds = array(
self::KIND_TRAIT => 'trait',
self::KIND_CLASS => 'class',
self::KIND_METHOD => 'method',
self::KIND_FUNCTION => 'function',
self::KIND_PROPERTY => 'property',
self::KIND_CONSTANT => 'constant',
self::KIND_VARIABLE => 'variable',
self::KIND_INTERFACE => 'interface',
self::KIND_NAMESPACE => 'namespace',
);
if(!empty($access)) {
$access = 'access:' . $access;
}
$patterns = array(
'/<name>/',
'/<file>/',
'/<line content>/',
'/<short kind>/',
'/<full kind>/',
'/<line number>/',
'/<scope>/',
'/<access>/'
);
$replacements = array(
$name,
$this->testDir . DIRECTORY_SEPARATOR . $filename,
$this->getSourceFileLineContent($filename, $line),
$kind,
$kinds[$kind],
$line,
$scope,
$access
);
$line = preg_replace($patterns, $replacements, self::FORMAT);
return rtrim($line, "\t");
}
/**
* @param string $filename
* @param int $line
*
* @return string
*/
private function getSourceFileLineContent($filename, $line)
{
$filename = $this->testDir . DIRECTORY_SEPARATOR . $filename;
$line--;
$file = file($filename);
return rtrim($file[$line], PHP_EOL);
}
}