1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPChunkit; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @testClass PHPChunkit\Test\TestFinderTest |
11
|
|
|
*/ |
12
|
|
|
class TestFinder |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $testsDirectory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $changed = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Finder |
26
|
|
|
*/ |
27
|
|
|
private $finder; |
28
|
|
|
|
29
|
5 |
|
public function __construct(string $testsDirectory) |
30
|
|
|
{ |
31
|
5 |
|
$this->testsDirectory = $testsDirectory; |
32
|
|
|
|
33
|
5 |
|
$this->finder = Finder::create() |
34
|
5 |
|
->files() |
35
|
5 |
|
->name('*Test.php') |
36
|
5 |
|
->in($this->testsDirectory) |
37
|
5 |
|
->sortByName(); |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
public function changed(bool $changed = true) : self |
41
|
|
|
{ |
42
|
|
|
$this->changed = $changed; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function filter(string $filter = null) : self |
48
|
|
|
{ |
49
|
|
|
$this->finder->path($filter); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function contains(string $contains = null) : self |
55
|
|
|
{ |
56
|
|
|
$this->finder->contains($contains); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
public function inGroup(string $group = null) : self |
62
|
|
|
{ |
63
|
3 |
|
$this->finder->contains(sprintf('@group %s', $group)); |
64
|
|
|
|
65
|
3 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function inGroups(array $groups = []) : self |
69
|
|
|
{ |
70
|
3 |
|
foreach ($groups as $group) { |
71
|
3 |
|
$this->inGroup($group); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function notInGroup(string $group = null) : self |
78
|
|
|
{ |
79
|
1 |
|
$this->finder->notContains(sprintf('@group %s', $group)); |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function notInGroups(array $groups = []) : self |
85
|
|
|
{ |
86
|
1 |
|
foreach ($groups as $group) { |
87
|
1 |
|
$this->notInGroup($group); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function findTestFilesByFilter(string $filter) : array |
94
|
|
|
{ |
95
|
|
|
$this->filter($filter); |
96
|
|
|
|
97
|
|
|
return $this->buildFilesArrayFromFinder(); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
public function findTestFilesInGroups(array $groups) : array |
101
|
|
|
{ |
102
|
3 |
|
$this->inGroups($groups); |
103
|
|
|
|
104
|
3 |
|
return $this->buildFilesArrayFromFinder(); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function findTestFilesExcludingGroups(array $excludeGroups) : array |
108
|
|
|
{ |
109
|
1 |
|
$this->notInGroups($excludeGroups); |
110
|
|
|
|
111
|
1 |
|
return $this->buildFilesArrayFromFinder(); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function findAllTestFiles() : array |
115
|
|
|
{ |
116
|
1 |
|
return $this->buildFilesArrayFromFinder(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function findChangedTestFiles() : array |
120
|
|
|
{ |
121
|
|
|
$command = "git status --porcelain | grep -e '^\(.*\)Test.php$' | cut -c 3-"; |
122
|
|
|
|
123
|
|
|
return $this->buildFilesArrayFromFindCommand($command); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getFiles() : array |
127
|
|
|
{ |
128
|
|
|
if ($this->changed) { |
129
|
|
|
return $this->findChangedTestFiles(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->buildFilesArrayFromFinder(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function buildFilesArrayFromFinder() : array |
136
|
|
|
{ |
137
|
5 |
|
return array_values(array_map(function($file) { |
138
|
4 |
|
return $file->getPathName(); |
139
|
5 |
|
}, iterator_to_array($this->finder))); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function buildFilesArrayFromFindCommand(string $command) : array |
143
|
|
|
{ |
144
|
|
|
$output = trim(shell_exec($command)); |
145
|
|
|
|
146
|
|
|
return $output ? array_map('trim', explode("\n", $output)) : []; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|