Passed
Pull Request — stable (#96)
by
unknown
02:52 queued 46s
created

TestCommand::binary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace NunoMaduro\Collision\Adapters\Laravel\Commands;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Repository\RepositoryBuilder;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Str;
9
use RuntimeException;
10
use Symfony\Component\Process\Exception\ProcessSignaledException;
11
use Symfony\Component\Process\Process;
12
13
class TestCommand extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'test {--without-tty : Disable output to TTY}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Run the application tests';
28
29
    /**
30
     * The arguments to be used while calling phpunit.
31
     *
32
     * @var array
33
     */
34
    protected $arguments = [
35
        '--printer',
36
        'NunoMaduro\Collision\Adapters\Phpunit\Printer',
37
    ];
38
39
    /**
40
     * Create a new command instance.
41
     *
42
     * @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...
43
     */
44
    public function __construct()
45
    {
46
        parent::__construct();
47
48
        $this->ignoreValidationErrors();
49
    }
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return mixed
55
     */
56
    public function handle()
57
    {
58
        $options = array_slice($_SERVER['argv'], $this->option('without-tty') ? 3 : 2);
59
60
        $this->clearEnv();
61
62
        $process = (new Process(array_merge(
63
            $this->binary(),
64
            array_merge(
65
                $this->arguments,
66
                $this->phpunitArguments($options)
67
            )
68
        )))->setTimeout(null);
69
70
        try {
71
            $process->setTty(! $this->option('without-tty'));
72
        } catch (RuntimeException $e) {
73
            $this->output->writeln('Warning: '.$e->getMessage());
74
        }
75
76
77
        try {
78
            return $process->run(function ($type, $line) {
79
                $this->output->write($line);
80
            });
81
        } catch (ProcessSignaledException $e) {
82
            if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
83
                throw $e;
84
            }
85
        }
86
    }
87
88
    /**
89
     * Get the PHP binary to execute.
90
     *
91
     * @return array
92
     */
93
    protected function binary()
94
    {
95
        if ('phpdbg' === PHP_SAPI) {
96
            return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit'];
97
        }
98
99
        return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit'];
100
    }
101
102
    /**
103
     * Get the array of arguments for running PHPUnit.
104
     *
105
     * @param  array  $options
106
     *
107
     * @return array
108
     */
109
    protected function phpunitArguments($options)
110
    {
111
        $options = array_values(array_filter($options, function ($option) {
112
            return ! Str::startsWith($option, '--env=');
113
        }));
114
115
        if (! file_exists($file = base_path('phpunit.xml'))) {
116
            $file = base_path('phpunit.xml.dist');
117
        }
118
119
        return array_merge(['-c', $file], $options);
120
    }
121
122
    /**
123
     * Clears any set Environment variables set by Laravel if the --env option is empty.
124
     *
125
     */
126
    protected function clearEnv()
127
    {
128
        if (! $this->option('env')) {
129
            $repositories = RepositoryBuilder::create()
130
                ->make();
131
132
            $envs = Dotenv::create(
133
                $repositories,
134
                $this->laravel->environmentPath(),
0 ignored issues
show
Bug introduced by
The method environmentPath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
135
                $this->laravel->environmentFile()
0 ignored issues
show
Bug introduced by
The method environmentFile() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean environment()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
            )->safeLoad();
137
138
            foreach ($envs as $name => $value) {
139
                $repositories->clear($name);
140
            }
141
        }
142
    }
143
}
144