Completed
Push — master ( bbfb28...e849e1 )
by Hannes
01:48
created

Presenter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester\Console;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
use hanneskod\readmetester\Expectation\ReturnObj\ReturnObj;
9
10
/**
11
 * Presenter for normal (non-verbose) output
12
 */
13
class Presenter
14
{
15
    /**
16
     * @var OutputInterface
17
     */
18
    protected $output;
19
20
    /**
21
     * @var integer The number of processed files
22
     */
23
    private $fileCount = 0;
24
25
    /**
26
     * @var integer The number of assertions
27
     */
28
    private $assertionCount = 0;
29
30
    /**
31
     * @var integer The number of failed assertions
32
     */
33
    private $failureCount = 0;
34
35
    public function __construct(OutputInterface $output)
36
    {
37
        $this->output = $output;
38
    }
39
40
    /**
41
     * Called when a new test run starts
42
     */
43
    public function begin()
44
    {
45
        $this->output->writeln("Readme-Tester by Hannes Forsgård.");
46
    }
47
48
    /**
49
     * Called when a bootstrap has ben pulled
50
     */
51
    public function bootstrap(string $bootstrap)
52
    {
53
        $this->output->writeln("Loading bootstrap <comment>$bootstrap</comment>");
54
    }
55
56
    /**
57
     * Called when a test run ends
58
     */
59
    public function end()
60
    {
61
        $this->output->writeln(
62
            sprintf(
63
                "<%s>%s file%s tested, %s assertion%s, %s failure%s.</%s>",
64
                $this->hasFailures() ? 'error' : 'info',
65
                $this->fileCount,
66
                $this->fileCount == 1 ? '' : 's',
67
                $this->assertionCount,
68
                $this->assertionCount == 1 ? '' : 's',
69
                $this->failureCount,
70
                $this->failureCount == 1 ? '' : 's',
71
                $this->hasFailures() ? 'error' : 'info'
72
            )
73
        );
74
    }
75
76
    /**
77
     * Called when a new file is tested
78
     */
79
    public function beginFile(string $fileName)
80
    {
81
        $this->fileCount++;
82
        $this->output->writeln("Testing examples in <comment>$fileName</comment>");
83
    }
84
85
    /**
86
     * Called when an assertion is tested
87
     */
88
    public function beginAssertion(string $exampleName, ReturnObj $returnObj)
89
    {
90
        $this->assertionCount++;
91
92
        if ($returnObj->isSuccess()) {
93
            $this->success($exampleName, $returnObj->getMessage());
94
            return;
95
        }
96
97
        $this->failureCount++;
98
        $this->failure($exampleName, $returnObj->getMessage());
99
    }
100
101
    /**
102
     * Check if any failures have been recorded
103
     */
104
    public function hasFailures(): bool
105
    {
106
        return !!$this->failureCount;
107
    }
108
109
    /**
110
     * Called when an assertion is successfull
111
     */
112
    protected function success(string $exampleName, string $message)
113
    {
114
    }
115
116
    /**
117
     * Called when an assertion failes
118
     */
119
    protected function failure(string $exampleName, string $message)
120
    {
121
        $this->output->writeln("\n<error>Example $exampleName: $message</error>\n");
122
    }
123
}
124