Passed
Pull Request — stable (#96)
by
unknown
02:05
created

TestCommand::clearEnv()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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