1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace SprykerTest\Shared\Testify\Filter; |
||
9 | |||
10 | use PHPUnit\Framework\TestSuite; |
||
11 | use RecursiveFilterIterator; |
||
12 | use RecursiveIterator; |
||
13 | |||
14 | /** |
||
15 | * Filters tests that have all the requested groups |
||
16 | */ |
||
17 | class InclusiveGroupFilterIterator extends RecursiveFilterIterator |
||
18 | { |
||
19 | /** |
||
20 | * @var array<string> |
||
21 | */ |
||
22 | protected $testGroups = []; |
||
23 | |||
24 | /** |
||
25 | * @param \RecursiveIterator $iterator |
||
26 | * @param array<string> $groups |
||
27 | * @param \PHPUnit\Framework\TestSuite $suite |
||
28 | */ |
||
29 | public function __construct(RecursiveIterator $iterator, array $groups, TestSuite $suite) |
||
30 | { |
||
31 | parent::__construct($iterator); |
||
32 | |||
33 | $this->setTestGroups( |
||
34 | $this->getSuiteGroupsIntersection($suite, $groups), |
||
35 | ); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return bool |
||
40 | */ |
||
41 | public function accept(): bool |
||
42 | { |
||
43 | $test = $this->getInnerIterator()->current(); |
||
44 | |||
45 | if ($test instanceof TestSuite) { |
||
46 | return true; |
||
47 | } |
||
48 | |||
49 | return in_array( |
||
50 | spl_object_hash($test), |
||
51 | $this->testGroups, |
||
52 | true, |
||
53 | ); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param \PHPUnit\Framework\TestSuite $suite |
||
58 | * @param array<string> $inclusiveGroups |
||
59 | * |
||
60 | * @return array |
||
61 | */ |
||
62 | protected function getSuiteGroupsIntersection(TestSuite $suite, array $inclusiveGroups): array |
||
63 | { |
||
64 | $suiteGroups = array_intersect_key( |
||
65 | $suite->getGroupDetails(), |
||
66 | array_flip($inclusiveGroups), |
||
67 | ); |
||
68 | |||
69 | if (array_diff_key(array_flip($inclusiveGroups), $suiteGroups) === []) { |
||
70 | return $suiteGroups; |
||
71 | } |
||
72 | |||
73 | return []; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param array<string> $suiteGroups |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | protected function setTestGroups(array $suiteGroups): void |
||
82 | { |
||
83 | foreach ($suiteGroups as $tests) { |
||
84 | $testHashes = array_map( |
||
85 | 'spl_object_hash', |
||
86 | $tests, |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
87 | ); |
||
88 | |||
89 | $this->testGroups = count($this->testGroups) === 0 |
||
90 | ? $testHashes |
||
91 | : array_intersect($testHashes, $this->testGroups); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 |