Mercurial > hg > isophonics-drupal-site
comparison core/modules/simpletest/tests/src/Unit/TestDiscoveryTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\simpletest\Unit; | |
4 | |
5 use Composer\Autoload\ClassLoader; | |
6 use Drupal\Core\Extension\Extension; | |
7 use Drupal\Core\Extension\ModuleHandlerInterface; | |
8 use Drupal\simpletest\Exception\MissingGroupException; | |
9 use Drupal\simpletest\TestDiscovery; | |
10 use Drupal\Tests\UnitTestCase; | |
11 use org\bovigo\vfs\vfsStream; | |
12 | |
13 /** | |
14 * @coversDefaultClass \Drupal\simpletest\TestDiscovery | |
15 * @group simpletest | |
16 */ | |
17 class TestDiscoveryTest extends UnitTestCase { | |
18 | |
19 /** | |
20 * @covers ::getTestInfo | |
21 * @dataProvider infoParserProvider | |
22 */ | |
23 public function testTestInfoParser($expected, $classname, $doc_comment = NULL) { | |
24 $info = TestDiscovery::getTestInfo($classname, $doc_comment); | |
25 $this->assertEquals($expected, $info); | |
26 } | |
27 | |
28 public function infoParserProvider() { | |
29 // A module provided unit test. | |
30 $tests[] = [ | |
31 // Expected result. | |
32 [ | |
33 'name' => 'Drupal\Tests\simpletest\Unit\TestDiscoveryTest', | |
34 'group' => 'simpletest', | |
35 'description' => 'Tests \Drupal\simpletest\TestDiscovery.', | |
36 'type' => 'PHPUnit-Unit', | |
37 ], | |
38 // Classname. | |
39 'Drupal\Tests\simpletest\Unit\TestDiscoveryTest', | |
40 ]; | |
41 | |
42 // A core unit test. | |
43 $tests[] = [ | |
44 // Expected result. | |
45 [ | |
46 'name' => 'Drupal\Tests\Core\DrupalTest', | |
47 'group' => 'DrupalTest', | |
48 'description' => 'Tests \Drupal.', | |
49 'type' => 'PHPUnit-Unit', | |
50 ], | |
51 // Classname. | |
52 'Drupal\Tests\Core\DrupalTest', | |
53 ]; | |
54 | |
55 // Functional PHPUnit test. | |
56 $tests[] = [ | |
57 // Expected result. | |
58 [ | |
59 'name' => 'Drupal\FunctionalTests\BrowserTestBaseTest', | |
60 'group' => 'browsertestbase', | |
61 'description' => 'Tests BrowserTestBase functionality.', | |
62 'type' => 'PHPUnit-Functional', | |
63 ], | |
64 // Classname. | |
65 'Drupal\FunctionalTests\BrowserTestBaseTest', | |
66 ]; | |
67 | |
68 // kernel PHPUnit test. | |
69 $tests['phpunit-kernel'] = [ | |
70 // Expected result. | |
71 [ | |
72 'name' => '\Drupal\Tests\file\Kernel\FileItemValidationTest', | |
73 'group' => 'file', | |
74 'description' => 'Tests that files referenced in file and image fields are always validated.', | |
75 'type' => 'PHPUnit-Kernel', | |
76 ], | |
77 // Classname. | |
78 '\Drupal\Tests\file\Kernel\FileItemValidationTest', | |
79 ]; | |
80 | |
81 // Simpletest classes can not be autoloaded in a PHPUnit test, therefore | |
82 // provide a docblock. | |
83 $tests[] = [ | |
84 // Expected result. | |
85 [ | |
86 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
87 'group' => 'simpletest', | |
88 'description' => 'Tests the Simpletest UI internal browser.', | |
89 'type' => 'Simpletest', | |
90 ], | |
91 // Classname. | |
92 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
93 // Doc block. | |
94 "/** | |
95 * Tests the Simpletest UI internal browser. | |
96 * | |
97 * @group simpletest | |
98 */ | |
99 ", | |
100 ]; | |
101 | |
102 // Test with a different amount of leading spaces. | |
103 $tests[] = [ | |
104 // Expected result. | |
105 [ | |
106 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
107 'group' => 'simpletest', | |
108 'description' => 'Tests the Simpletest UI internal browser.', | |
109 'type' => 'Simpletest', | |
110 ], | |
111 // Classname. | |
112 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
113 // Doc block. | |
114 "/** | |
115 * Tests the Simpletest UI internal browser. | |
116 * | |
117 * @group simpletest | |
118 */ | |
119 */ | |
120 ", | |
121 ]; | |
122 | |
123 // Make sure that a "* @" inside a string does not get parsed as an | |
124 // annotation. | |
125 $tests[] = [ | |
126 // Expected result. | |
127 [ | |
128 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
129 'group' => 'simpletest', | |
130 'description' => 'Tests the Simpletest UI internal browser. * @', | |
131 'type' => 'Simpletest', | |
132 ], | |
133 // Classname. | |
134 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
135 // Doc block. | |
136 "/** | |
137 * Tests the Simpletest UI internal browser. * @ | |
138 * | |
139 * @group simpletest | |
140 */ | |
141 ", | |
142 ]; | |
143 | |
144 // Multiple @group annotations. | |
145 $tests[] = [ | |
146 // Expected result. | |
147 [ | |
148 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
149 'group' => 'Test', | |
150 'description' => 'Tests the Simpletest UI internal browser.', | |
151 'type' => 'Simpletest', | |
152 ], | |
153 // Classname. | |
154 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
155 // Doc block. | |
156 "/** | |
157 * Tests the Simpletest UI internal browser. | |
158 * | |
159 * @group Test | |
160 * @group simpletest | |
161 */ | |
162 ", | |
163 ]; | |
164 | |
165 // @dependencies annotation. | |
166 $tests[] = [ | |
167 // Expected result. | |
168 [ | |
169 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
170 'description' => 'Tests the Simpletest UI internal browser.', | |
171 'type' => 'Simpletest', | |
172 'requires' => ['module' => ['test']], | |
173 'group' => 'simpletest', | |
174 ], | |
175 // Classname. | |
176 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
177 // Doc block. | |
178 "/** | |
179 * Tests the Simpletest UI internal browser. | |
180 * | |
181 * @dependencies test | |
182 * @group simpletest | |
183 */ | |
184 ", | |
185 ]; | |
186 | |
187 // Multiple @dependencies annotation. | |
188 $tests[] = [ | |
189 // Expected result. | |
190 [ | |
191 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
192 'description' => 'Tests the Simpletest UI internal browser.', | |
193 'type' => 'Simpletest', | |
194 'requires' => ['module' => ['test', 'test1', 'test2']], | |
195 'group' => 'simpletest', | |
196 ], | |
197 // Classname. | |
198 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
199 // Doc block. | |
200 "/** | |
201 * Tests the Simpletest UI internal browser. | |
202 * | |
203 * @dependencies test, test1, test2 | |
204 * @group simpletest | |
205 */ | |
206 ", | |
207 ]; | |
208 | |
209 // Multi-line summary line. | |
210 $tests[] = [ | |
211 // Expected result. | |
212 [ | |
213 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
214 'description' => 'Tests the Simpletest UI internal browser. And the summary line continues an there is no gap to the annotation.', | |
215 'type' => 'Simpletest', | |
216 'group' => 'simpletest', | |
217 ], | |
218 // Classname. | |
219 'Drupal\simpletest\Tests\ExampleSimpleTest', | |
220 // Doc block. | |
221 "/** | |
222 * Tests the Simpletest UI internal browser. And the summary line continues an | |
223 * there is no gap to the annotation. | |
224 * | |
225 * @group simpletest | |
226 */ | |
227 ", | |
228 ]; | |
229 return $tests; | |
230 } | |
231 | |
232 /** | |
233 * @covers ::getTestInfo | |
234 */ | |
235 public function testTestInfoParserMissingGroup() { | |
236 $classname = 'Drupal\KernelTests\field\BulkDeleteTest'; | |
237 $doc_comment = <<<EOT | |
238 /** | |
239 * Bulk delete storages and fields, and clean up afterwards. | |
240 */ | |
241 EOT; | |
242 $this->setExpectedException(MissingGroupException::class, 'Missing @group annotation in Drupal\KernelTests\field\BulkDeleteTest'); | |
243 TestDiscovery::getTestInfo($classname, $doc_comment); | |
244 } | |
245 | |
246 /** | |
247 * @covers ::getTestInfo | |
248 */ | |
249 public function testTestInfoParserMissingSummary() { | |
250 $classname = 'Drupal\KernelTests\field\BulkDeleteTest'; | |
251 $doc_comment = <<<EOT | |
252 /** | |
253 * @group field | |
254 */ | |
255 EOT; | |
256 $info = TestDiscovery::getTestInfo($classname, $doc_comment); | |
257 $this->assertEmpty($info['description']); | |
258 } | |
259 | |
260 protected function setupVfsWithTestClasses() { | |
261 vfsStream::setup('drupal'); | |
262 | |
263 $test_file = <<<EOF | |
264 <?php | |
265 | |
266 /** | |
267 * Test description | |
268 * @group example | |
269 */ | |
270 class FunctionalExampleTest {} | |
271 EOF; | |
272 | |
273 vfsStream::create([ | |
274 'modules' => [ | |
275 'test_module' => [ | |
276 'tests' => [ | |
277 'src' => [ | |
278 'Functional' => [ | |
279 'FunctionalExampleTest.php' => $test_file, | |
280 'FunctionalExampleTest2.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest2', '@group example2'], $test_file), | |
281 ], | |
282 'Kernel' => [ | |
283 'KernelExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['KernelExampleTest3', '@group example2'], $test_file), | |
284 ], | |
285 ], | |
286 ], | |
287 ], | |
288 ], | |
289 ]); | |
290 } | |
291 | |
292 /** | |
293 * @covers ::getTestClasses | |
294 */ | |
295 public function testGetTestClasses() { | |
296 $this->setupVfsWithTestClasses(); | |
297 $class_loader = $this->prophesize(ClassLoader::class); | |
298 $module_handler = $this->prophesize(ModuleHandlerInterface::class); | |
299 | |
300 $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal()); | |
301 | |
302 $extensions = [ | |
303 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'), | |
304 ]; | |
305 $test_discovery->setExtensions($extensions); | |
306 $result = $test_discovery->getTestClasses(); | |
307 $this->assertCount(2, $result); | |
308 $this->assertEquals([ | |
309 'example' => [ | |
310 'Drupal\Tests\test_module\Functional\FunctionalExampleTest' => [ | |
311 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest', | |
312 'description' => 'Test description', | |
313 'group' => 'example', | |
314 'type' => 'PHPUnit-Functional', | |
315 ], | |
316 ], | |
317 'example2' => [ | |
318 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2' => [ | |
319 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2', | |
320 'description' => 'Test description', | |
321 'group' => 'example2', | |
322 'type' => 'PHPUnit-Functional', | |
323 ], | |
324 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [ | |
325 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3', | |
326 'description' => 'Test description', | |
327 'group' => 'example2', | |
328 'type' => 'PHPUnit-Kernel', | |
329 ], | |
330 ], | |
331 ], $result); | |
332 } | |
333 | |
334 /** | |
335 * @covers ::getTestClasses | |
336 */ | |
337 public function testGetTestClassesWithSelectedTypes() { | |
338 $this->setupVfsWithTestClasses(); | |
339 $class_loader = $this->prophesize(ClassLoader::class); | |
340 $module_handler = $this->prophesize(ModuleHandlerInterface::class); | |
341 | |
342 $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal()); | |
343 | |
344 $extensions = [ | |
345 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'), | |
346 ]; | |
347 $test_discovery->setExtensions($extensions); | |
348 $result = $test_discovery->getTestClasses(NULL, ['PHPUnit-Kernel']); | |
349 $this->assertCount(2, $result); | |
350 $this->assertEquals([ | |
351 'example' => [], | |
352 'example2' => [ | |
353 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [ | |
354 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3', | |
355 'description' => 'Test description', | |
356 'group' => 'example2', | |
357 'type' => 'PHPUnit-Kernel', | |
358 ], | |
359 ], | |
360 ], $result); | |
361 } | |
362 | |
363 /** | |
364 * @covers ::getPhpunitTestSuite | |
365 * @dataProvider providerTestGetPhpunitTestSuite | |
366 */ | |
367 public function testGetPhpunitTestSuite($classname, $expected) { | |
368 $this->assertEquals($expected, TestDiscovery::getPhpunitTestSuite($classname)); | |
369 } | |
370 | |
371 public function providerTestGetPhpunitTestSuite() { | |
372 $data = []; | |
373 $data['simpletest-webtest'] = ['\Drupal\rest\Tests\NodeTest', FALSE]; | |
374 $data['simpletest-kerneltest'] = ['\Drupal\hal\Tests\FileNormalizeTest', FALSE]; | |
375 $data['module-unittest'] = [static::class, 'Unit']; | |
376 $data['module-kerneltest'] = ['\Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest', 'Kernel']; | |
377 $data['module-functionaltest'] = ['\Drupal\FunctionalTests\BrowserTestBaseTest', 'Functional']; | |
378 $data['module-functionaljavascripttest'] = ['\Drupal\Tests\toolbar\FunctionalJavascript\ToolbarIntegrationTest', 'FunctionalJavascript']; | |
379 $data['core-unittest'] = ['\Drupal\Tests\ComposerIntegrationTest', 'Unit']; | |
380 $data['core-unittest2'] = ['Drupal\Tests\Core\DrupalTest', 'Unit']; | |
381 $data['core-kerneltest'] = ['\Drupal\KernelTests\KernelTestBaseTest', 'Kernel']; | |
382 $data['core-functionaltest'] = ['\Drupal\FunctionalTests\ExampleTest', 'Functional']; | |
383 $data['core-functionaljavascripttest'] = ['\Drupal\FunctionalJavascriptTests\ExampleTest', 'FunctionalJavascript']; | |
384 | |
385 return $data; | |
386 } | |
387 | |
388 } | |
389 | |
390 class TestTestDiscovery extends TestDiscovery { | |
391 | |
392 /** | |
393 * @var \Drupal\Core\Extension\Extension[] | |
394 */ | |
395 protected $extensions = []; | |
396 | |
397 public function setExtensions(array $extensions) { | |
398 $this->extensions = $extensions; | |
399 } | |
400 | |
401 /** | |
402 * {@inheritdoc} | |
403 */ | |
404 protected function getExtensions() { | |
405 return $this->extensions; | |
406 } | |
407 | |
408 } | |
409 | |
410 namespace Drupal\simpletest\Tests; | |
411 | |
412 use Drupal\simpletest\WebTestBase; | |
413 | |
414 /** | |
415 * Tests the Simpletest UI internal browser. | |
416 * | |
417 * @group simpletest | |
418 */ | |
419 class ExampleSimpleTest extends WebTestBase { | |
420 } |