TestInfoParsingTest.php 12.1 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Contains \Drupal\Tests\simpletest\Unit\TestInfoParsingTest.
 */

8
namespace Drupal\Tests\simpletest\Unit {
9

10 11 12
use Composer\Autoload\ClassLoader;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandlerInterface;
13
use Drupal\simpletest\TestDiscovery;
14
use Drupal\Tests\UnitTestCase;
15
use org\bovigo\vfs\vfsStream;
16 17 18 19 20 21 22 23 24 25 26 27

/**
 * @coversDefaultClass \Drupal\simpletest\TestDiscovery
 * @group simpletest
 */
class TestInfoParsingTest extends UnitTestCase {

  /**
   * @covers ::getTestInfo
   * @dataProvider infoParserProvider
   */
  public function testTestInfoParser($expected, $classname, $doc_comment = NULL) {
28
    $info = TestDiscovery::getTestInfo($classname, $doc_comment);
29 30 31 32
    $this->assertEquals($expected, $info);
  }

  public function infoParserProvider() {
33
    // A module provided unit test.
34 35 36 37
    $tests[] = [
      // Expected result.
      [
        'name' => 'Drupal\Tests\simpletest\Unit\TestInfoParsingTest',
38
        'group' => 'simpletest',
39
        'description' => 'Tests \Drupal\simpletest\TestDiscovery.',
40
        'type' => 'PHPUnit-Unit',
41 42 43 44 45
      ],
      // Classname.
      'Drupal\Tests\simpletest\Unit\TestInfoParsingTest',
    ];

46 47 48 49 50
    // A core unit test.
    $tests[] = [
      // Expected result.
      [
        'name' => 'Drupal\Tests\Core\DrupalTest',
51
        'group' => 'DrupalTest',
52
        'description' => 'Tests \Drupal.',
53
        'type' => 'PHPUnit-Unit',
54 55 56 57 58 59 60 61 62 63 64 65
      ],
      // Classname.
      'Drupal\Tests\Core\DrupalTest',
    ];

    // Functional PHPUnit test.
    $tests[] = [
      // Expected result.
      [
        'name' => 'Drupal\Tests\simpletest\Functional\BrowserTestBaseTest',
        'group' => 'simpletest',
        'description' => 'Tests BrowserTestBase functionality.',
66
        'type' => 'PHPUnit-Functional',
67 68 69 70 71
      ],
      // Classname.
      'Drupal\Tests\simpletest\Functional\BrowserTestBaseTest',
    ];

72 73 74 75 76 77 78 79 80 81 82 83 84
    // kernel PHPUnit test.
    $tests['phpunit-kernel'] = [
      // Expected result.
      [
        'name' => '\Drupal\Tests\file\Kernel\FileItemValidationTest',
        'group' => 'file',
        'description' => 'Tests that files referenced in file and image fields are always validated.',
        'type' => 'PHPUnit-Kernel',
      ],
      // Classname.
      '\Drupal\Tests\file\Kernel\FileItemValidationTest',
    ];

85 86
    // Simpletest classes can not be autoloaded in a PHPUnit test, therefore
    // provide a docblock.
87 88 89
    $tests[] = [
      // Expected result.
      [
90 91 92
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
        'group' => 'simpletest',
        'description' => 'Tests the Simpletest UI internal browser.',
93
        'type' => 'Simpletest',
94 95
      ],
      // Classname.
96
      'Drupal\simpletest\Tests\ExampleSimpleTest',
97 98
      // Doc block.
      "/**
99
 * Tests the Simpletest UI internal browser.
100
 *
101
 * @group simpletest
102 103 104 105
 */
 ",
    ];

106 107 108 109
    // Test with a different amount of leading spaces.
    $tests[] = [
      // Expected result.
      [
110 111 112 113
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
        'group' => 'simpletest',
        'description' => 'Tests the Simpletest UI internal browser.',
        'type' => 'Simpletest',
114 115
      ],
      // Classname.
116
      'Drupal\simpletest\Tests\ExampleSimpleTest',
117 118
      // Doc block.
      "/**
119
   * Tests the Simpletest UI internal browser.
120
   *
121 122
   * @group simpletest
   */
123 124 125 126 127 128 129 130 131
   */
 ",
    ];

    // Make sure that a "* @" inside a string does not get parsed as an
    // annotation.
    $tests[] = [
      // Expected result.
      [
132 133 134 135
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
        'group' => 'simpletest',
        'description' => 'Tests the Simpletest UI internal browser. * @',
        'type' => 'Simpletest',
136 137
      ],
      // Classname.
138
      'Drupal\simpletest\Tests\ExampleSimpleTest',
139 140
      // Doc block.
      "/**
141
   * Tests the Simpletest UI internal browser. * @
142
   *
143
   * @group simpletest
144 145 146 147
   */
 ",
    ];

148 149 150 151
    // Multiple @group annotations.
    $tests[] = [
      // Expected result.
      [
152
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
153
        'group' => 'Test',
154 155
        'description' => 'Tests the Simpletest UI internal browser.',
        'type' => 'Simpletest',
156 157
      ],
      // Classname.
158
      'Drupal\simpletest\Tests\ExampleSimpleTest',
159 160
      // Doc block.
      "/**
161
 * Tests the Simpletest UI internal browser.
162 163
 *
 * @group Test
164
 * @group simpletest
165 166 167 168 169 170 171 172
 */
 ",
    ];

    // @dependencies annotation.
    $tests[] = [
      // Expected result.
      [
173 174 175
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
        'description' => 'Tests the Simpletest UI internal browser.',
        'type' => 'Simpletest',
176
        'requires' => ['module' => ['test']],
177
        'group' => 'simpletest',
178 179
      ],
      // Classname.
180
      'Drupal\simpletest\Tests\ExampleSimpleTest',
181 182
      // Doc block.
      "/**
183
 * Tests the Simpletest UI internal browser.
184 185
 *
 * @dependencies test
186
 * @group simpletest
187 188 189 190 191 192 193 194
 */
 ",
    ];

    // Multiple @dependencies annotation.
    $tests[] = [
      // Expected result.
      [
195 196 197
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
        'description' => 'Tests the Simpletest UI internal browser.',
        'type' => 'Simpletest',
198
        'requires' => ['module' => ['test', 'test1', 'test2']],
199
        'group' => 'simpletest',
200 201
      ],
      // Classname.
202
      'Drupal\simpletest\Tests\ExampleSimpleTest',
203 204
      // Doc block.
      "/**
205
 * Tests the Simpletest UI internal browser.
206
 *
207 208
 * @dependencies test, test1, test2
 * @group simpletest
209 210 211 212 213 214 215 216
 */
 ",
    ];

    // Multi-line summary line.
    $tests[] = [
      // Expected result.
      [
217 218 219 220
        'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
        'description' => 'Tests the Simpletest UI internal browser. And the summary line continues an there is no gap to the annotation.',
        'type' => 'Simpletest',
        'group' => 'simpletest',
221 222
      ],
      // Classname.
223
      'Drupal\simpletest\Tests\ExampleSimpleTest',
224 225
      // Doc block.
      "/**
226 227 228 229
 * Tests the Simpletest UI internal browser. And the summary line continues an
 * there is no gap to the annotation.
 *
 * @group simpletest
230 231 232 233 234 235 236 237 238
 */
 ",
    ];
    return $tests;
  }

  /**
   * @covers ::getTestInfo
   * @expectedException \Drupal\simpletest\Exception\MissingGroupException
239
   * @expectedExceptionMessage Missing @group annotation in Drupal\KernelTests\field\BulkDeleteTest
240 241
   */
  public function testTestInfoParserMissingGroup() {
242
    $classname = 'Drupal\KernelTests\field\BulkDeleteTest';
243 244 245 246 247
    $doc_comment = <<<EOT
/**
 * Bulk delete storages and fields, and clean up afterwards.
 */
EOT;
248
    TestDiscovery::getTestInfo($classname, $doc_comment);
249 250 251 252 253 254
  }

  /**
   * @covers ::getTestInfo
   */
  public function testTestInfoParserMissingSummary() {
255
    $classname = 'Drupal\KernelTests\field\BulkDeleteTest';
256 257 258 259 260
    $doc_comment = <<<EOT
/**
 * @group field
 */
EOT;
261
    $info = TestDiscovery::getTestInfo($classname, $doc_comment);
262
    $this->assertEmpty($info['description']);
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  protected function setupVfsWithTestClasses() {
    vfsStream::setup('drupal');

    $test_file = <<<EOF
<?php

/**
 * Test description
 * @group example
 */
class FunctionalExampleTest {}
EOF;

    vfsStream::create([
      'modules' => [
        'test_module' => [
          'tests' => [
            'src' => [
              'Functional' => [
                'FunctionalExampleTest.php' => $test_file,
                'FunctionalExampleTest2.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest2', '@group example2'], $test_file),
              ],
              'Kernel' => [
                'KernelExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['KernelExampleTest3', '@group example2'], $test_file),
              ],
            ],
          ],
        ],
      ],
    ]);
  }

  /**
   * @covers ::getTestClasses
   */
  public function testGetTestClasses() {
    $this->setupVfsWithTestClasses();
    $class_loader = $this->prophesize(ClassLoader::class);
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);

    $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal());

    $extensions = [
      'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
    ];
    $test_discovery->setExtensions($extensions);
    $result = $test_discovery->getTestClasses();
    $this->assertCount(2, $result);
    $this->assertEquals([
      'example' => [
        'Drupal\Tests\test_module\Functional\FunctionalExampleTest' => [
          'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest',
          'description' => 'Test description',
          'group' => 'example',
          'type' => 'PHPUnit-Functional',
        ],
      ],
      'example2' => [
        'Drupal\Tests\test_module\Functional\FunctionalExampleTest2' => [
          'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2',
          'description' => 'Test description',
          'group' => 'example2',
          'type' => 'PHPUnit-Functional',
        ],
        'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [
          'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3',
          'description' => 'Test description',
          'group' => 'example2',
          'type' => 'PHPUnit-Kernel',
        ],
      ],
    ], $result);
  }

  /**
   * @covers ::getTestClasses
   */
  public function testGetTestClassesWithSelectedTypes() {
    $this->setupVfsWithTestClasses();
    $class_loader = $this->prophesize(ClassLoader::class);
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);

    $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal());

    $extensions = [
      'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
    ];
    $test_discovery->setExtensions($extensions);
    $result = $test_discovery->getTestClasses(NULL, ['PHPUnit-Kernel']);
    $this->assertCount(2, $result);
    $this->assertEquals([
      'example' => [
      ],
      'example2' => [
        'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [
          'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3',
          'description' => 'Test description',
          'group' => 'example2',
          'type' => 'PHPUnit-Kernel',
        ],
      ],
    ], $result);
  }

}

class TestTestDiscovery extends TestDiscovery {

  /**
   * @var \Drupal\Core\Extension\Extension[]
   */
  protected $extensions = [];

  public function setExtensions(array $extensions) {
    $this->extensions = $extensions;
  }

  /**
   * {@inheritdoc}
   */
  protected function getExtensions() {
    return $this->extensions;
  }

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  /**
   * @covers ::getPhpunitTestSuite
   * @dataProvider providerTestGetPhpunitTestSuite
   */
  public function testGetPhpunitTestSuite($classname, $expected) {
    $this->assertEquals($expected, TestDiscovery::getPhpunitTestSuite($classname));
  }

  public function providerTestGetPhpunitTestSuite() {
    $data = [];
    $data['simpletest-webtest'] = ['\Drupal\rest\Tests\NodeTest', FALSE];
    $data['simpletest-kerneltest'] = ['\Drupal\hal\Tests\FileNormalizeTest', FALSE];
    $data['module-unittest'] = [static::class, 'Unit'];
    $data['module-kerneltest'] = ['\Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest', 'Kernel'];
    $data['module-functionaltest'] = ['\Drupal\Tests\simpletest\Functional\BrowserTestBaseTest', 'Functional'];
    $data['module-functionaljavascripttest'] = ['\Drupal\Tests\toolbar\FunctionalJavascript\ToolbarIntegrationTest', 'FunctionalJavascript'];
    $data['core-unittest'] = ['\Drupal\Tests\ComposerIntegrationTest', 'Unit'];
    $data['core-unittest2'] = ['Drupal\Tests\Core\DrupalTest', 'Unit'];
    $data['core-kerneltest'] = ['\Drupal\KernelTests\KernelTestBaseTest', 'Kernel'];
    $data['core-functionaltest'] = ['\Drupal\FunctionalTests\ExampleTest', 'Functional'];
    $data['core-functionaljavascripttest'] = ['\Drupal\FunctionalJavascriptTests\ExampleTest', 'FunctionalJavascript'];

    return $data;
  }

414
}
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

}

namespace Drupal\simpletest\Tests {

use Drupal\simpletest\WebTestBase;

/**
 * Tests the Simpletest UI internal browser.
 *
 * @group simpletest
 */
class ExampleSimpleTest extends WebTestBase {
}

}