Passed
Push — stable ( 0d8cdb...757352 )
by Nuno
02:03
created

TestCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
     * Create a new command instance.
39
     *
40
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
41
     */
42
    public function __construct()
43
    {
44
        parent::__construct();
45
46
        $this->ignoreValidationErrors();
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return mixed
53
     */
54
    public function handle()
55
    {
56
        $options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2);
57
58
        $process = (new Process(array_merge(
59
            $this->binary(), array_merge(
60
                $this->arguments,
61
                $this->phpunitArguments($options)
62
            )
63
        )))->setTimeout(null);
64
65
        try {
66
            $process->setTty(! $this->option('without-tty'));
67
        } catch (RuntimeException $e) {
68
            $this->output->writeln('Warning: '.$e->getMessage());
69
        }
70
71
        try {
72
            return $process->run(function ($type, $line) {
73
                $this->output->write($line);
74
            });
75
        } catch (ProcessSignaledException $e) {
76
            if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
77
                throw $e;
78
            }
79
        }
80
    }
81
82
    /**
83
     * Get the PHP binary to execute.
84
     *
85
     * @return array
86
     */
87
    protected function binary()
88
    {
89
        if ('phpdbg' === PHP_SAPI) {
90
            return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit'];
91
        }
92
93
        return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit'];
94
    }
95
96
    /**
97
     * Get the array of arguments for running PHPUnit.
98
     *
99
     * @param  array  $options
100
     *
101
     * @return array
102
     */
103
    protected function phpunitArguments($options)
104
    {
105
        $options = array_values(array_filter($options, function ($option) {
106
            return ! Str::startsWith($option, '--env=');
107
        }));
108
109
        if (! file_exists($file = base_path('phpunit.xml'))) {
110
            $file = base_path('phpunit.xml.dist');
111
        }
112
113
        return array_merge(['-c', $file], $options);
114
    }
115
}
116