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