Passed
Pull Request — master (#65)
by Dmitriy
03:03
created

PHPUnitCommand::getDescription()   A

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
        if (!$process->isSuccessful()) {
53
            return new CommandResponse(
54
                status: CommandResponse::STATUS_ERROR,
55
                result: null,
56
                errors: [$process->getErrorOutput()],
57
            );
58
        }
59
60
        return new CommandResponse(
61
            status: CommandResponse::STATUS_OK,
62
            result: json_decode(
63
                file_get_contents($debugDirectory . DIRECTORY_SEPARATOR . PHPUnitJSONReporter::FILENAME),
64
                true,
65
                512,
66
                JSON_THROW_ON_ERROR
67
            )
68
        );
69
    }
70
}
71