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

TestFinder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 65.96%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 116
ccs 31
cts 47
cp 0.6596
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A changed() 0 6 1
A inGroup() 0 4 1
A inGroups() 0 8 2
A notInGroup() 0 4 1
A notInGroups() 0 8 2
A findTestFilesByFilter() 0 6 1
A findTestFilesInGroups() 0 6 1
A findTestFilesExcludingGroups() 0 6 1
A findAllTestFiles() 0 4 1
A findChangedTestFiles() 0 6 1
A getFiles() 0 8 2
A buildFilesArrayFromFinder() 0 6 1
A buildFilesArrayFromFindCommand() 0 6 2
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