You are here

class TestDiscoveryTest in Drupal 8

Same name in this branch
  1. 8 core/tests/Drupal/Tests/Core/Test/TestDiscoveryTest.php \Drupal\Tests\Core\Test\TestDiscoveryTest
  2. 8 core/modules/simpletest/tests/src/Unit/TestDiscoveryTest.php \Drupal\Tests\simpletest\Unit\TestDiscoveryTest

@coversDefaultClass \Drupal\simpletest\TestDiscovery

@group simpletest @group legacy

Hierarchy

Expanded class hierarchy of TestDiscoveryTest

File

core/modules/simpletest/tests/src/Unit/TestDiscoveryTest.php, line 20

Namespace

Drupal\Tests\simpletest\Unit
View source
class TestDiscoveryTest extends UnitTestCase {
  protected function setupVfsWithTestClasses() {
    vfsStream::setup('drupal');
    $test_file = <<<EOF
<?php

/**
 * Test description
 * @group example
 */
class FunctionalExampleTest {}
EOF;
    $test_profile_info = <<<EOF
name: Testing
type: profile
core: 8.x
EOF;
    $test_module_info = <<<EOF
name: Testing
type: module
core: 8.x
EOF;
    vfsStream::create([
      'modules' => [
        'test_module' => [
          'test_module.info.yml' => $test_module_info,
          '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\n * @group kernel\n",
                ], $test_file),
                'KernelExampleTestBase.php' => str_replace([
                  'FunctionalExampleTest',
                  '@group example',
                ], [
                  'KernelExampleTestBase',
                  '@group example2',
                ], $test_file),
                'KernelExampleTrait.php' => str_replace([
                  'FunctionalExampleTest',
                  '@group example',
                ], [
                  'KernelExampleTrait',
                  '@group example2',
                ], $test_file),
                'KernelExampleInterface.php' => str_replace([
                  'FunctionalExampleTest',
                  '@group example',
                ], [
                  'KernelExampleInterface',
                  '@group example2',
                ], $test_file),
              ],
            ],
          ],
        ],
      ],
      'profiles' => [
        'test_profile' => [
          'test_profile.info.yml' => $test_profile_info,
          'modules' => [
            'test_profile_module' => [
              'test_profile_module.info.yml' => $test_module_info,
              'tests' => [
                'src' => [
                  'Kernel' => [
                    'KernelExampleTest4.php' => str_replace([
                      'FunctionalExampleTest',
                      '@group example',
                    ], [
                      'KernelExampleTest4',
                      '@group example3',
                    ], $test_file),
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
    ]);
  }

  /**
   * Mock a TestDiscovery object to return specific extension values.
   */
  protected function getTestDiscoveryMock($app_root, $extensions) {
    $class_loader = $this
      ->prophesize(ClassLoader::class);
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $test_discovery = $this
      ->getMockBuilder(TestDiscovery::class)
      ->setConstructorArgs([
      $app_root,
      $class_loader
        ->reveal(),
      $module_handler
        ->reveal(),
    ])
      ->setMethods([
      'getExtensions',
    ])
      ->getMock();
    $test_discovery
      ->expects($this
      ->any())
      ->method('getExtensions')
      ->willReturn($extensions);
    return $test_discovery;
  }

  /**
   * @covers ::getTestClasses
   */
  public function testGetTestClasses() {
    $this
      ->setupVfsWithTestClasses();
    $extensions = [
      'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
    ];
    $test_discovery = $this
      ->getTestDiscoveryMock('vfs://drupal', $extensions);
    $result = $test_discovery
      ->getTestClasses();
    $this
      ->assertCount(3, $result);
    $this
      ->assertEquals([
      'example' => [
        'Drupal\\Tests\\test_module\\Functional\\FunctionalExampleTest' => [
          'name' => 'Drupal\\Tests\\test_module\\Functional\\FunctionalExampleTest',
          'description' => 'Test description',
          'group' => 'example',
          'groups' => [
            'example',
          ],
          'type' => 'PHPUnit-Functional',
        ],
      ],
      'example2' => [
        'Drupal\\Tests\\test_module\\Functional\\FunctionalExampleTest2' => [
          'name' => 'Drupal\\Tests\\test_module\\Functional\\FunctionalExampleTest2',
          'description' => 'Test description',
          'group' => 'example2',
          'groups' => [
            'example2',
          ],
          'type' => 'PHPUnit-Functional',
        ],
        'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3' => [
          'name' => 'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3',
          'description' => 'Test description',
          'group' => 'example2',
          'groups' => [
            'example2',
            'kernel',
          ],
          'type' => 'PHPUnit-Kernel',
        ],
      ],
      'kernel' => [
        'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3' => [
          'name' => 'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3',
          'description' => 'Test description',
          'group' => 'example2',
          'groups' => [
            'example2',
            'kernel',
          ],
          'type' => 'PHPUnit-Kernel',
        ],
      ],
    ], $result);
  }

  /**
   * @covers ::getTestClasses
   */
  public function testGetTestClassesWithSelectedTypes() {
    $this
      ->setupVfsWithTestClasses();
    $extensions = [
      'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
      'test_profile_module' => new Extension('vfs://drupal', 'profile', 'profiles/test_profile/modules/test_profile_module/test_profile_module.info.yml'),
    ];
    $test_discovery = $this
      ->getTestDiscoveryMock('vfs://drupal', $extensions);
    $result = $test_discovery
      ->getTestClasses(NULL, [
      'PHPUnit-Kernel',
    ]);
    $this
      ->assertCount(4, $result);
    $this
      ->assertEquals([
      'example' => [],
      'example2' => [
        'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3' => [
          'name' => 'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3',
          'description' => 'Test description',
          'group' => 'example2',
          'groups' => [
            'example2',
            'kernel',
          ],
          'type' => 'PHPUnit-Kernel',
        ],
      ],
      'kernel' => [
        'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3' => [
          'name' => 'Drupal\\Tests\\test_module\\Kernel\\KernelExampleTest3',
          'description' => 'Test description',
          'group' => 'example2',
          'groups' => [
            'example2',
            'kernel',
          ],
          'type' => 'PHPUnit-Kernel',
        ],
      ],
      'example3' => [
        'Drupal\\Tests\\test_profile_module\\Kernel\\KernelExampleTest4' => [
          'name' => 'Drupal\\Tests\\test_profile_module\\Kernel\\KernelExampleTest4',
          'description' => 'Test description',
          'group' => 'example3',
          'groups' => [
            'example3',
          ],
          'type' => 'PHPUnit-Kernel',
        ],
      ],
    ], $result);
  }

  /**
   * @covers ::getTestClasses
   */
  public function testGetTestsInProfiles() {
    $this
      ->setupVfsWithTestClasses();
    $class_loader = $this
      ->prophesize(ClassLoader::class);
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $container = new Container();
    $container
      ->set('kernel', new DrupalKernel('prod', new ClassLoader()));
    $container
      ->set('site.path', 'sites/default');
    \Drupal::setContainer($container);
    $test_discovery = new TestDiscovery('vfs://drupal', $class_loader
      ->reveal(), $module_handler
      ->reveal());
    $result = $test_discovery
      ->getTestClasses('test_profile_module', [
      'PHPUnit-Kernel',
    ]);
    $expected = [
      'example3' => [
        'Drupal\\Tests\\test_profile_module\\Kernel\\KernelExampleTest4' => [
          'name' => 'Drupal\\Tests\\test_profile_module\\Kernel\\KernelExampleTest4',
          'description' => 'Test description',
          'group' => 'example3',
          'groups' => [
            'example3',
          ],
          'type' => 'PHPUnit-Kernel',
        ],
      ],
    ];
    $this
      ->assertEquals($expected, $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TestDiscoveryTest::getTestDiscoveryMock protected function Mock a TestDiscovery object to return specific extension values.
TestDiscoveryTest::setupVfsWithTestClasses protected function
TestDiscoveryTest::testGetTestClasses public function @covers ::getTestClasses
TestDiscoveryTest::testGetTestClassesWithSelectedTypes public function @covers ::getTestClasses
TestDiscoveryTest::testGetTestsInProfiles public function @covers ::getTestClasses
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340