Completed
Push — stable ( 8ba0c8...0d8cdb )
by Nuno
17:40 queued 15:16
created

TestCommand::handle()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 16
cp 0
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 0
crap 42
1
<?php
2
3
namespace NunoMaduro\Collision\Adapters\Laravel\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use RuntimeException;
8
use Symfony\Component\Process\Exception\ProcessSignaledException;
9
use Symfony\Component\Process\Process;
10
11
class TestCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'test {--without-tty : Disable output to TTY}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Run the application tests';
26
27
    /**
28
     * The arguments to be used while calling phpunit.
29
     *
30
     * @var array
31
     */
32
    protected $arguments = [
33
        '--printer',
34
        'NunoMaduro\Collision\Adapters\Phpunit\Printer',
35
    ];
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return mixed
41
     */
42
    public function handle()
43
    {
44
        $options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2);
45
46
        $process = (new Process(array_merge(
47
            $this->binary(), array_merge(
48
                $this->arguments,
49
                $this->phpunitArguments($options)
50
            )
51
        )))->setTimeout(null);
52
53
        try {
54
            $process->setTty(! $this->option('without-tty'));
55
        } catch (RuntimeException $e) {
56
            $this->output->writeln('Warning: '.$e->getMessage());
57
        }
58
59
        try {
60
            return $process->run(function ($type, $line) {
61
                $this->output->write($line);
62
            });
63
        } catch (ProcessSignaledException $e) {
64
            if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
65
                throw $e;
66
            }
67
        }
68
    }
69
70
    /**
71
     * Get the PHP binary to execute.
72
     *
73
     * @return array
74
     */
75
    protected function binary()
76
    {
77
        if ('phpdbg' === PHP_SAPI) {
78
            return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit'];
79
        }
80
81
        return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit'];
82
    }
83
84
    /**
85
     * Get the array of arguments for running PHPUnit.
86
     *
87
     * @param  array  $options
88
     *
89
     * @return array
90
     */
91
    protected function phpunitArguments($options)
92
    {
93
        $options = array_values(array_filter($options, function ($option) {
94
            return ! Str::startsWith($option, '--env=');
95
        }));
96
97
        if (! file_exists($file = base_path('phpunit.xml'))) {
98
            $file = base_path('phpunit.xml.dist');
99
        }
100
101
        return array_merge(['-c', $file], $options);
102
    }
103
}
104