diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 6c41f07..2a4834e 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -45,9 +45,6 @@ * * @see \Drupal\Tests\KernelTestBase::$modules * @see \Drupal\Tests\KernelTestBase::enableModules() - * - * @todo Extend ::setRequirementsFromAnnotation() and ::checkRequirements() to - * account for '@requires module'. */ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements ServiceProviderInterface { @@ -225,6 +222,49 @@ protected static function getDrupalRoot() { /** * {@inheritdoc} */ + protected function checkRequirements() { + parent::checkRequirements(); + + $this->root = static::getDrupalRoot(); + + // Check if required dependencies exist. + $annotations = $this->getAnnotations(); + if (!empty($annotations['class']['requires'])) { + $this->checkModuleRequirements($annotations['class']['requires']); + } + if (!empty($annotations['method']['requires'])) { + $this->checkModuleRequirements($annotations['method']['requires']); + } + } + + /** + * Checks missing module requirements. + * + * Iterates through a list of requires annotations a looks for missing modules + * and skipping the test if any are missing. + * + * @param $annotations + * A list of requires annotations from either a method or class annotation. + */ + protected function checkModuleRequirements(array $annotations) { + foreach ($annotations as $requirement) { + if (strpos($requirement, 'module ') === 0) { + $module = trim(str_replace('module ', '', $requirement)); + try { + static::initFileCache(); + // drupal_valid_ua() might not be loaded. + require_once $this->root . '/core/includes/bootstrap.inc'; + $this->getExtensionsForModules([$module]); + } catch (\PHPUnit_Framework_Exception $e) { + throw new \PHPUnit_Framework_SkippedTestError("Module $module is required."); + } + } + } + } + + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 30f05b2..08e7178 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -206,6 +206,13 @@ public function testRenderWithTheme() { } /** + * @requires module drupal_dne_module + */ + public function testRequiresModule() { + $this->fail('Running test with missing required module.'); + } + + /** * {@inheritdoc} */ protected function tearDown() { diff --git a/core/tests/Drupal/KernelTests/MissingDependentModuleUnitTest.php b/core/tests/Drupal/KernelTests/MissingDependentModuleUnitTest.php index 124e144..831c20e 100644 --- a/core/tests/Drupal/KernelTests/MissingDependentModuleUnitTest.php +++ b/core/tests/Drupal/KernelTests/MissingDependentModuleUnitTest.php @@ -1,19 +1,19 @@ fail('Running test with missing required module.');