Passed
Push — master ( 517a56...adf5dc )
by Alexander
02:50
created

CodeceptionJSONReporter::getTestName()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
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\Test;
6
7
use Codeception\Event\FailEvent;
8
use Codeception\Event\PrintResultEvent;
9
use Codeception\Event\TestEvent;
10
use Codeception\Events;
11
use Codeception\Extension;
12
use Codeception\Test\Descriptor;
13
14
final class CodeceptionJSONReporter extends Extension
15
{
16
    protected array $config = [
17
        'output-path' => __DIR__,
18
    ];
19
    private array $data = [];
20
    public const FILENAME = 'codeception-report.json';
21
22
    public function _initialize(): void
23
    {
24
        $this->_reconfigure(['settings' => ['silent' => true]]);
25
    }
26
27
    public static array $events = [
28
        Events::TEST_SUCCESS => 'success',
29
        Events::TEST_FAIL => 'fail',
30
        Events::TEST_ERROR => 'error',
31
        Events::RESULT_PRINT_AFTER => 'all',
32
    ];
33
34
    public function success(TestEvent $event): void
35
    {
36
        $this->data[] = [
37
            'file' => $this->getTestFilename($event),
38
            'test' => $this->getTestName($event),
39
            'status' => 'ok',
40
            'stacktrace' => [],
41
        ];
42
    }
43
44
    public function fail(FailEvent $event): void
45
    {
46
        $this->data[] = [
47
            'file' => $this->getTestFilename($event),
48
            'test' => $this->getTestName($event),
49
            'status' => 'fail',
50
            'stacktrace' => $event->getFail()->getTrace(),
51
        ];
52
    }
53
54
    public function error(FailEvent $event): void
55
    {
56
        $this->data[] = [
57
            'file' => $this->getTestFilename($event),
58
            'test' => $this->getTestName($event),
59
            'status' => 'error',
60
            'stacktrace' => $event->getFail()->getTrace(),
61
        ];
62
    }
63
64
    public function all(PrintResultEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
    public function all(/** @scrutinizer ignore-unused */ PrintResultEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        file_put_contents($this->config['output-path'] . DIRECTORY_SEPARATOR . self::FILENAME, json_encode($this->data));
67
    }
68
69
    private function getTestName(TestEvent $event): string
70
    {
71
        return Descriptor::getTestFullName($event->getTest());
72
    }
73
74
    private function getTestFilename(TestEvent $event): string
75
    {
76
        return Descriptor::getTestFileName($event->getTest());
77
    }
78
}
79