Test Failed
Push — master ( 614ce5...540a9b )
by Hannes
02:11
created

JsonFormatter::onBootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester\Console;
6
7
use hanneskod\readmetester\Example\Example;
8
use hanneskod\readmetester\Expectation\Status;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Default text output formatter
13
 */
14
class JsonFormatter implements FormatterInterface
15
{
16
    /**
17
     * @var OutputInterface
18
     */
19
    private $output;
20
21
    /**
22
     * @var array
23
     */
24
    private $data;
25
26
    /**
27
     * @var array
28
     */
29
    private $keys;
30
31
    public function __construct(OutputInterface $output)
32
    {
33
        $this->output = $output;
34
    }
35
36
    public function onInvokationStart(): void
37
    {
38
        $this->data = [
39
            'bootstrap' => '',
40
            'tests' => [],
41
            'counts' => [
42
                'files' => 0,
43
                'examples' => 0,
44
                'ignored' => 0,
45
                'assertions' => 0,
46
                'failures' => 0,
47
            ]
48
        ];
49
    }
50
51
    public function onBootstrap(string $filename): void
52
    {
53
        $this->data['bootstrap'] = $filename;
54
    }
55
56
    public function onFile(string $filename): void
57
    {
58
        $this->data['tests'][$filename] = [];
59
        $this->data['counts']['files']++;
60
        $this->keys['file'] = $filename;
61
    }
62
63
    public function onExample(Example $example): void
64
    {
65
        $this->data['tests'][$this->keys['file']][$example->getName()] = [];
66
        $this->data['counts']['examples']++;
67
        $this->keys['example'] = $example->getName();
68
    }
69
70
    public function onIgnoredExample(Example $example): void
71
    {
72
        $this->data['tests'][$this->keys['file']][$example->getName()] = 'IGNORED';
73
        $this->data['counts']['ignored']++;
74
    }
75
76
    public function onExpectation(Status $status): void
77
    {
78
        $this->data['tests'][$this->keys['file']][$this->keys['example']][] = [
79
            $status->isSuccess() ? 'success' : 'failure' => (string)$status
80
        ];
81
82
        $this->data['counts']['assertions']++;
83
84
        if (!$status->isSuccess()) {
85
            $this->data['counts']['failures']++;
86
        }
87
    }
88
89
    public function onInvokationEnd(): void
90
    {
91
        $this->output->writeln(json_encode($this->data, JSON_PRETTY_PRINT));
92
    }
93
}
94