Passed
Push — stable ( 1a7c9c...235132 )
by Nuno
02:25
created

TestCommand::phpunitEnvs()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 31

Duplication

Lines 14
Ratio 45.16 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 14
loc 31
ccs 0
cts 15
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 10
nop 0
crap 90
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
        $envs = $this->phpunitEnvs();
59
60
        $process = (new Process(array_merge(
61
            $this->binary(), array_merge(
62
                $this->arguments,
63
                $this->phpunitArguments($options)
64
            )
65
        ), null, $envs))->setTimeout(null);
66
67
        try {
68
            $process->setTty(! $this->option('without-tty'));
69
        } catch (RuntimeException $e) {
70
            $this->output->writeln('Warning: ' . $e->getMessage());
71
        }
72
73
        try {
74
            return $process->run(function ($type, $line) {
75
                $this->output->write($line);
76
            });
77
        } catch (ProcessSignaledException $e) {
78
            if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
79
                throw $e;
80
            }
81
        }
82
    }
83
84
    /**
85
     * Get the PHP binary to execute.
86
     *
87
     * @return array
88
     */
89
    protected function binary()
90
    {
91
        if ('phpdbg' === PHP_SAPI) {
92
            return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit'];
93
        }
94
95
        return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit'];
96
    }
97
98
    /**
99
     * Get the array of arguments for running PHPUnit.
100
     *
101
     * @param  array  $options
102
     *
103
     * @return array
104
     */
105
    protected function phpunitArguments($options)
106
    {
107
        $options = array_values(array_filter($options, function ($option) {
108
            return ! Str::startsWith($option, '--env=');
109
        }));
110
111
        if (! file_exists($file = base_path('phpunit.xml'))) {
112
            $file = base_path('phpunit.xml.dist');
113
        }
114
115
        return array_merge(['-c', $file], $options);
116
    }
117
118
    /**
119
     * Gets an array with phpunit envs detected on the phpunit.xml file.
120
     *
121
     * @return array
122
     */
123
    protected function phpunitEnvs()
124
    {
125
        $envs = [];
126
127
        if (! file_exists($file = base_path('phpunit.xml'))) {
128
            $file = base_path('phpunit.xml.dist');
129
        }
130
131
        if (file_exists($file)) {
132
            /** @var \SimpleXMLElement $xml */
133
            $xml = simplexml_load_string((string) file_get_contents($file));
134
135 View Code Duplication
            if (is_iterable($xml->php->server)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
                foreach ($xml->php->server as $env) {
137
                    if (isset($env['name'])) {
138
                        $envs[$env['name']->__toString()] = false;
139
                    }
140
                }
141
            }
142
143 View Code Duplication
            if (is_iterable($xml->php->env)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
                foreach ($xml->php->env as $env) {
145
                    if (isset($env['name'])) {
146
                        $envs[$env['name']->__toString()] = false;
147
                    }
148
                }
149
            }
150
        }
151
152
        return $envs;
153
    }
154
}
155