Completed
Push — master ( aa144c...0ba72b )
by Jonathan
02:44
created

TestFinder::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
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 5
    }
33
34
    public function changed(bool $changed = true) : self
35
    {
36
        $this->changed = $changed;
37
38
        return $this;
39
    }
40
41
    public function filter(string $filter = null) : self
42
    {
43
        $this->getFinder()->path($filter);
44
45
        return $this;
46
    }
47
48
    public function contains(string $contains = null) : self
49
    {
50
        $this->getFinder()->contains($contains);
51
52
        return $this;
53
    }
54
55
    public function notContains(string $notContains = null) : self
56
    {
57
        $this->getFinder()->notContains($notContains);
58
59
        return $this;
60
    }
61
62 3
    public function inGroup(string $group = null) : self
63
    {
64 3
        $this->getFinder()->contains(sprintf('@group %s', $group));
65
66 3
        return $this;
67
    }
68
69 3
    public function inGroups(array $groups = []) : self
70
    {
71 3
        foreach ($groups as $group) {
72 3
            $this->inGroup($group);
73
        }
74
75 3
        return $this;
76
    }
77
78 1
    public function notInGroup(string $group = null) : self
79
    {
80 1
        $this->getFinder()->notContains(sprintf('@group %s', $group));
81
82 1
        return $this;
83
    }
84
85 1
    public function notInGroups(array $groups = []) : self
86
    {
87 1
        foreach ($groups as $group) {
88 1
            $this->notInGroup($group);
89
        }
90
91 1
        return $this;
92
    }
93
94
    public function findTestFilesByFilter(string $filter) : array
95
    {
96
        $this->filter($filter);
97
98
        return $this->buildFilesArrayFromFinder();
99
    }
100
101 3
    public function findTestFilesInGroups(array $groups) : array
102
    {
103 3
        $this->inGroups($groups);
104
105 3
        return $this->buildFilesArrayFromFinder();
106
    }
107
108 1
    public function findTestFilesExcludingGroups(array $excludeGroups) : array
109
    {
110 1
        $this->notInGroups($excludeGroups);
111
112 1
        return $this->buildFilesArrayFromFinder();
113
    }
114
115 1
    public function findAllTestFiles() : array
116
    {
117 1
        return $this->buildFilesArrayFromFinder();
118
    }
119
120
    public function findChangedTestFiles() : array
121
    {
122
        $command = "git status --porcelain | grep -e '^\(.*\)Test.php$' | cut -c 3-";
123
124
        return $this->buildFilesArrayFromFindCommand($command);
125
    }
126
127
    public function getFiles() : array
128
    {
129
        if ($this->changed) {
130
            return $this->findChangedTestFiles();
131
        }
132
133
        return $this->buildFilesArrayFromFinder();
134
    }
135
136 5
    private function getFinder() : Finder
137
    {
138 5
        if ($this->finder === null) {
139 5
            $this->finder = Finder::create()
140 5
                ->files()
141 5
                ->name('*Test.php')
142 5
                ->in($this->testsDirectory)
143 5
                ->sortByName();
144
        }
145
146 5
        return $this->finder;
147
    }
148
149
    private function buildFilesArrayFromFinder() : array
150
    {
151 5
        return array_values(array_map(function($file) {
152 4
            return $file->getPathName();
153 5
        }, iterator_to_array($this->getFinder())));
154
    }
155
156
    private function buildFilesArrayFromFindCommand(string $command) : array
157
    {
158
        $output = trim(shell_exec($command));
159
160
        return $output ? array_map('trim', explode("\n", $output)) : [];
161
    }
162
}
163