Passed
Pull Request — develop (#4)
by ANTHONIUS
04:07
created

RoboFile.php (1 issue)

1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
use Lurker\Event\FilesystemEvent;
15
use Robo\Tasks;
16
17
/**
18
 * This is project's console commands configuration for Robo task runner.
19
 *
20
 * @see http://robo.li/
21
 */
22
class RoboFile extends Tasks
23
{
24
    private $coverage = false;
25
26
    private $watch = false;
27
28
    /**
29
     * @param array $options
30
     */
31
    public function watch($options = ['coverage' => false])
32
    {
33
        $this->watch = true;
34
35
        $paths = [
36
            'src',
37
            'tests',
38
            'spec',
39
            'features',
40
        ];
41
42
        $this->taskWatch()
43
            ->monitor(
44
                $paths,
45
                function (FilesystemEvent $event) use ($options) {
46
                    $resource = (string) $event->getResource();
47
                    if (
48
                        false !== strpos($resource, 'build')
49
                        || false !== strpos($resource, 'var')
50
                    ) {
51
                        return;
52
                    }
53
                    $this->test($options);
54
                },
55
                FilesystemEvent::ALL
56
            )
57
            ->run();
58
    }
59
60
    public function test()
61
    {
62
        $this->taskExec('clear')->run();
63
64
        if ($this->coverage) {
65
            $this->taskFilesystemStack()
66
                ->mkdir(__DIR__.'/build', 0775)
67
                ->mkdir(__DIR__.'/build/cov', 0775)
68
                ->run();
69
        }
70
71
        /** @var \Robo\Result[] $results */
72
        $results   = [];
73
        $results[] = $this->configurePhpSpec()->run();
74
        //$results[] = $this->configurePhpUnit()->run();
75
76
        if (!$this->watch) {
77
            //$results[] = $this->configureBehat()->run();
78
        }
79
80
        $hasError = false;
81
        foreach ($results as $result) {
82
            if (0 !== $result->getExitCode()) {
83
                $hasError = true;
84
            }
85
        }
86
87
        if ($this->coverage) {
88
            $this->mergeCoverage();
89
        }
90
91
        if (!$this->watch) {
92
            if ($hasError) {
93
                throw new \Robo\Exception\TaskException($this, 'Tests failed');
94
            }
95
        }
96
    }
97
98
    public function coverage()
99
    {
100
        $this->coverage = true;
101
        $this->test();
102
    }
103
104
    public function mergeCoverage()
105
    {
106
        $this
107
            ->taskExec('phpdbg -qrr ./vendor/bin/phpcov')
108
            ->arg('merge')
109
            ->option('clover', 'build/logs/clover.xml')
110
            ->option('html', 'build/html')
111
            ->option('text')
112
            ->option('ansi')
113
            ->arg('build/cov')
114
            ->run();
115
    }
116
117
    /**
118
     * @return \Robo\Task\Base\Exec|\Robo\Task\Testing\Behat
119
     */
120
    private function configureBehat()
0 ignored issues
show
The method configureBehat() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
121
    {
122
        $task = $this->taskBehat();
123
        $task->noInteraction()
124
            ->format('progress')
125
            ->colors();
126
127
        if ($this->coverage) {
128
            $task->option('coverage');
129
            $command = $task->getCommand();
130
            $task    = $this->taskExec('phpdbg -qrr '.$command);
131
        } else {
132
            $task->option('tags', '~@remote');
133
        }
134
135
        return $task;
136
    }
137
138
    /**
139
     * @return \Robo\Task\Base\Exec|\Robo\Task\Testing\Phpspec
140
     */
141
    private function configurePhpSpec()
142
    {
143
        $task = $this->taskPhpspec();
144
        $task->noCodeGeneration()
145
            ->noInteraction()
146
            ->format('dot');
147
148
        if ($this->coverage) {
149
            $task->option('coverage');
150
            $task = $this->taskExec('phpdbg -qrr '.$task->getCommand());
151
        }
152
153
        return $task;
154
    }
155
156
    /**
157
     * @return \Robo\Task\Base\Exec|\Robo\Task\Testing\PHPUnit
158
     */
159
    private function configurePhpUnit()
160
    {
161
        $task = $this->taskPhpUnit();
162
163
        if ($this->coverage) {
164
            $task = $this->taskExec('phpdbg -qrr '.$task->getCommand());
165
            $task->option('coverage-php', 'build/cov/01-phpunit.cov')
166
                ->option('coverage-html', 'build/phpunit');
167
        }
168
169
        return $task;
170
    }
171
}
172