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