5 * Contains \Drupal\Tests\simpletest\Unit\TestInfoParsingTest.
8 namespace Drupal\Tests\simpletest\Unit;
10 use Composer\Autoload\ClassLoader;
11 use Drupal\Core\Extension\Extension;
12 use Drupal\Core\Extension\ModuleHandlerInterface;
13 use Drupal\simpletest\Exception\MissingGroupException;
14 use Drupal\simpletest\TestDiscovery;
15 use Drupal\Tests\UnitTestCase;
16 use org\bovigo\vfs\vfsStream;
19 * @coversDefaultClass \Drupal\simpletest\TestDiscovery
22 class TestInfoParsingTest extends UnitTestCase {
25 * @covers ::getTestInfo
26 * @dataProvider infoParserProvider
28 public function testTestInfoParser($expected, $classname, $doc_comment = NULL) {
29 $info = TestDiscovery::getTestInfo($classname, $doc_comment);
30 $this->assertEquals($expected, $info);
33 public function infoParserProvider() {
34 // A module provided unit test.
38 'name' => 'Drupal\Tests\simpletest\Unit\TestInfoParsingTest',
39 'group' => 'simpletest',
40 'description' => 'Tests \Drupal\simpletest\TestDiscovery.',
41 'type' => 'PHPUnit-Unit',
44 'Drupal\Tests\simpletest\Unit\TestInfoParsingTest',
51 'name' => 'Drupal\Tests\Core\DrupalTest',
52 'group' => 'DrupalTest',
53 'description' => 'Tests \Drupal.',
54 'type' => 'PHPUnit-Unit',
57 'Drupal\Tests\Core\DrupalTest',
60 // Functional PHPUnit test.
64 'name' => 'Drupal\FunctionalTests\BrowserTestBaseTest',
65 'group' => 'browsertestbase',
66 'description' => 'Tests BrowserTestBase functionality.',
67 'type' => 'PHPUnit-Functional',
70 'Drupal\FunctionalTests\BrowserTestBaseTest',
73 // kernel PHPUnit test.
74 $tests['phpunit-kernel'] = [
77 'name' => '\Drupal\Tests\file\Kernel\FileItemValidationTest',
79 'description' => 'Tests that files referenced in file and image fields are always validated.',
80 'type' => 'PHPUnit-Kernel',
83 '\Drupal\Tests\file\Kernel\FileItemValidationTest',
86 // Simpletest classes can not be autoloaded in a PHPUnit test, therefore
87 // provide a docblock.
91 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
92 'group' => 'simpletest',
93 'description' => 'Tests the Simpletest UI internal browser.',
94 'type' => 'Simpletest',
97 'Drupal\simpletest\Tests\ExampleSimpleTest',
100 * Tests the Simpletest UI internal browser.
107 // Test with a different amount of leading spaces.
111 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
112 'group' => 'simpletest',
113 'description' => 'Tests the Simpletest UI internal browser.',
114 'type' => 'Simpletest',
117 'Drupal\simpletest\Tests\ExampleSimpleTest',
120 * Tests the Simpletest UI internal browser.
128 // Make sure that a "* @" inside a string does not get parsed as an
133 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
134 'group' => 'simpletest',
135 'description' => 'Tests the Simpletest UI internal browser. * @',
136 'type' => 'Simpletest',
139 'Drupal\simpletest\Tests\ExampleSimpleTest',
142 * Tests the Simpletest UI internal browser. * @
149 // Multiple @group annotations.
153 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
155 'description' => 'Tests the Simpletest UI internal browser.',
156 'type' => 'Simpletest',
159 'Drupal\simpletest\Tests\ExampleSimpleTest',
162 * Tests the Simpletest UI internal browser.
170 // @dependencies annotation.
174 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
175 'description' => 'Tests the Simpletest UI internal browser.',
176 'type' => 'Simpletest',
177 'requires' => ['module' => ['test']],
178 'group' => 'simpletest',
181 'Drupal\simpletest\Tests\ExampleSimpleTest',
184 * Tests the Simpletest UI internal browser.
192 // Multiple @dependencies annotation.
196 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
197 'description' => 'Tests the Simpletest UI internal browser.',
198 'type' => 'Simpletest',
199 'requires' => ['module' => ['test', 'test1', 'test2']],
200 'group' => 'simpletest',
203 'Drupal\simpletest\Tests\ExampleSimpleTest',
206 * Tests the Simpletest UI internal browser.
208 * @dependencies test, test1, test2
214 // Multi-line summary line.
218 'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
219 'description' => 'Tests the Simpletest UI internal browser. And the summary line continues an there is no gap to the annotation.',
220 'type' => 'Simpletest',
221 'group' => 'simpletest',
224 'Drupal\simpletest\Tests\ExampleSimpleTest',
227 * Tests the Simpletest UI internal browser. And the summary line continues an
228 * there is no gap to the annotation.
238 * @covers ::getTestInfo
240 public function testTestInfoParserMissingGroup() {
241 $classname = 'Drupal\KernelTests\field\BulkDeleteTest';
242 $doc_comment = <<<EOT
244 * Bulk delete storages and fields, and clean up afterwards.
247 $this->setExpectedException(MissingGroupException::class, 'Missing @group annotation in Drupal\KernelTests\field\BulkDeleteTest');
248 TestDiscovery::getTestInfo($classname, $doc_comment);
252 * @covers ::getTestInfo
254 public function testTestInfoParserMissingSummary() {
255 $classname = 'Drupal\KernelTests\field\BulkDeleteTest';
256 $doc_comment = <<<EOT
261 $info = TestDiscovery::getTestInfo($classname, $doc_comment);
262 $this->assertEmpty($info['description']);
265 protected function setupVfsWithTestClasses() {
266 vfsStream::setup('drupal');
275 class FunctionalExampleTest {}
284 'FunctionalExampleTest.php' => $test_file,
285 'FunctionalExampleTest2.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest2', '@group example2'], $test_file),
288 'KernelExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['KernelExampleTest3', '@group example2'], $test_file),
298 * @covers ::getTestClasses
300 public function testGetTestClasses() {
301 $this->setupVfsWithTestClasses();
302 $class_loader = $this->prophesize(ClassLoader::class);
303 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
305 $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal());
308 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
310 $test_discovery->setExtensions($extensions);
311 $result = $test_discovery->getTestClasses();
312 $this->assertCount(2, $result);
313 $this->assertEquals([
315 'Drupal\Tests\test_module\Functional\FunctionalExampleTest' => [
316 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest',
317 'description' => 'Test description',
318 'group' => 'example',
319 'type' => 'PHPUnit-Functional',
323 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2' => [
324 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2',
325 'description' => 'Test description',
326 'group' => 'example2',
327 'type' => 'PHPUnit-Functional',
329 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [
330 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3',
331 'description' => 'Test description',
332 'group' => 'example2',
333 'type' => 'PHPUnit-Kernel',
340 * @covers ::getTestClasses
342 public function testGetTestClassesWithSelectedTypes() {
343 $this->setupVfsWithTestClasses();
344 $class_loader = $this->prophesize(ClassLoader::class);
345 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
347 $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal());
350 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
352 $test_discovery->setExtensions($extensions);
353 $result = $test_discovery->getTestClasses(NULL, ['PHPUnit-Kernel']);
354 $this->assertCount(2, $result);
355 $this->assertEquals([
359 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [
360 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3',
361 'description' => 'Test description',
362 'group' => 'example2',
363 'type' => 'PHPUnit-Kernel',
371 class TestTestDiscovery extends TestDiscovery {
374 * @var \Drupal\Core\Extension\Extension[]
376 protected $extensions = [];
378 public function setExtensions(array $extensions) {
379 $this->extensions = $extensions;
385 protected function getExtensions() {
386 return $this->extensions;
390 * @covers ::getPhpunitTestSuite
391 * @dataProvider providerTestGetPhpunitTestSuite
393 public function testGetPhpunitTestSuite($classname, $expected) {
394 $this->assertEquals($expected, TestDiscovery::getPhpunitTestSuite($classname));
397 public function providerTestGetPhpunitTestSuite() {
399 $data['simpletest-webtest'] = ['\Drupal\rest\Tests\NodeTest', FALSE];
400 $data['simpletest-kerneltest'] = ['\Drupal\hal\Tests\FileNormalizeTest', FALSE];
401 $data['module-unittest'] = [static::class, 'Unit'];
402 $data['module-kerneltest'] = ['\Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest', 'Kernel'];
403 $data['module-functionaltest'] = ['\Drupal\FunctionalTests\BrowserTestBaseTest', 'Functional'];
404 $data['module-functionaljavascripttest'] = ['\Drupal\Tests\toolbar\FunctionalJavascript\ToolbarIntegrationTest', 'FunctionalJavascript'];
405 $data['core-unittest'] = ['\Drupal\Tests\ComposerIntegrationTest', 'Unit'];
406 $data['core-unittest2'] = ['Drupal\Tests\Core\DrupalTest', 'Unit'];
407 $data['core-kerneltest'] = ['\Drupal\KernelTests\KernelTestBaseTest', 'Kernel'];
408 $data['core-functionaltest'] = ['\Drupal\FunctionalTests\ExampleTest', 'Functional'];
409 $data['core-functionaljavascripttest'] = ['\Drupal\FunctionalJavascriptTests\ExampleTest', 'FunctionalJavascript'];
416 namespace Drupal\simpletest\Tests;
418 use Drupal\simpletest\WebTestBase;
421 * Tests the Simpletest UI internal browser.
425 class ExampleSimpleTest extends WebTestBase {