diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index f883b5ec8f..9931804232 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -11,7 +11,6 @@ /** * Tests BrowserTestBase functionality. * - * @coversDefaultClass \Drupal\Tests\BrowserTestBase * @group browsertestbase */ class BrowserTestBaseTest extends BrowserTestBase { @@ -290,48 +289,4 @@ public function testInstall() { $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists"); } - /** - * Tests that a test method is skipped when it requires a module not present. - * - * In order to catch checkRequirements() regressions, we have to make a new - * test object and run checkRequirements() here. - * - * @covers ::checkRequirements - * @covers ::checkModuleRequirements - */ - public function testMethodRequiresModule() { - require __DIR__ . '/../../fixtures/BrowserMissingDependentModuleMethodTest.php'; - - $test = new BrowserMissingDependentModuleMethodTest(); - // We have to setName() to the method name we're concerned with. - $test->setName('testRequiresModule'); - $this->setExpectedException( - \PHPUnit_Framework_SkippedTestError::class, - 'Module module_does_not_exist is required.' - ); - $test->checkRequirements(); - } - - /** - * Tests that a test case is skipped when it requires a module not present. - * - * In order to catch checkRequirements() regressions, we have to make a new - * test object and run checkRequirements() here. - * - * @covers ::checkRequirements - * @covers ::checkModuleRequirements - */ - public function testRequiresModule() { - require __DIR__ . '/../../fixtures/BrowserMissingDependentModuleTest.php'; - - $test = new BrowserMissingDependentModuleTest(); - // We have to setName() to the method name we're concerned with. - $test->setName('testRequiresModule'); - $this->setExpectedException( - \PHPUnit_Framework_SkippedTestError::class, - 'Module module_does_not_exist is required.' - ); - $test->checkRequirements(); - } - } diff --git a/core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php b/core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php new file mode 100644 index 0000000000..056441ae1e --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php @@ -0,0 +1,77 @@ +setName('testRequiresModule'); + $class = new \ReflectionClass($stub_test); + $check_requirements = $class->getMethod('checkRequirements'); + $check_requirements->setAccessible(TRUE); + + // We cannot use $this->setExpectedException() because PHPUnit would skip + // the test before comparing the exception type. + try { + $check_requirements->invoke($stub_test); + $this->fail('Missing required module throws skipped test exception.'); + } + catch (\PHPUnit_Framework_SkippedTestError $e) { + $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); + } + } + + /** + * Tests that a test case is skipped when it requires a module not present. + * + * In order to catch checkRequirements() regressions, we have to make a new + * test object and run checkRequirements() here. + * + * @covers ::checkRequirements + * @covers ::checkModuleRequirements + */ + public function testRequiresModule() { + require __DIR__ . '/../../../../fixtures/BrowserMissingDependentModuleTest.php'; + + $stub_test = new BrowserMissingDependentModuleTest(); + // We have to setName() to the method name we're concerned with. + $stub_test->setName('testRequiresModule'); + $class = new \ReflectionClass($stub_test); + $check_requirements = $class->getMethod('checkRequirements'); + $check_requirements->setAccessible(TRUE); + + // We cannot use $this->setExpectedException() because PHPUnit would skip + // the test before comparing the exception type. + try { + $check_requirements->invoke($stub_test); + $this->fail('Missing required module throws skipped test exception.'); + } + catch (\PHPUnit_Framework_SkippedTestError $e) { + $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); + } + } + +} diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 9972e71459..c5bfbd85fe 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -9,7 +9,9 @@ /** * @coversDefaultClass \Drupal\KernelTests\KernelTestBase + * * @group PHPUnit + * @group KernelTests */ class KernelTestBaseTest extends KernelTestBase { @@ -225,14 +227,22 @@ public function testFileDefaultScheme() { public function testMethodRequiresModule() { require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php'; - $test = new KernelMissingDependentModuleMethodTest(); + $stub_test = new KernelMissingDependentModuleMethodTest(); // We have to setName() to the method name we're concerned with. - $test->setName('testRequiresModule'); - $this->setExpectedException( - \PHPUnit_Framework_SkippedTestError::class, - 'Module module_does_not_exist is required.' - ); - $test->checkRequirements(); + $stub_test->setName('testRequiresModule'); + $class = new \ReflectionClass($stub_test); + $check_requirements = $class->getMethod('checkRequirements'); + $check_requirements->setAccessible(TRUE); + + // We cannot use $this->setExpectedException() because PHPUnit would skip + // the test before comparing the exception type. + try { + $check_requirements->invoke($stub_test); + $this->fail('Missing required module throws skipped test exception.'); + } + catch (\PHPUnit_Framework_SkippedTestError $e) { + $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); + } } /** @@ -247,14 +257,22 @@ public function testMethodRequiresModule() { public function testRequiresModule() { require __DIR__ . '/../../fixtures/KernelMissingDependentModuleTest.php'; - $test = new KernelMissingDependentModuleTest(); + $stub_test = new KernelMissingDependentModuleTest(); // We have to setName() to the method name we're concerned with. - $test->setName('testRequiresModule'); - $this->setExpectedException( - \PHPUnit_Framework_SkippedTestError::class, - 'Module module_does_not_exist is required.' - ); - $test->checkRequirements(); + $stub_test->setName('testRequiresModule'); + $class = new \ReflectionClass($stub_test); + $check_requirements = $class->getMethod('checkRequirements'); + $check_requirements->setAccessible(TRUE); + + // We cannot use $this->setExpectedException() because PHPUnit would skip + // the test before comparing the exception type. + try { + $check_requirements->invoke($stub_test); + $this->fail('Missing required module throws skipped test exception.'); + } + catch (\PHPUnit_Framework_SkippedTestError $e) { + $this->assertEqual('Required modules: module_does_not_exist', $e->getMessage()); + } } /** diff --git a/core/tests/Drupal/Tests/TestRequirementsTrait.php b/core/tests/Drupal/Tests/TestRequirementsTrait.php index 0e1717185d..9268c6ebf2 100644 --- a/core/tests/Drupal/Tests/TestRequirementsTrait.php +++ b/core/tests/Drupal/Tests/TestRequirementsTrait.php @@ -66,17 +66,24 @@ protected function checkRequirements() { private function checkModuleRequirements($root, array $annotations) { // drupal_valid_ua() might not be loaded. require_once $root . '/core/includes/bootstrap.inc'; - // Scan for modules. - $discovery = new ExtensionDiscovery($root, FALSE); - $discovery->setProfileDirectories([]); - $list = $discovery->scan('module'); - // Find requirements in the list of modules. + + // Make a list of required modules. + $required_modules = []; foreach ($annotations as $requirement) { if (strpos($requirement, 'module ') === 0) { - $module = trim(str_replace('module ', '', $requirement)); - if (!isset($list[$module])) { - throw new \PHPUnit_Framework_SkippedTestError("Module $module is required."); - } + $required_modules[] = trim(str_replace('module ', '', $requirement)); + } + } + + // If there are required modules, check if they're available. + if (!empty($required_modules)) { + // Scan for modules. + $discovery = new ExtensionDiscovery($root, FALSE); + $discovery->setProfileDirectories([]); + $list = array_keys($discovery->scan('module')); + $not_available = array_diff($required_modules, $list); + if (!empty($not_available)) { + throw new \PHPUnit_Framework_SkippedTestError('Required modules: ' . implode(', ', $not_available)); } } }