Passed
Push — dev ( 62ccfd...bd0680 )
by Fike
02:45
created

RoboFile::coverageReport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use AmaTeam\Image\Projection\Test\Support\Fixture\Projection\Manager;
4
5
class RoboFile extends \Robo\Tasks // NOSONAR
6
{
7
    const COVERAGE_OPTION = 'coverage';
8
    const DEFAULT_TEST_OPTIONS = ['coverage' => false];
9
    const COVERAGE_DIRECTORY = 'Coverage';
10
11
    public function testSetup()
12
    {
13
        $this
14
            ->taskFilesystemStack()
15
            ->mkdir(self::testsMetadataDir())
16
            ->run()
17
            ->stopOnFail();
18
        $this
19
            ->taskExecStack()
20
            ->exec([self::binary('codecept'), 'build'])
21
            ->run()
22
            ->stopOnFail();
23
        $manager = new Manager();
24
        $manager->install();
25
    }
26
27
    private function runTests($suite = null, array $options = self::DEFAULT_TEST_OPTIONS)
28
    {
29
        $suite = $suite ? self::normalizeTestSuite($suite) : null;
30
        $directory = $suite ? ['Suite', $suite] : [];
31
        $coverageDir = $directory;
32
        $coverageDir[] = self::COVERAGE_DIRECTORY;
33
        $this
34
            ->taskFilesystemStack()
35
            ->mkdir(self::testsMetadataDir($coverageDir))
36
            ->run()
37
            ->stopOnFail();
38
        $task = $this
39
            ->taskCodecept(self::binary('codecept'))
40
            ->suite($suite)
41
            ->xml(self::testsMetadataDir(array_merge($directory, ['junit.xml'])))
42
            ->debug();
43
        if ($options[self::COVERAGE_OPTION]) {
44
            $task
45
                ->coverage(self::testsMetadataDir(array_merge($coverageDir, ['coverage.cov'])))
46
                ->coverageXml(self::testsMetadataDir(array_merge($coverageDir, ['coverage.xml'])));
47
        }
48
        return $task->run();
49
    }
50
51
    public function testUnit($options = self::DEFAULT_TEST_OPTIONS)
52
    {
53
        return $this->runTests('Unit', $options);
54
    }
55
56
    public function testFunctional($options = self::DEFAULT_TEST_OPTIONS)
57
    {
58
        return $this->runTests('Functional', $options);
59
    }
60
61
    public function testIntegration($options = self::DEFAULT_TEST_OPTIONS)
62
    {
63
        return $this->runTests('Integration', $options);
64
    }
65
66
    public function testAcceptance($options = self::DEFAULT_TEST_OPTIONS)
67
    {
68
        return $this->runTests('Acceptance', $options);
69
    }
70
71
    public function test()
72
    {
73
        $this->testClean()->stopOnFail();
74
        $this->runTests()->stopOnFail();
75
        return $this->coverageReport();
76
    }
77
78
    public function testClean()
79
    {
80
        return $this
81
            ->taskCleanDir([
82
                self::testsReportDir(),
83
                self::testsMetadataDir()
84
            ])
85
            ->run();
86
    }
87
88
    public function testReport()
89
    {
90
        $this->coverageReport()->stopOnFail();
91
        return $this->allureReport();
92
    }
93
94
    public function allureReport()
95
    {
96
        $command = [
97
            'allure',
98
            'generate',
99
            '--clean',
100
            '-o',
101
            self::testsReportDir(['Allure']),
102
            '--',
103
            self::testsMetadataDir(['Allure'])
104
        ];
105
        return $this
106
            ->taskExecStack()
107
            ->exec($command)
108
            ->run();
109
    }
110
111
    public function coveragePublish($suite = null)
112
    {
113
        $suite = self::normalizeTestSuite($suite);
114
        $directory = $suite ? ['Suite', $suite] : [];
115
        $path = array_merge($directory, ['Coverage', 'coverage.xml']);
116
        $this
117
            ->taskExecStack()
118
            ->exec([
119
                self::binary('coveralls'),
120
                '-x',
121
                self::testsMetadataDir($path)
122
            ])
123
            ->run();
124
    }
125
126
    public function coverageReport()
127
    {
128
        $command = [
129
            self::binary('phpcov'),
130
            'merge',
131
            '--html',
132
            self::testsReportDir([self::COVERAGE_DIRECTORY]),
133
            '--',
134
            self::testsMetadataDir()
135
        ];
136
        return $this
137
            ->taskExecStack()
138
            ->exec($command)
139
            ->run();
140
    }
141
142
    public function lint()
143
    {
144
        $rules = [
145
            'cleancode',
146
            'codesize',
147
            'controversial',
148
            'design',
149
            'naming',
150
            'unusedcode'
151
        ];
152
        $commands = [
153
            [
154
                self::binary('phpcs'),
155
                '--standard=PSR2',
156
                self::sourcesDir()
157
            ],
158
            [
159
                self::binary('phpmd'),
160
                self::sourcesDir(),
161
                'html',
162
                implode(',', $rules),
163
                '--reportfile',
164
                self::testsReportDir(['Lint', 'phpmd.html'])
165
            ]
166
        ];
167
        $this
168
            ->taskFilesystemStack()
169
            ->mkdir(self::testsReportDir(['Lint']))
170
            ->run();
171
        $executor = $this
172
            ->taskParallelExec()
173
            ->printed();
174
        foreach ($commands as $command) {
175
            $command = array_map('escapeshellarg', $command);
176
            $executor->process(implode(' ', $command));
177
        }
178
        return $executor->run();
179
    }
180
181
    private static function path(array $path = [])
182
    {
183
        array_unshift($path, __DIR__);
184
        return implode(DIRECTORY_SEPARATOR, $path);
185
    }
186
187
    private static function sourcesDir(array $path = [])
188
    {
189
        array_unshift($path, 'src');
190
        return self::path($path);
191
    }
192
193
    private static function testsDir(array $path = [])
194
    {
195
        array_unshift($path, 'tests');
196
        return self::path($path);
197
    }
198
199
    private static function testsMetadataDir(array $path = [])
200
    {
201
        array_unshift($path, 'Metadata');
202
        return self::testsDir($path);
203
    }
204
205
    private static function testsReportDir(array $path = [])
206
    {
207
        array_unshift($path, 'Report');
208
        return self::testsDir($path);
209
    }
210
211
    private static function binary($name)
212
    {
213
        return self::path(['bin', $name]);
214
    }
215
216
    private static function normalizeTestSuite($suite)
217
    {
218
        if (!$suite) {
219
            return null;
220
        }
221
        return strtoupper($suite[0]) . strtolower(substr($suite, 1));
222
    }
223
}
224