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

ReadmeTester.php$0 ➔ onExpectation()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 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;
6
7
use hanneskod\readmetester\Example\ExampleFactory;
8
use hanneskod\readmetester\Example\Example;
9
use hanneskod\readmetester\Expectation\Status;
10
use hanneskod\readmetester\Parser\Parser;
11
use hanneskod\readmetester\Runner\EvalRunner;
12
use hanneskod\readmetester\Expectation\ExpectationEvaluator;
13
14
/**
15
 * Test examples in readme file
16
 */
17
class ReadmeTester
18
{
19
    /**
20
     * @var Parser Helper to extract example definitions
21
     */
22
    private $parser;
23
24
    /**
25
     * @var ExampleFactory Helper to create example objects
26
     */
27
    private $exampleFactory;
28
29
    public function __construct(Parser $parser = null, ExampleFactory $exampleFactory = null)
30
    {
31
        $this->parser = $parser ?: new Parser;
32
        $this->exampleFactory = $exampleFactory ?: new ExampleFactory(new Expectation\ExpectationFactory);
33
    }
34
35
    /**
36
     * Test examples in file
37
     *
38
     * @param string $contents File contents to test examples in
39
     */
40
    public function test(string $contents): iterable
41
    {
42
        $tester = new ExampleTester(
43
            new EvalRunner,
44
            new ExpectationEvaluator
45
        );
46
47
        $listener = new class implements ListenerInterface {
48
            private $example;
49
50
            public $statuses = [];
51
52
            function onExample(Example $example): void
53
            {
54
                $this->example = $example;
55
            }
56
57
            function onIgnoredExample(Example $example): void
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
            {
59
            }
60
61
            function onExpectation(Status $status): void
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
            {
63
                $this->statuses[] = [$this->example->getName(), $status];
64
            }
65
        };
66
67
        $tester->registerListener($listener);
68
69
        foreach ($this->exampleFactory->createExamples(...$this->parser->parse($contents)) as $example) {
70
            $tester->testExample($example);
71
        }
72
73
        foreach ($listener->statuses as $vals) {
74
            yield $vals[0] => $vals[1];
75
        };
76
    }
77
}
78