PHPUnitCommand::getTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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