SpecReporter::onSuiteStart()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Peridot\Reporter;
3
4
use Peridot\Core\Suite;
5
use Peridot\Core\Test;
6
use Peridot\Core\TestResult;
7
use Peridot\Runner\Context;
8
9
/**
10
 * The SpecReporter is the default Peridot reporter. It organizes Suite and Test results
11
 * in a hierarchical manner.
12
 *
13
 * @package Peridot\Reporter
14
 */
15
class SpecReporter extends AbstractBaseReporter
16
{
17
    /**
18
     * @var int
19
     */
20
    protected $column = 0;
21
22
    /**
23
     * @var \Peridot\Core\Suite
24
     */
25
    protected $root;
26
27
    /**
28
     * Initialize reporter. Setup and listen for runner events
29
     *
30
     * @return void
31
     */
32
    public function init()
33
    {
34
        $this->root = Context::getInstance()->getCurrentSuite();
35
36
        $this->eventEmitter->on('runner.start', [$this, 'onRunnerStart']);
37
        $this->eventEmitter->on('suite.start', [$this, 'onSuiteStart']);
38
        $this->eventEmitter->on('suite.end', [$this, 'onSuiteEnd']);
39
        $this->eventEmitter->on('test.passed', [$this, 'onTestPassed']);
40
        $this->eventEmitter->on('test.failed', [$this, 'onTestFailed']);
41
        $this->eventEmitter->on('test.pending', [$this, 'onTestPending']);
42
        $this->eventEmitter->on('runner.end', [$this, 'onRunnerEnd']);
43
    }
44
45
    public function onRunnerStart()
46
    {
47
        $this->output->writeln("");
48
    }
49
50
    /**
51
     * @param Suite $suite
52
     */
53
    public function onSuiteStart(Suite $suite)
54
    {
55
        if ($suite != $this->root) {
56
            ++$this->column;
57
            $this->output->writeln(sprintf('%s%s', $this->indent(), $suite->getDescription()));
58
        }
59
    }
60
61
    public function onSuiteEnd()
62
    {
63
        --$this->column;
64
        if ($this->column == 0) {
65
            $this->output->writeln("");
66
        }
67
    }
68
69
    /**
70
     * @param Test $test
71
     */
72
    public function onTestPassed(Test $test)
73
    {
74
        $this->output->writeln(sprintf(
75
            "  %s%s %s",
76
            $this->indent(),
77
            $this->color('success', $this->symbol('check')),
78
            $this->color('muted', $test->getDescription())
79
        ));
80
    }
81
82
    /**
83
     * @param Test $test
84
     */
85
    public function onTestFailed(Test $test)
86
    {
87
        $this->output->writeln(sprintf(
88
            "  %s%s",
89
            $this->indent(),
90
            $this->color('error', sprintf("%d) %s", count($this->errors), $test->getDescription()))
91
        ));
92
    }
93
94
    /**
95
     * @param Test $test
96
     */
97
    public function onTestPending(Test $test)
98
    {
99
        $this->output->writeln(sprintf(
100
            $this->color('pending', "  %s- %s"),
101
            $this->indent(),
102
            $test->getDescription()
103
        ));
104
    }
105
106
    /**
107
     * @param float $time
108
     * @param TestResult $result
109
     */
110
    public function onRunnerEnd($time, TestResult $result)
111
    {
112
        $this->footer();
113
        $this->warnings($result);
114
    }
115
116
    /**
117
     * Returns the current indent for the spec reporter
118
     *
119
     * @return string
120
     */
121
    public function indent()
122
    {
123
        return implode('  ', array_fill(0, $this->column + 1, ''));
124
    }
125
}
126