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

ExampleTester::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester;
6
7
use hanneskod\readmetester\Example\Example;
8
use hanneskod\readmetester\Expectation\ExpectationEvaluator;
9
use hanneskod\readmetester\Expectation\Status;
10
use hanneskod\readmetester\Runner\RunnerInterface;
11
12
/**
13
 * Run example code and evaluate expectations
14
 */
15
class ExampleTester
16
{
17
    /**
18
     * @var RunnerInterface
19
     */
20
    private $runner;
21
22
    /**
23
     * @var ExpectationEvaluator
24
     */
25
    private $evaluator;
26
27
    /**
28
     * @var ListenerInterface[]
29
     */
30
    private $listeners = [];
31
32
    public function __construct(RunnerInterface $runner, ExpectationEvaluator $evaluator)
33
    {
34
        $this->runner = $runner;
35
        $this->evaluator = $evaluator;
36
    }
37
38
    public function registerListener(ListenerInterface $listener): void
39
    {
40
        $this->listeners[] = $listener;
41
    }
42
43
    public function testExample(Example $example): void
44
    {
45
        if (!$example->shouldBeEvaluated()) {
46
            foreach ($this->listeners as $listener) {
47
                $listener->onIgnoredExample($example);
48
            }
49
            return;
50
        }
51
52
        foreach ($this->listeners as $listener) {
53
            $listener->onExample($example);
54
        }
55
56
        $outcomes = $this->runner->run($example->getCodeBlock());
57
58
        foreach ($this->evaluator->evaluate($example->getExpectations(), $outcomes) as $status) {
59
            foreach ($this->listeners as $listener) {
60
                $listener->onExpectation($status);
61
            }
62
        }
63
    }
64
}
65