PHPUnitCommand::run()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 39
ccs 0
cts 31
cp 0
rs 9.488
cc 3
nc 2
nop 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Inspector\Command;
6
7
use Symfony\Component\Process\Process;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Yii\Debug\Api\Inspector\CommandInterface;
10
use Yiisoft\Yii\Debug\Api\Inspector\CommandResponse;
11
use Yiisoft\Yii\Debug\Api\Inspector\Test\PHPUnitJSONReporter;
12
13
class PHPUnitCommand implements CommandInterface
14
{
15
    public const COMMAND_NAME = 'test/phpunit';
16
17
    public function __construct(private Aliases $aliases)
18
    {
19
    }
20
21
    public static function getTitle(): string
22
    {
23
        return 'PHPUnit';
24
    }
25
26
    public static function getDescription(): string
27
    {
28
        return '';
29
    }
30
31
    public function run(): CommandResponse
32
    {
33
        $projectDirectory = $this->aliases->get('@root');
34
        $debugDirectory = $this->aliases->get('@runtime/debug');
35
36
        $extension = PHPUnitJSONReporter::class;
37
        $params = [
38
            'vendor/bin/phpunit',
39
            '--printer',
40
            $extension,
41
            '-vvv',
42
        ];
43
44
        $process = new Process($params);
45
46
        $process
47
            ->setEnv([PHPUnitJSONReporter::ENVIRONMENT_VARIABLE_DIRECTORY_NAME => $debugDirectory])
48
            ->setWorkingDirectory($projectDirectory)
49
            ->setTimeout(null)
50
            ->run();
51
52
        $processOutput = json_decode(
53
            file_get_contents($debugDirectory . DIRECTORY_SEPARATOR . PHPUnitJSONReporter::FILENAME),
54
            true,
55
            512,
56
            JSON_THROW_ON_ERROR
57
        );
58
59
        if (!$process->getExitCode() > 1) {
60
            return new CommandResponse(
61
                status: CommandResponse::STATUS_FAIL,
62
                result: null,
63
                errors: array_filter([$processOutput, $process->getErrorOutput()]),
64
            );
65
        }
66
67
        return new CommandResponse(
68
            status: $process->isSuccessful() ? CommandResponse::STATUS_OK : CommandResponse::STATUS_ERROR,
69
            result: $processOutput
70
        );
71
    }
72
}
73