TestDiscovery.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. namespace Drupal\Core\Test;
  3. use Doctrine\Common\Reflection\StaticReflectionParser;
  4. use Drupal\Component\Annotation\Reflection\MockFileFinder;
  5. use Drupal\Component\Utility\NestedArray;
  6. use Drupal\Core\Extension\ExtensionDiscovery;
  7. use Drupal\Core\Test\Exception\MissingGroupException;
  8. use PHPUnit\Util\Test;
  9. /**
  10. * Discovers available tests.
  11. */
  12. class TestDiscovery {
  13. /**
  14. * The class loader.
  15. *
  16. * @var \Composer\Autoload\ClassLoader
  17. */
  18. protected $classLoader;
  19. /**
  20. * Statically cached list of test classes.
  21. *
  22. * @var array
  23. */
  24. protected $testClasses;
  25. /**
  26. * Cached map of all test namespaces to respective directories.
  27. *
  28. * @var array
  29. */
  30. protected $testNamespaces;
  31. /**
  32. * Cached list of all available extension names, keyed by extension type.
  33. *
  34. * @var array
  35. */
  36. protected $availableExtensions;
  37. /**
  38. * The app root.
  39. *
  40. * @var string
  41. */
  42. protected $root;
  43. /**
  44. * Constructs a new test discovery.
  45. *
  46. * @param string $root
  47. * The app root.
  48. * @param $class_loader
  49. * The class loader. Normally Composer's ClassLoader, as included by the
  50. * front controller, but may also be decorated; e.g.,
  51. * \Symfony\Component\ClassLoader\ApcClassLoader.
  52. */
  53. public function __construct($root, $class_loader) {
  54. $this->root = $root;
  55. $this->classLoader = $class_loader;
  56. }
  57. /**
  58. * Registers test namespaces of all extensions and core test classes.
  59. *
  60. * @return array
  61. * An associative array whose keys are PSR-4 namespace prefixes and whose
  62. * values are directory names.
  63. */
  64. public function registerTestNamespaces() {
  65. if (isset($this->testNamespaces)) {
  66. return $this->testNamespaces;
  67. }
  68. $this->testNamespaces = [];
  69. $existing = $this->classLoader->getPrefixesPsr4();
  70. // Add PHPUnit test namespaces of Drupal core.
  71. $this->testNamespaces['Drupal\\Tests\\'] = [$this->root . '/core/tests/Drupal/Tests'];
  72. $this->testNamespaces['Drupal\\BuildTests\\'] = [$this->root . '/core/tests/Drupal/BuildTests'];
  73. $this->testNamespaces['Drupal\\KernelTests\\'] = [$this->root . '/core/tests/Drupal/KernelTests'];
  74. $this->testNamespaces['Drupal\\FunctionalTests\\'] = [$this->root . '/core/tests/Drupal/FunctionalTests'];
  75. $this->testNamespaces['Drupal\\FunctionalJavascriptTests\\'] = [$this->root . '/core/tests/Drupal/FunctionalJavascriptTests'];
  76. $this->testNamespaces['Drupal\\TestTools\\'] = [$this->root . '/core/tests/Drupal/TestTools'];
  77. $this->availableExtensions = [];
  78. foreach ($this->getExtensions() as $name => $extension) {
  79. $this->availableExtensions[$extension->getType()][$name] = $name;
  80. $base_path = $this->root . '/' . $extension->getPath();
  81. // Add namespace of disabled/uninstalled extensions.
  82. if (!isset($existing["Drupal\\$name\\"])) {
  83. $this->classLoader->addPsr4("Drupal\\$name\\", "$base_path/src");
  84. }
  85. // Add Simpletest test namespace.
  86. $this->testNamespaces["Drupal\\$name\\Tests\\"][] = "$base_path/src/Tests";
  87. // Add PHPUnit test namespaces.
  88. $this->testNamespaces["Drupal\\Tests\\$name\\Unit\\"][] = "$base_path/tests/src/Unit";
  89. $this->testNamespaces["Drupal\\Tests\\$name\\Kernel\\"][] = "$base_path/tests/src/Kernel";
  90. $this->testNamespaces["Drupal\\Tests\\$name\\Functional\\"][] = "$base_path/tests/src/Functional";
  91. $this->testNamespaces["Drupal\\Tests\\$name\\Build\\"][] = "$base_path/tests/src/Build";
  92. $this->testNamespaces["Drupal\\Tests\\$name\\FunctionalJavascript\\"][] = "$base_path/tests/src/FunctionalJavascript";
  93. // Add discovery for traits which are shared between different test
  94. // suites.
  95. $this->testNamespaces["Drupal\\Tests\\$name\\Traits\\"][] = "$base_path/tests/src/Traits";
  96. }
  97. foreach ($this->testNamespaces as $prefix => $paths) {
  98. $this->classLoader->addPsr4($prefix, $paths);
  99. }
  100. return $this->testNamespaces;
  101. }
  102. /**
  103. * Discovers all available tests in all extensions.
  104. *
  105. * @param string $extension
  106. * (optional) The name of an extension to limit discovery to; e.g., 'node'.
  107. * @param string[] $types
  108. * An array of included test types.
  109. *
  110. * @return array
  111. * An array of tests keyed by the group name. If a test is annotated to
  112. * belong to multiple groups, it will appear under all group keys it belongs
  113. * to.
  114. * @code
  115. * $groups['block'] => array(
  116. * 'Drupal\Tests\block\Functional\BlockTest' => array(
  117. * 'name' => 'Drupal\Tests\block\Functional\BlockTest',
  118. * 'description' => 'Tests block UI CRUD functionality.',
  119. * 'group' => 'block',
  120. * 'groups' => ['block', 'group2', 'group3'],
  121. * ),
  122. * );
  123. * @endcode
  124. *
  125. * @todo Remove singular grouping; retain list of groups in 'group' key.
  126. * @see https://www.drupal.org/node/2296615
  127. */
  128. public function getTestClasses($extension = NULL, array $types = []) {
  129. if (!isset($extension) && empty($types)) {
  130. if (!empty($this->testClasses)) {
  131. return $this->testClasses;
  132. }
  133. }
  134. $list = [];
  135. $classmap = $this->findAllClassFiles($extension);
  136. // Prevent expensive class loader lookups for each reflected test class by
  137. // registering the complete classmap of test classes to the class loader.
  138. // This also ensures that test classes are loaded from the discovered
  139. // pathnames; a namespace/classname mismatch will throw an exception.
  140. $this->classLoader->addClassMap($classmap);
  141. foreach ($classmap as $classname => $pathname) {
  142. $finder = MockFileFinder::create($pathname);
  143. $parser = new StaticReflectionParser($classname, $finder, TRUE);
  144. try {
  145. $info = static::getTestInfo($classname, $parser->getDocComment());
  146. }
  147. catch (MissingGroupException $e) {
  148. // If the class name ends in Test and is not a migrate table dump.
  149. if (preg_match('/Test$/', $classname) && strpos($classname, 'migrate_drupal\Tests\Table') === FALSE) {
  150. throw $e;
  151. }
  152. // If the class is @group annotation just skip it. Most likely it is an
  153. // abstract class, trait or test fixture.
  154. continue;
  155. }
  156. // Skip this test class if it is a Simpletest-based test and requires
  157. // unavailable modules. TestDiscovery should not filter out module
  158. // requirements for PHPUnit-based test classes.
  159. // @todo Move this behavior to \Drupal\simpletest\TestBase so tests can be
  160. // marked as skipped, instead.
  161. // @see https://www.drupal.org/node/1273478
  162. if ($info['type'] == 'Simpletest') {
  163. if (!empty($info['requires']['module'])) {
  164. if (array_diff($info['requires']['module'], $this->availableExtensions['module'])) {
  165. continue;
  166. }
  167. }
  168. }
  169. foreach ($info['groups'] as $group) {
  170. $list[$group][$classname] = $info;
  171. }
  172. }
  173. // Sort the groups and tests within the groups by name.
  174. uksort($list, 'strnatcasecmp');
  175. foreach ($list as &$tests) {
  176. uksort($tests, 'strnatcasecmp');
  177. }
  178. if (!isset($extension) && empty($types)) {
  179. $this->testClasses = $list;
  180. }
  181. if ($types) {
  182. $list = NestedArray::filter($list, function ($element) use ($types) {
  183. return !(is_array($element) && isset($element['type']) && !in_array($element['type'], $types));
  184. });
  185. }
  186. return $list;
  187. }
  188. /**
  189. * Discovers all class files in all available extensions.
  190. *
  191. * @param string $extension
  192. * (optional) The name of an extension to limit discovery to; e.g., 'node'.
  193. *
  194. * @return array
  195. * A classmap containing all discovered class files; i.e., a map of
  196. * fully-qualified classnames to pathnames.
  197. */
  198. public function findAllClassFiles($extension = NULL) {
  199. $classmap = [];
  200. $namespaces = $this->registerTestNamespaces();
  201. if (isset($extension)) {
  202. // Include tests in the \Drupal\Tests\{$extension} namespace.
  203. $pattern = "/Drupal\\\(Tests\\\)?$extension\\\/";
  204. $namespaces = array_intersect_key($namespaces, array_flip(preg_grep($pattern, array_keys($namespaces))));
  205. }
  206. foreach ($namespaces as $namespace => $paths) {
  207. foreach ($paths as $path) {
  208. if (!is_dir($path)) {
  209. continue;
  210. }
  211. $classmap += static::scanDirectory($namespace, $path);
  212. }
  213. }
  214. return $classmap;
  215. }
  216. /**
  217. * Scans a given directory for class files.
  218. *
  219. * @param string $namespace_prefix
  220. * The namespace prefix to use for discovered classes. Must contain a
  221. * trailing namespace separator (backslash).
  222. * For example: 'Drupal\\node\\Tests\\'
  223. * @param string $path
  224. * The directory path to scan.
  225. * For example: '/path/to/drupal/core/modules/node/tests/src'
  226. *
  227. * @return array
  228. * An associative array whose keys are fully-qualified class names and whose
  229. * values are corresponding filesystem pathnames.
  230. *
  231. * @throws \InvalidArgumentException
  232. * If $namespace_prefix does not end in a namespace separator (backslash).
  233. *
  234. * @todo Limit to '*Test.php' files (~10% less files to reflect/introspect).
  235. * @see https://www.drupal.org/node/2296635
  236. */
  237. public static function scanDirectory($namespace_prefix, $path) {
  238. if (substr($namespace_prefix, -1) !== '\\') {
  239. throw new \InvalidArgumentException("Namespace prefix for $path must contain a trailing namespace separator.");
  240. }
  241. $flags = \FilesystemIterator::UNIX_PATHS;
  242. $flags |= \FilesystemIterator::SKIP_DOTS;
  243. $flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
  244. $flags |= \FilesystemIterator::CURRENT_AS_SELF;
  245. $flags |= \FilesystemIterator::KEY_AS_FILENAME;
  246. $iterator = new \RecursiveDirectoryIterator($path, $flags);
  247. $filter = new \RecursiveCallbackFilterIterator($iterator, function ($current, $file_name, $iterator) {
  248. if ($iterator->hasChildren()) {
  249. return TRUE;
  250. }
  251. // We don't want to discover abstract TestBase classes, traits or
  252. // interfaces. They can be deprecated and will call @trigger_error()
  253. // during discovery.
  254. return substr($file_name, -4) === '.php' &&
  255. substr($file_name, -12) !== 'TestBase.php' &&
  256. substr($file_name, -9) !== 'Trait.php' &&
  257. substr($file_name, -13) !== 'Interface.php';
  258. });
  259. $files = new \RecursiveIteratorIterator($filter);
  260. $classes = [];
  261. foreach ($files as $fileinfo) {
  262. $class = $namespace_prefix;
  263. if ('' !== $subpath = $fileinfo->getSubPath()) {
  264. $class .= strtr($subpath, '/', '\\') . '\\';
  265. }
  266. $class .= $fileinfo->getBasename('.php');
  267. $classes[$class] = $fileinfo->getPathname();
  268. }
  269. return $classes;
  270. }
  271. /**
  272. * Retrieves information about a test class for UI purposes.
  273. *
  274. * @param string $classname
  275. * The test classname.
  276. * @param string $doc_comment
  277. * (optional) The class PHPDoc comment. If not passed in reflection will be
  278. * used but this is very expensive when parsing all the test classes.
  279. *
  280. * @return array
  281. * An associative array containing:
  282. * - name: The test class name.
  283. * - description: The test (PHPDoc) summary.
  284. * - group: The test's first @group (parsed from PHPDoc annotations).
  285. * - groups: All of the test's @group annotations, as an array (parsed from
  286. * PHPDoc annotations).
  287. * - requires: An associative array containing test requirements parsed from
  288. * PHPDoc annotations:
  289. * - module: List of Drupal module extension names the test depends on.
  290. *
  291. * @throws \Drupal\simpletest\Exception\MissingGroupException
  292. * If the class does not have a @group annotation.
  293. */
  294. public static function getTestInfo($classname, $doc_comment = NULL) {
  295. if ($doc_comment === NULL) {
  296. $reflection = new \ReflectionClass($classname);
  297. $doc_comment = $reflection->getDocComment();
  298. }
  299. $info = [
  300. 'name' => $classname,
  301. ];
  302. $annotations = [];
  303. // Look for annotations, allow an arbitrary amount of spaces before the
  304. // * but nothing else.
  305. preg_match_all('/^[ ]*\* \@([^\s]*) (.*$)/m', $doc_comment, $matches);
  306. if (isset($matches[1])) {
  307. foreach ($matches[1] as $key => $annotation) {
  308. // For historical reasons, there is a single-value 'group' result key
  309. // and a 'groups' key as an array.
  310. if ($annotation === 'group') {
  311. $annotations['groups'][] = $matches[2][$key];
  312. }
  313. if (!empty($annotations[$annotation])) {
  314. // Only @group is allowed to have more than one annotation, in the
  315. // 'groups' key. Other annotations only have one value per key.
  316. continue;
  317. }
  318. $annotations[$annotation] = $matches[2][$key];
  319. }
  320. }
  321. if (empty($annotations['group'])) {
  322. // Concrete tests must have a group.
  323. throw new MissingGroupException(sprintf('Missing @group annotation in %s', $classname));
  324. }
  325. $info['group'] = $annotations['group'];
  326. $info['groups'] = $annotations['groups'];
  327. // Sort out PHPUnit-runnable tests by type.
  328. if ($testsuite = static::getPhpunitTestSuite($classname)) {
  329. $info['type'] = 'PHPUnit-' . $testsuite;
  330. }
  331. else {
  332. $info['type'] = 'Simpletest';
  333. }
  334. if (!empty($annotations['coversDefaultClass'])) {
  335. $info['description'] = 'Tests ' . $annotations['coversDefaultClass'] . '.';
  336. }
  337. else {
  338. $info['description'] = static::parseTestClassSummary($doc_comment);
  339. }
  340. if (isset($annotations['dependencies'])) {
  341. $info['requires']['module'] = array_map('trim', explode(',', $annotations['dependencies']));
  342. }
  343. return $info;
  344. }
  345. /**
  346. * Parses the phpDoc summary line of a test class.
  347. *
  348. * @param string $doc_comment
  349. *
  350. * @return string
  351. * The parsed phpDoc summary line. An empty string is returned if no summary
  352. * line can be parsed.
  353. */
  354. public static function parseTestClassSummary($doc_comment) {
  355. // Normalize line endings.
  356. $doc_comment = preg_replace('/\r\n|\r/', '\n', $doc_comment);
  357. // Strip leading and trailing doc block lines.
  358. $doc_comment = substr($doc_comment, 4, -4);
  359. $lines = explode("\n", $doc_comment);
  360. $summary = [];
  361. // Add every line to the summary until the first empty line or annotation
  362. // is found.
  363. foreach ($lines as $line) {
  364. if (preg_match('/^[ ]*\*$/', $line) || preg_match('/^[ ]*\* \@/', $line)) {
  365. break;
  366. }
  367. $summary[] = trim($line, ' *');
  368. }
  369. return implode(' ', $summary);
  370. }
  371. /**
  372. * Parses annotations in the phpDoc of a test class.
  373. *
  374. * @param \ReflectionClass $class
  375. * The reflected test class.
  376. *
  377. * @return array
  378. * An associative array that contains all annotations on the test class;
  379. * typically including:
  380. * - group: A list of @group values.
  381. * - requires: An associative array of @requires values; e.g.:
  382. * - module: A list of Drupal module dependencies that are required to
  383. * exist.
  384. *
  385. * @see \PHPUnit\Util\Test::parseTestMethodAnnotations()
  386. * @see http://phpunit.de/manual/current/en/incomplete-and-skipped-tests.html#incomplete-and-skipped-tests.skipping-tests-using-requires
  387. */
  388. public static function parseTestClassAnnotations(\ReflectionClass $class) {
  389. $annotations = Test::parseTestMethodAnnotations($class->getName())['class'];
  390. // @todo Enhance PHPUnit upstream to allow for custom @requires identifiers.
  391. // @see \PHPUnit\Util\Test::getRequirements()
  392. // @todo Add support for 'PHP', 'OS', 'function', 'extension'.
  393. // @see https://www.drupal.org/node/1273478
  394. if (isset($annotations['requires'])) {
  395. foreach ($annotations['requires'] as $i => $value) {
  396. list($type, $value) = explode(' ', $value, 2);
  397. if ($type === 'module') {
  398. $annotations['requires']['module'][$value] = $value;
  399. unset($annotations['requires'][$i]);
  400. }
  401. }
  402. }
  403. return $annotations;
  404. }
  405. /**
  406. * Determines the phpunit testsuite for a given classname, based on namespace.
  407. *
  408. * @param string $classname
  409. * The test classname.
  410. *
  411. * @return string|false
  412. * The testsuite name or FALSE if its not a phpunit test.
  413. */
  414. public static function getPhpunitTestSuite($classname) {
  415. if (preg_match('/Drupal\\\\Tests\\\\(\w+)\\\\(\w+)/', $classname, $matches)) {
  416. // This could be an extension test, in which case the first match will be
  417. // the extension name. We assume that lower-case strings are module names.
  418. if (strtolower($matches[1]) == $matches[1]) {
  419. return $matches[2];
  420. }
  421. return 'Unit';
  422. }
  423. // Core tests.
  424. elseif (preg_match('/Drupal\\\\(\w*)Tests\\\\/', $classname, $matches)) {
  425. if ($matches[1] == '') {
  426. return 'Unit';
  427. }
  428. return $matches[1];
  429. }
  430. return FALSE;
  431. }
  432. /**
  433. * Returns all available extensions.
  434. *
  435. * @return \Drupal\Core\Extension\Extension[]
  436. * An array of Extension objects, keyed by extension name.
  437. */
  438. protected function getExtensions() {
  439. $listing = new ExtensionDiscovery($this->root);
  440. // Ensure that tests in all profiles are discovered.
  441. $listing->setProfileDirectories([]);
  442. $extensions = $listing->scan('module', TRUE);
  443. $extensions += $listing->scan('profile', TRUE);
  444. $extensions += $listing->scan('theme', TRUE);
  445. return $extensions;
  446. }
  447. }