annotate core/tests/Drupal/Tests/TestRequirementsTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests;
Chris@0 4
Chris@0 5 use Drupal\Core\Extension\ExtensionDiscovery;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Allows test classes to require Drupal modules as dependencies.
Chris@0 9 *
Chris@0 10 * This trait is assumed to be on a subclass of \PHPUnit_Framework_TestCase, and
Chris@0 11 * overrides \PHPUnit_Framework_TestCase::checkRequirements(). This allows the
Chris@0 12 * test to be marked as skipped before any kernel boot processes have happened.
Chris@0 13 */
Chris@0 14 trait TestRequirementsTrait {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Returns the Drupal root directory.
Chris@0 18 *
Chris@0 19 * @return string
Chris@0 20 */
Chris@0 21 protected static function getDrupalRoot() {
Chris@0 22 return dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
Chris@0 23 }
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Check module requirements for the Drupal use case.
Chris@0 27 *
Chris@0 28 * This method is assumed to override
Chris@0 29 * \PHPUnit_Framework_TestCase::checkRequirements().
Chris@0 30 *
Chris@0 31 * @throws \PHPUnit_Framework_SkippedTestError
Chris@0 32 * Thrown when the requirements are not met, and this test should be
Chris@0 33 * skipped. Callers should not catch this exception.
Chris@0 34 */
Chris@0 35 protected function checkRequirements() {
Chris@0 36 parent::checkRequirements();
Chris@0 37
Chris@0 38 $root = static::getDrupalRoot();
Chris@0 39
Chris@0 40 // Check if required dependencies exist.
Chris@0 41 $annotations = $this->getAnnotations();
Chris@0 42 if (!empty($annotations['class']['requires'])) {
Chris@0 43 $this->checkModuleRequirements($root, $annotations['class']['requires']);
Chris@0 44 }
Chris@0 45 if (!empty($annotations['method']['requires'])) {
Chris@0 46 $this->checkModuleRequirements($root, $annotations['method']['requires']);
Chris@0 47 }
Chris@0 48 }
Chris@0 49
Chris@0 50 /**
Chris@0 51 * Checks missing module requirements.
Chris@0 52 *
Chris@0 53 * Iterates through a list of requires annotations and looks for missing
Chris@0 54 * modules. The test will be skipped if any of the required modules is
Chris@0 55 * missing.
Chris@0 56 *
Chris@0 57 * @param string $root
Chris@0 58 * The path to the root of the Drupal installation to scan.
Chris@0 59 * @param string[] $annotations
Chris@0 60 * A list of requires annotations from either a method or class annotation.
Chris@0 61 *
Chris@0 62 * @throws \PHPUnit_Framework_SkippedTestError
Chris@0 63 * Thrown when the requirements are not met, and this test should be
Chris@0 64 * skipped. Callers should not catch this exception.
Chris@0 65 */
Chris@0 66 private function checkModuleRequirements($root, array $annotations) {
Chris@0 67 // drupal_valid_ua() might not be loaded.
Chris@0 68 require_once $root . '/core/includes/bootstrap.inc';
Chris@0 69
Chris@0 70 // Make a list of required modules.
Chris@0 71 $required_modules = [];
Chris@0 72 foreach ($annotations as $requirement) {
Chris@0 73 if (strpos($requirement, 'module ') === 0) {
Chris@0 74 $required_modules[] = trim(str_replace('module ', '', $requirement));
Chris@0 75 }
Chris@0 76 }
Chris@0 77
Chris@0 78 // If there are required modules, check if they're available.
Chris@0 79 if (!empty($required_modules)) {
Chris@0 80 // Scan for modules.
Chris@0 81 $discovery = new ExtensionDiscovery($root, FALSE);
Chris@0 82 $discovery->setProfileDirectories([]);
Chris@0 83 $list = array_keys($discovery->scan('module'));
Chris@0 84 $not_available = array_diff($required_modules, $list);
Chris@0 85 if (!empty($not_available)) {
Chris@0 86 throw new \PHPUnit_Framework_SkippedTestError('Required modules: ' . implode(', ', $not_available));
Chris@0 87 }
Chris@0 88 }
Chris@0 89 }
Chris@0 90
Chris@0 91 }