Completed
Push — master ( 80d94b...95c442 )
by Jonathan
31s
created

TestFinder::notInGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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
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
    public function notContains(string $notContains = null) : self
62
    {
63
        $this->finder->notContains($notContains);
64
65
        return $this;
66
    }
67
68 3
    public function inGroup(string $group = null) : self
69
    {
70 3
        $this->finder->contains(sprintf('@group %s', $group));
71
72 3
        return $this;
73
    }
74
75 3
    public function inGroups(array $groups = []) : self
76
    {
77 3
        foreach ($groups as $group) {
78 3
            $this->inGroup($group);
79
        }
80
81 3
        return $this;
82
    }
83
84 1
    public function notInGroup(string $group = null) : self
85
    {
86 1
        $this->finder->notContains(sprintf('@group %s', $group));
87
88 1
        return $this;
89
    }
90
91 1
    public function notInGroups(array $groups = []) : self
92
    {
93 1
        foreach ($groups as $group) {
94 1
            $this->notInGroup($group);
95
        }
96
97 1
        return $this;
98
    }
99
100
    public function findTestFilesByFilter(string $filter) : array
101
    {
102
        $this->filter($filter);
103
104
        return $this->buildFilesArrayFromFinder();
105
    }
106
107 3
    public function findTestFilesInGroups(array $groups) : array
108
    {
109 3
        $this->inGroups($groups);
110
111 3
        return $this->buildFilesArrayFromFinder();
112
    }
113
114 1
    public function findTestFilesExcludingGroups(array $excludeGroups) : array
115
    {
116 1
        $this->notInGroups($excludeGroups);
117
118 1
        return $this->buildFilesArrayFromFinder();
119
    }
120
121 1
    public function findAllTestFiles() : array
122
    {
123 1
        return $this->buildFilesArrayFromFinder();
124
    }
125
126
    public function findChangedTestFiles() : array
127
    {
128
        $command = "git status --porcelain | grep -e '^\(.*\)Test.php$' | cut -c 3-";
129
130
        return $this->buildFilesArrayFromFindCommand($command);
131
    }
132
133
    public function getFiles() : array
134
    {
135
        if ($this->changed) {
136
            return $this->findChangedTestFiles();
137
        }
138
139
        return $this->buildFilesArrayFromFinder();
140
    }
141
142
    private function buildFilesArrayFromFinder() : array
143
    {
144 5
        return array_values(array_map(function($file) {
145 4
            return $file->getPathName();
146 5
        }, iterator_to_array($this->finder)));
147
    }
148
149
    private function buildFilesArrayFromFindCommand(string $command) : array
150
    {
151
        $output = trim(shell_exec($command));
152
153
        return $output ? array_map('trim', explode("\n", $output)) : [];
154
    }
155
}
156