Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Framework;
11
 
12
use const PHP_EOL;
13
use function array_keys;
14
use function array_map;
15
use function array_merge;
16
use function array_slice;
17
use function array_unique;
18
use function basename;
19
use function call_user_func;
20
use function class_exists;
21
use function count;
22
use function dirname;
23
use function get_declared_classes;
24
use function implode;
25
use function is_bool;
26
use function is_callable;
27
use function is_file;
28
use function is_object;
29
use function is_string;
30
use function method_exists;
31
use function preg_match;
32
use function preg_quote;
33
use function sprintf;
34
use function strpos;
35
use function substr;
36
use Iterator;
37
use IteratorAggregate;
38
use PHPUnit\Runner\BaseTestRunner;
39
use PHPUnit\Runner\Filter\Factory;
40
use PHPUnit\Runner\PhptTestCase;
41
use PHPUnit\Util\FileLoader;
42
use PHPUnit\Util\Reflection;
43
use PHPUnit\Util\Test as TestUtil;
44
use ReflectionClass;
45
use ReflectionException;
46
use ReflectionMethod;
47
use Throwable;
48
 
49
/**
50
 * @template-implements IteratorAggregate<int, Test>
51
 *
52
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
53
 */
54
class TestSuite implements IteratorAggregate, Reorderable, SelfDescribing, Test
55
{
56
    /**
57
     * Enable or disable the backup and restoration of the $GLOBALS array.
58
     *
59
     * @var bool
60
     */
61
    protected $backupGlobals;
62
 
63
    /**
64
     * Enable or disable the backup and restoration of static attributes.
65
     *
66
     * @var bool
67
     */
68
    protected $backupStaticAttributes;
69
 
70
    /**
71
     * @var bool
72
     */
73
    protected $runTestInSeparateProcess = false;
74
 
75
    /**
76
     * The name of the test suite.
77
     *
78
     * @var string
79
     */
80
    protected $name = '';
81
 
82
    /**
83
     * The test groups of the test suite.
84
     *
85
     * @psalm-var array<string,list<Test>>
86
     */
87
    protected $groups = [];
88
 
89
    /**
90
     * The tests in the test suite.
91
     *
92
     * @var Test[]
93
     */
94
    protected $tests = [];
95
 
96
    /**
97
     * The number of tests in the test suite.
98
     *
99
     * @var int
100
     */
101
    protected $numTests = -1;
102
 
103
    /**
104
     * @var bool
105
     */
106
    protected $testCase = false;
107
 
108
    /**
109
     * @var string[]
110
     */
111
    protected $foundClasses = [];
112
 
113
    /**
114
     * @var null|list<ExecutionOrderDependency>
115
     */
116
    protected $providedTests;
117
 
118
    /**
119
     * @var null|list<ExecutionOrderDependency>
120
     */
121
    protected $requiredTests;
122
 
123
    /**
124
     * @var bool
125
     */
126
    private $beStrictAboutChangesToGlobalState;
127
 
128
    /**
129
     * @var Factory
130
     */
131
    private $iteratorFilter;
132
 
133
    /**
134
     * @var int
135
     */
136
    private $declaredClassesPointer;
137
 
138
    /**
139
     * @psalm-var array<int,string>
140
     */
141
    private $warnings = [];
142
 
143
    /**
144
     * Constructs a new TestSuite.
145
     *
146
     *   - PHPUnit\Framework\TestSuite() constructs an empty TestSuite.
147
     *
148
     *   - PHPUnit\Framework\TestSuite(ReflectionClass) constructs a
149
     *     TestSuite from the given class.
150
     *
151
     *   - PHPUnit\Framework\TestSuite(ReflectionClass, String)
152
     *     constructs a TestSuite from the given class with the given
153
     *     name.
154
     *
155
     *   - PHPUnit\Framework\TestSuite(String) either constructs a
156
     *     TestSuite from the given class (if the passed string is the
157
     *     name of an existing class) or constructs an empty TestSuite
158
     *     with the given name.
159
     *
160
     * @param ReflectionClass|string $theClass
161
     *
162
     * @throws Exception
163
     */
164
    public function __construct($theClass = '', string $name = '')
165
    {
166
        if (!is_string($theClass) && !$theClass instanceof ReflectionClass) {
167
            throw InvalidArgumentException::create(
168
                1,
169
                'ReflectionClass object or string'
170
            );
171
        }
172
 
173
        $this->declaredClassesPointer = count(get_declared_classes());
174
 
175
        if (!$theClass instanceof ReflectionClass) {
176
            if (class_exists($theClass, true)) {
177
                if ($name === '') {
178
                    $name = $theClass;
179
                }
180
 
181
                try {
182
                    $theClass = new ReflectionClass($theClass);
183
                } catch (ReflectionException $e) {
184
                    throw new Exception(
185
                        $e->getMessage(),
186
                        $e->getCode(),
187
                        $e
188
                    );
189
                }
190
                // @codeCoverageIgnoreEnd
191
            } else {
192
                $this->setName($theClass);
193
 
194
                return;
195
            }
196
        }
197
 
198
        if (!$theClass->isSubclassOf(TestCase::class)) {
199
            $this->setName((string) $theClass);
200
 
201
            return;
202
        }
203
 
204
        if ($name !== '') {
205
            $this->setName($name);
206
        } else {
207
            $this->setName($theClass->getName());
208
        }
209
 
210
        $constructor = $theClass->getConstructor();
211
 
212
        if ($constructor !== null &&
213
            !$constructor->isPublic()) {
214
            $this->addTest(
215
                new WarningTestCase(
216
                    sprintf(
217
                        'Class "%s" has no public constructor.',
218
                        $theClass->getName()
219
                    )
220
                )
221
            );
222
 
223
            return;
224
        }
225
 
226
        foreach ((new Reflection)->publicMethodsInTestClass($theClass) as $method) {
227
            if (!TestUtil::isTestMethod($method)) {
228
                continue;
229
            }
230
 
231
            $this->addTestMethod($theClass, $method);
232
        }
233
 
234
        if (empty($this->tests)) {
235
            $this->addTest(
236
                new WarningTestCase(
237
                    sprintf(
238
                        'No tests found in class "%s".',
239
                        $theClass->getName()
240
                    )
241
                )
242
            );
243
        }
244
 
245
        $this->testCase = true;
246
    }
247
 
248
    /**
249
     * Returns a string representation of the test suite.
250
     */
251
    public function toString(): string
252
    {
253
        return $this->getName();
254
    }
255
 
256
    /**
257
     * Adds a test to the suite.
258
     *
259
     * @param array $groups
260
     */
261
    public function addTest(Test $test, $groups = []): void
262
    {
263
        try {
264
            $class = new ReflectionClass($test);
265
            // @codeCoverageIgnoreStart
266
        } catch (ReflectionException $e) {
267
            throw new Exception(
268
                $e->getMessage(),
269
                $e->getCode(),
270
                $e
271
            );
272
        }
273
        // @codeCoverageIgnoreEnd
274
 
275
        if (!$class->isAbstract()) {
276
            $this->tests[] = $test;
277
            $this->clearCaches();
278
 
279
            if ($test instanceof self && empty($groups)) {
280
                $groups = $test->getGroups();
281
            }
282
 
283
            if ($this->containsOnlyVirtualGroups($groups)) {
284
                $groups[] = 'default';
285
            }
286
 
287
            foreach ($groups as $group) {
288
                if (!isset($this->groups[$group])) {
289
                    $this->groups[$group] = [$test];
290
                } else {
291
                    $this->groups[$group][] = $test;
292
                }
293
            }
294
 
295
            if ($test instanceof TestCase) {
296
                $test->setGroups($groups);
297
            }
298
        }
299
    }
300
 
301
    /**
302
     * Adds the tests from the given class to the suite.
303
     *
304
     * @psalm-param object|class-string $testClass
305
     *
306
     * @throws Exception
307
     */
308
    public function addTestSuite($testClass): void
309
    {
310
        if (!(is_object($testClass) || (is_string($testClass) && class_exists($testClass)))) {
311
            throw InvalidArgumentException::create(
312
                1,
313
                'class name or object'
314
            );
315
        }
316
 
317
        if (!is_object($testClass)) {
318
            try {
319
                $testClass = new ReflectionClass($testClass);
320
                // @codeCoverageIgnoreStart
321
            } catch (ReflectionException $e) {
322
                throw new Exception(
323
                    $e->getMessage(),
324
                    $e->getCode(),
325
                    $e
326
                );
327
            }
328
            // @codeCoverageIgnoreEnd
329
        }
330
 
331
        if ($testClass instanceof self) {
332
            $this->addTest($testClass);
333
        } elseif ($testClass instanceof ReflectionClass) {
334
            $suiteMethod = false;
335
 
336
            if (!$testClass->isAbstract() && $testClass->hasMethod(BaseTestRunner::SUITE_METHODNAME)) {
337
                try {
338
                    $method = $testClass->getMethod(
339
                        BaseTestRunner::SUITE_METHODNAME
340
                    );
341
                    // @codeCoverageIgnoreStart
342
                } catch (ReflectionException $e) {
343
                    throw new Exception(
344
                        $e->getMessage(),
345
                        $e->getCode(),
346
                        $e
347
                    );
348
                }
349
                // @codeCoverageIgnoreEnd
350
 
351
                if ($method->isStatic()) {
352
                    $this->addTest(
353
                        $method->invoke(null, $testClass->getName())
354
                    );
355
 
356
                    $suiteMethod = true;
357
                }
358
            }
359
 
360
            if (!$suiteMethod && !$testClass->isAbstract() && $testClass->isSubclassOf(TestCase::class)) {
361
                $this->addTest(new self($testClass));
362
            }
363
        } else {
364
            throw new Exception;
365
        }
366
    }
367
 
368
    public function addWarning(string $warning): void
369
    {
370
        $this->warnings[] = $warning;
371
    }
372
 
373
    /**
374
     * Wraps both <code>addTest()</code> and <code>addTestSuite</code>
375
     * as well as the separate import statements for the user's convenience.
376
     *
377
     * If the named file cannot be read or there are no new tests that can be
378
     * added, a <code>PHPUnit\Framework\WarningTestCase</code> will be created instead,
379
     * leaving the current test run untouched.
380
     *
381
     * @throws Exception
382
     */
383
    public function addTestFile(string $filename): void
384
    {
385
        if (is_file($filename) && substr($filename, -5) === '.phpt') {
386
            $this->addTest(new PhptTestCase($filename));
387
 
388
            $this->declaredClassesPointer = count(get_declared_classes());
389
 
390
            return;
391
        }
392
 
393
        $numTests = count($this->tests);
394
 
395
        // The given file may contain further stub classes in addition to the
396
        // test class itself. Figure out the actual test class.
397
        $filename   = FileLoader::checkAndLoad($filename);
398
        $newClasses = array_slice(get_declared_classes(), $this->declaredClassesPointer);
399
 
400
        // The diff is empty in case a parent class (with test methods) is added
401
        // AFTER a child class that inherited from it. To account for that case,
402
        // accumulate all discovered classes, so the parent class may be found in
403
        // a later invocation.
404
        if (!empty($newClasses)) {
405
            // On the assumption that test classes are defined first in files,
406
            // process discovered classes in approximate LIFO order, so as to
407
            // avoid unnecessary reflection.
408
            $this->foundClasses           = array_merge($newClasses, $this->foundClasses);
409
            $this->declaredClassesPointer = count(get_declared_classes());
410
        }
411
 
412
        // The test class's name must match the filename, either in full, or as
413
        // a PEAR/PSR-0 prefixed short name ('NameSpace_ShortName'), or as a
414
        // PSR-1 local short name ('NameSpace\ShortName'). The comparison must be
415
        // anchored to prevent false-positive matches (e.g., 'OtherShortName').
416
        $shortName      = basename($filename, '.php');
417
        $shortNameRegEx = '/(?:^|_|\\\\)' . preg_quote($shortName, '/') . '$/';
418
 
419
        foreach ($this->foundClasses as $i => $className) {
420
            if (preg_match($shortNameRegEx, $className)) {
421
                try {
422
                    $class = new ReflectionClass($className);
423
                    // @codeCoverageIgnoreStart
424
                } catch (ReflectionException $e) {
425
                    throw new Exception(
426
                        $e->getMessage(),
427
                        $e->getCode(),
428
                        $e
429
                    );
430
                }
431
                // @codeCoverageIgnoreEnd
432
 
433
                if ($class->getFileName() == $filename) {
434
                    $newClasses = [$className];
435
                    unset($this->foundClasses[$i]);
436
 
437
                    break;
438
                }
439
            }
440
        }
441
 
442
        foreach ($newClasses as $className) {
443
            try {
444
                $class = new ReflectionClass($className);
445
                // @codeCoverageIgnoreStart
446
            } catch (ReflectionException $e) {
447
                throw new Exception(
448
                    $e->getMessage(),
449
                    $e->getCode(),
450
                    $e
451
                );
452
            }
453
            // @codeCoverageIgnoreEnd
454
 
455
            if (dirname($class->getFileName()) === __DIR__) {
456
                continue;
457
            }
458
 
621 lars 459
            if ($class->isAbstract() && $class->isSubclassOf(TestCase::class)) {
460
                $this->addWarning(
461
                    sprintf(
462
                        'Abstract test case classes with "Test" suffix are deprecated (%s)',
463
                        $class->getName()
464
                    )
465
                );
466
            }
467
 
148 lars 468
            if (!$class->isAbstract()) {
469
                if ($class->hasMethod(BaseTestRunner::SUITE_METHODNAME)) {
470
                    try {
471
                        $method = $class->getMethod(
472
                            BaseTestRunner::SUITE_METHODNAME
473
                        );
474
                        // @codeCoverageIgnoreStart
475
                    } catch (ReflectionException $e) {
476
                        throw new Exception(
477
                            $e->getMessage(),
478
                            $e->getCode(),
479
                            $e
480
                        );
481
                    }
482
                    // @codeCoverageIgnoreEnd
483
 
484
                    if ($method->isStatic()) {
485
                        $this->addTest($method->invoke(null, $className));
486
                    }
487
                } elseif ($class->implementsInterface(Test::class)) {
488
                    // Do we have modern namespacing ('Foo\Bar\WhizBangTest') or old-school namespacing ('Foo_Bar_WhizBangTest')?
489
                    $isPsr0            = (!$class->inNamespace()) && (strpos($class->getName(), '_') !== false);
490
                    $expectedClassName = $isPsr0 ? $className : $shortName;
491
 
492
                    if (($pos = strpos($expectedClassName, '.')) !== false) {
493
                        $expectedClassName = substr(
494
                            $expectedClassName,
495
                            0,
496
                            $pos
497
                        );
498
                    }
499
 
500
                    if ($class->getShortName() !== $expectedClassName) {
501
                        $this->addWarning(
502
                            sprintf(
503
                                "Test case class not matching filename is deprecated\n               in %s\n               Class name was '%s', expected '%s'",
504
                                $filename,
505
                                $class->getShortName(),
506
                                $expectedClassName
507
                            )
508
                        );
509
                    }
510
 
511
                    $this->addTestSuite($class);
512
                }
513
            }
514
        }
515
 
516
        if (count($this->tests) > ++$numTests) {
517
            $this->addWarning(
518
                sprintf(
519
                    "Multiple test case classes per file is deprecated\n               in %s",
520
                    $filename
521
                )
522
            );
523
        }
524
 
525
        $this->numTests = -1;
526
    }
527
 
528
    /**
529
     * Wrapper for addTestFile() that adds multiple test files.
530
     *
531
     * @throws Exception
532
     */
533
    public function addTestFiles(iterable $fileNames): void
534
    {
535
        foreach ($fileNames as $filename) {
536
            $this->addTestFile((string) $filename);
537
        }
538
    }
539
 
540
    /**
541
     * Counts the number of test cases that will be run by this test.
542
     *
543
     * @todo refactor usage of numTests in DefaultResultPrinter
544
     */
545
    public function count(): int
546
    {
547
        $this->numTests = 0;
548
 
549
        foreach ($this as $test) {
550
            $this->numTests += count($test);
551
        }
552
 
553
        return $this->numTests;
554
    }
555
 
556
    /**
557
     * Returns the name of the suite.
558
     */
559
    public function getName(): string
560
    {
561
        return $this->name;
562
    }
563
 
564
    /**
565
     * Returns the test groups of the suite.
566
     *
567
     * @psalm-return list<string>
568
     */
569
    public function getGroups(): array
570
    {
571
        return array_map(
572
            static function ($key): string
573
            {
574
                return (string) $key;
575
            },
576
            array_keys($this->groups)
577
        );
578
    }
579
 
580
    public function getGroupDetails(): array
581
    {
582
        return $this->groups;
583
    }
584
 
585
    /**
586
     * Set tests groups of the test case.
587
     */
588
    public function setGroupDetails(array $groups): void
589
    {
590
        $this->groups = $groups;
591
    }
592
 
593
    /**
594
     * Runs the tests and collects their result in a TestResult.
595
     *
596
     * @throws \PHPUnit\Framework\CodeCoverageException
597
     * @throws \SebastianBergmann\CodeCoverage\InvalidArgumentException
598
     * @throws \SebastianBergmann\CodeCoverage\UnintentionallyCoveredCodeException
599
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
600
     * @throws Warning
601
     */
602
    public function run(TestResult $result = null): TestResult
603
    {
604
        if ($result === null) {
605
            $result = $this->createResult();
606
        }
607
 
608
        if (count($this) === 0) {
609
            return $result;
610
        }
611
 
612
        /** @psalm-var class-string $className */
613
        $className   = $this->name;
614
        $hookMethods = TestUtil::getHookMethods($className);
615
 
616
        $result->startTestSuite($this);
617
 
618
        $test = null;
619
 
620
        if ($this->testCase && class_exists($this->name, false)) {
621
            try {
622
                foreach ($hookMethods['beforeClass'] as $beforeClassMethod) {
623
                    if (method_exists($this->name, $beforeClassMethod)) {
624
                        if ($missingRequirements = TestUtil::getMissingRequirements($this->name, $beforeClassMethod)) {
625
                            $this->markTestSuiteSkipped(implode(PHP_EOL, $missingRequirements));
626
                        }
627
 
628
                        call_user_func([$this->name, $beforeClassMethod]);
629
                    }
630
                }
621 lars 631
            } catch (SkippedTestSuiteError|SkippedTestError $error) {
148 lars 632
                foreach ($this->tests() as $test) {
633
                    $result->startTest($test);
634
                    $result->addFailure($test, $error, 0);
635
                    $result->endTest($test, 0);
636
                }
637
 
638
                $result->endTestSuite($this);
639
 
640
                return $result;
641
            } catch (Throwable $t) {
642
                $errorAdded = false;
643
 
644
                foreach ($this->tests() as $test) {
645
                    if ($result->shouldStop()) {
646
                        break;
647
                    }
648
 
649
                    $result->startTest($test);
650
 
651
                    if (!$errorAdded) {
652
                        $result->addError($test, $t, 0);
653
 
654
                        $errorAdded = true;
655
                    } else {
656
                        $result->addFailure(
657
                            $test,
658
                            new SkippedTestError('Test skipped because of an error in hook method'),
659
 
660
                        );
661
                    }
662
 
663
                    $result->endTest($test, 0);
664
                }
665
 
666
                $result->endTestSuite($this);
667
 
668
                return $result;
669
            }
670
        }
671
 
672
        foreach ($this as $test) {
673
            if ($result->shouldStop()) {
674
                break;
675
            }
676
 
677
            if ($test instanceof TestCase || $test instanceof self) {
678
                $test->setBeStrictAboutChangesToGlobalState($this->beStrictAboutChangesToGlobalState);
679
                $test->setBackupGlobals($this->backupGlobals);
680
                $test->setBackupStaticAttributes($this->backupStaticAttributes);
681
                $test->setRunTestInSeparateProcess($this->runTestInSeparateProcess);
682
            }
683
 
684
            $test->run($result);
685
        }
686
 
687
        if ($this->testCase && class_exists($this->name, false)) {
688
            foreach ($hookMethods['afterClass'] as $afterClassMethod) {
689
                if (method_exists($this->name, $afterClassMethod)) {
690
                    try {
691
                        call_user_func([$this->name, $afterClassMethod]);
692
                    } catch (Throwable $t) {
693
                        $message = "Exception in {$this->name}::{$afterClassMethod}" . PHP_EOL . $t->getMessage();
694
                        $error   = new SyntheticError($message, 0, $t->getFile(), $t->getLine(), $t->getTrace());
695
 
696
                        $placeholderTest = clone $test;
697
                        $placeholderTest->setName($afterClassMethod);
698
 
699
                        $result->startTest($placeholderTest);
700
                        $result->addFailure($placeholderTest, $error, 0);
701
                        $result->endTest($placeholderTest, 0);
702
                    }
703
                }
704
            }
705
        }
706
 
707
        $result->endTestSuite($this);
708
 
709
        return $result;
710
    }
711
 
712
    public function setRunTestInSeparateProcess(bool $runTestInSeparateProcess): void
713
    {
714
        $this->runTestInSeparateProcess = $runTestInSeparateProcess;
715
    }
716
 
717
    public function setName(string $name): void
718
    {
719
        $this->name = $name;
720
    }
721
 
722
    /**
723
     * Returns the tests as an enumeration.
724
     *
725
     * @return Test[]
726
     */
727
    public function tests(): array
728
    {
729
        return $this->tests;
730
    }
731
 
732
    /**
733
     * Set tests of the test suite.
734
     *
735
     * @param Test[] $tests
736
     */
737
    public function setTests(array $tests): void
738
    {
739
        $this->tests = $tests;
740
    }
741
 
742
    /**
743
     * Mark the test suite as skipped.
744
     *
745
     * @param string $message
746
     *
747
     * @throws SkippedTestSuiteError
748
     *
749
     * @psalm-return never-return
750
     */
751
    public function markTestSuiteSkipped($message = ''): void
752
    {
753
        throw new SkippedTestSuiteError($message);
754
    }
755
 
756
    /**
757
     * @param bool $beStrictAboutChangesToGlobalState
758
     */
759
    public function setBeStrictAboutChangesToGlobalState($beStrictAboutChangesToGlobalState): void
760
    {
761
        if (null === $this->beStrictAboutChangesToGlobalState && is_bool($beStrictAboutChangesToGlobalState)) {
762
            $this->beStrictAboutChangesToGlobalState = $beStrictAboutChangesToGlobalState;
763
        }
764
    }
765
 
766
    /**
767
     * @param bool $backupGlobals
768
     */
769
    public function setBackupGlobals($backupGlobals): void
770
    {
771
        if (null === $this->backupGlobals && is_bool($backupGlobals)) {
772
            $this->backupGlobals = $backupGlobals;
773
        }
774
    }
775
 
776
    /**
777
     * @param bool $backupStaticAttributes
778
     */
779
    public function setBackupStaticAttributes($backupStaticAttributes): void
780
    {
781
        if (null === $this->backupStaticAttributes && is_bool($backupStaticAttributes)) {
782
            $this->backupStaticAttributes = $backupStaticAttributes;
783
        }
784
    }
785
 
786
    /**
787
     * Returns an iterator for this test suite.
788
     */
789
    public function getIterator(): Iterator
790
    {
791
        $iterator = new TestSuiteIterator($this);
792
 
793
        if ($this->iteratorFilter !== null) {
794
            $iterator = $this->iteratorFilter->factory($iterator, $this);
795
        }
796
 
797
        return $iterator;
798
    }
799
 
800
    public function injectFilter(Factory $filter): void
801
    {
802
        $this->iteratorFilter = $filter;
803
 
804
        foreach ($this as $test) {
805
            if ($test instanceof self) {
806
                $test->injectFilter($filter);
807
            }
808
        }
809
    }
810
 
811
    /**
812
     * @psalm-return array<int,string>
813
     */
814
    public function warnings(): array
815
    {
816
        return array_unique($this->warnings);
817
    }
818
 
819
    /**
820
     * @return list<ExecutionOrderDependency>
821
     */
822
    public function provides(): array
823
    {
824
        if ($this->providedTests === null) {
825
            $this->providedTests = [];
826
 
827
            if (is_callable($this->sortId(), true)) {
828
                $this->providedTests[] = new ExecutionOrderDependency($this->sortId());
829
            }
830
 
831
            foreach ($this->tests as $test) {
832
                if (!($test instanceof Reorderable)) {
833
                    // @codeCoverageIgnoreStart
834
                    continue;
835
                    // @codeCoverageIgnoreEnd
836
                }
837
                $this->providedTests = ExecutionOrderDependency::mergeUnique($this->providedTests, $test->provides());
838
            }
839
        }
840
 
841
        return $this->providedTests;
842
    }
843
 
844
    /**
845
     * @return list<ExecutionOrderDependency>
846
     */
847
    public function requires(): array
848
    {
849
        if ($this->requiredTests === null) {
850
            $this->requiredTests = [];
851
 
852
            foreach ($this->tests as $test) {
853
                if (!($test instanceof Reorderable)) {
854
                    // @codeCoverageIgnoreStart
855
                    continue;
856
                    // @codeCoverageIgnoreEnd
857
                }
858
                $this->requiredTests = ExecutionOrderDependency::mergeUnique(
859
                    ExecutionOrderDependency::filterInvalid($this->requiredTests),
860
                    $test->requires()
861
                );
862
            }
863
 
864
            $this->requiredTests = ExecutionOrderDependency::diff($this->requiredTests, $this->provides());
865
        }
866
 
867
        return $this->requiredTests;
868
    }
869
 
870
    public function sortId(): string
871
    {
872
        return $this->getName() . '::class';
873
    }
874
 
875
    /**
876
     * Creates a default TestResult object.
877
     */
878
    protected function createResult(): TestResult
879
    {
880
        return new TestResult;
881
    }
882
 
883
    /**
884
     * @throws Exception
885
     */
886
    protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method): void
887
    {
888
        $methodName = $method->getName();
889
 
890
        $test = (new TestBuilder)->build($class, $methodName);
891
 
892
        if ($test instanceof TestCase || $test instanceof DataProviderTestSuite) {
893
            $test->setDependencies(
894
                TestUtil::getDependencies($class->getName(), $methodName)
895
            );
896
        }
897
 
898
        $this->addTest(
899
            $test,
900
            TestUtil::getGroups($class->getName(), $methodName)
901
        );
902
    }
903
 
904
    private function clearCaches(): void
905
    {
906
        $this->numTests      = -1;
907
        $this->providedTests = null;
908
        $this->requiredTests = null;
909
    }
910
 
911
    private function containsOnlyVirtualGroups(array $groups): bool
912
    {
913
        foreach ($groups as $group) {
914
            if (strpos($group, '__phpunit_') !== 0) {
915
                return false;
916
            }
917
        }
918
 
919
        return true;
920
    }
921
}