TestCommand   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 131
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A clearEnv() 0 17 3
B handle() 0 30 6
A binary() 0 8 2
A phpunitArguments() 0 12 2
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
        try {
77
            return $process->run(function ($type, $line) {
78
                $this->output->write($line);
79
            });
80
        } catch (ProcessSignaledException $e) {
81
            if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
82
                throw $e;
83
            }
84
        }
85
    }
86
87
    /**
88
     * Get the PHP binary to execute.
89
     *
90
     * @return array
91
     */
92
    protected function binary()
93
    {
94
        if ('phpdbg' === PHP_SAPI) {
95
            return [PHP_BINARY, '-qrr', 'vendor/phpunit/phpunit/phpunit'];
96
        }
97
98
        return [PHP_BINARY, 'vendor/phpunit/phpunit/phpunit'];
99
    }
100
101
    /**
102
     * Get the array of arguments for running PHPUnit.
103
     *
104
     * @param array $options
105
     *
106
     * @return array
107
     */
108
    protected function phpunitArguments($options)
109
    {
110
        $options = array_values(array_filter($options, function ($option) {
111
            return !Str::startsWith($option, '--env=');
112
        }));
113
114
        if (!file_exists($file = base_path('phpunit.xml'))) {
115
            $file = base_path('phpunit.xml.dist');
116
        }
117
118
        return array_merge(['-c', $file], $options);
119
    }
120
121
    /**
122
     * Clears any set Environment variables set by Laravel if the --env option is empty.
123
     *
124
     * @return void
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