CodeceptionCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 1
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\CodeceptionJSONReporter;
12
13
class CodeceptionCommand implements CommandInterface
14
{
15
    public const COMMAND_NAME = 'test/codeception';
16
17
    public function __construct(private Aliases $aliases)
18
    {
19
    }
20
21
    public static function getTitle(): string
22
    {
23
        return 'Codeception';
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 = CodeceptionJSONReporter::class;
37
        $params = [
38
            'vendor/bin/codecept',
39
            'run',
40
            '--silent',
41
            '-e',
42
            $extension,
43
            '--override',
44
            "extensions: config: {$extension}: output-path: {$debugDirectory}",
45
            '-vvv',
46
        ];
47
48
        $process = new Process($params);
49
50
        $process
51
            ->setWorkingDirectory($projectDirectory)
52
            ->setTimeout(null)
53
            ->run();
54
55
        $processOutput = json_decode(
56
            file_get_contents($debugDirectory . DIRECTORY_SEPARATOR . CodeceptionJSONReporter::FILENAME),
57
            true,
58
            512,
59
            JSON_THROW_ON_ERROR
60
        );
61
62
        if (!$process->getExitCode() > 1) {
63
            return new CommandResponse(
64
                status: CommandResponse::STATUS_FAIL,
65
                result: null,
66
                errors: array_filter([$processOutput, $process->getErrorOutput()]),
67
            );
68
        }
69
70
        return new CommandResponse(
71
            status: $process->isSuccessful() ? CommandResponse::STATUS_OK : CommandResponse::STATUS_ERROR,
72
            result: $processOutput
73
        );
74
    }
75
}
76