Completed
Pull Request — master (#38)
by Jonathan
02:50
created

TestFinder::buildFilesArrayFromFinder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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