-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRunner.php
More file actions
260 lines (225 loc) · 5.96 KB
/
TestRunner.php
File metadata and controls
260 lines (225 loc) · 5.96 KB
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
<?php
/**
*
* EPV :: The phpBB Forum Extension Pre Validator.
*
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace Phpbb\Epv\Tests;
use Phpbb\Epv\Files\FileInterface;
use Phpbb\Epv\Files\FileLoader;
use Phpbb\Epv\Files\Line;
use Phpbb\Epv\Output\Output;
use Phpbb\Epv\Output\OutputInterface;
use Phpbb\Epv\Tests\Exception\TestException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class TestRunner
{
/** @var array */
public $tests = array();
/** @var array */
private $files = array();
/** @var array */
private $dirList = array();
private $output;
private $directory;
private $debug;
private $basedir;
private $namespace;
private $titania;
/**
* @param OutputInterface $output
* @param string $directory The directory where the extension is located
* @param boolean $debug Debug mode
* @param boolean $isTitania If we run from titania or not
*/
public function __construct(OutputInterface $output, $directory, $debug, $isTitania = false)
{
$this->output = $output;
$this->directory = realpath($directory);
$this->debug = $debug;
$this->titania = $isTitania;
$this->setBasedir();
$this->loadTests();
$this->loadFiles();
}
/**
* @param string $resource
* @return bool|string
*/
public static function getResource($resource)
{
return realpath(__DIR__. '/../Resources/' . $resource);
}
/**
* Run the actual test suite.
*
* @throws Exception\TestException
*/
public function runTests()
{
if (count($this->tests) == 0)
{
throw new TestException("TestRunner not initialised.");
}
$this->output->writeln("Running tests.");
// Now, we basicly do the same as above, but we do really run the tests.
// All other tests are specific to files.
/** @var \Phpbb\Epv\Tests\TestInterface $test */
foreach ($this->tests as $test)
{
if ($test->doValidateDirectory())
{
$test->validateDirectory($this->dirList);
}
}
// Time to loop over the files in memory,
// And over the tests that are available.
// First do the full file check.
// After that loop over each line and test per line.
/** @var FileInterface $file */
foreach ($this->files as $file)
{
$linetest = array();
/** @var \Phpbb\Epv\Tests\TestInterface $test */
foreach ($this->tests as $test)
{
if ($test->doValidateFile($file->getFileType()))
{
$test->validateFile($file);
}
// To prevent looping over too many tests, we check here if we need to loop
// over tests for line by line tests.
if ($test->doValidateLine($file->getFileType()))
{
$linetest[] = $test;
}
}
if (count($linetest))
{
$linenr = 1;
foreach ($file->getLines() as $line)
{
$runline = new Line($file, $linenr, $line);
foreach ($linetest as $test)
{
$test->validateLine($runline);
}
$linenr++;
}
}
}
}
/**
* Set the base directory for the extension.
* @throws Exception\TestException
*/
private function setBasedir()
{
$finder = new Finder();
// First find composer.json.
// composer.json is required, so it should always be there.
// We use it to find the base directory of all files.
$iterator = $finder
->files()
->name('composer.json')
->exclude(['vendor', 'tests'])
->in($this->directory);
$iteratorCount = count($iterator);
if ($iteratorCount !== 1)
{
throw new TestException($iteratorCount === 0 ? 'Could not find the required composer.json file.' : 'Found multiple composer.json files.');
}
$composer = '';
foreach ($iterator as $file)
{
$composer = $file;
}
if (empty($composer))
{
throw new TestException('Iterator did result a empty filename');
}
$this->basedir = str_replace('composer.json', '', $composer);
$composer = @json_decode(@file_get_contents($composer));
if (!$composer)
{
throw new TestException('composer.json is unreadable or invalid json');
}
$this->namespace = $composer->name ?? '';
}
/**
* Load all files from the extension.
*/
private function loadFiles()
{
$finder = new Finder();
$iterator = $finder
->ignoreDotFiles(false)
->files()
->sortByName()
//->name('*')
->ignoreVCS(true)
->exclude('vendor')
->exclude('tests')
->exclude('travis')
->in($this->directory);
$loader = new FileLoader($this->output, $this->debug, $this->basedir, $this->directory);
foreach ($iterator as $file)
{
/** @var \Symfony\Component\Finder\SplFileInfo $file */
if (!$file->getRealPath())
{
$this->output->write("<info>Finder found a file, but it does not seem to be readable or does not actually exist.</info>");
continue;
}
$loadedFile = $loader->loadFile($file->getRealPath());
if ($loadedFile != null)
{
$this->files[] = $loadedFile;
$this->dirList[] = $file->getRealPath();
}
else
{
$this->output->addMessage(Output::FATAL, "Unable to load file: " . $file->getRealPath());
}
}
}
/**
* Load all available tests.
*/
private function loadTests()
{
$finder = new Finder();
$iterator = $finder
->files()
->name('epv_test_*.php')
->size(">= 0K")
->in(__DIR__ . '/Tests');
foreach ($iterator as $test)
{
$this->tryToLoadTest($test);
}
}
/**
* Try to load and initialise a specific test.
*
* @param SplFileInfo $test
*
* @throws Exception\TestException
*/
private function tryToLoadTest(SplFileInfo $test)
{
$this->output->writelnIfDebug("<info>Found {$test->getRealpath()}.</info>");
$file = str_replace('.php', '', basename($test->getRealPath()));
$class = '\\Phpbb\Epv\\Tests\\Tests\\' . $file;
$filetest = new $class($this->debug, $this->output, $this->basedir, $this->namespace, $this->titania, $this->directory);
if (!$filetest instanceof TestInterface)
{
throw new TestException("$class does not implement the TestInterface, but matches the test expression.");
}
$this->tests[] = $filetest;
}
}