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

ExpectationEvaluator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B evaluate() 0 24 6
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester\Expectation;
6
7
use hanneskod\readmetester\Runner\OutcomeInterface;
8
9
/**
10
 * Evaluate expectations against a set of outcomes
11
 */
12
class ExpectationEvaluator
13
{
14
    /**
15
     * Evaluate expectations against a set of outcomes
16
     *
17
     * All outcomes must be handled by at least one expectation. Expectations
18
     * that does not handle an outcome defaults to failure.
19
     *
20
     * @param  ExpectationInterface[] $expectations
21
     * @param  OutcomeInterface[]     $outcomes
22
     * @return Status[]
23
     */
24
    public function evaluate(array $expectations, array $outcomes): array
25
    {
26
        $statuses = [];
27
28
        foreach ($expectations as $index => $expectation) {
29
            $statuses[$index] = new Failure("Failed $expectation");
30
        }
31
32
        foreach ($outcomes as $outcome) {
33
            $isHandled = false;
34
35
            foreach ($expectations as $index => $expectation) {
36
                if ($expectation->handles($outcome)) {
37
                    $statuses[$index] = $expectation->handle($outcome);
38
                    $isHandled = true;
39
                }
40
            }
41
42
            if (!$isHandled) {
43
                $statuses[] = new Failure("Unhandled $outcome");
44
            }
45
        }
46
47
        return $statuses;
48
    }
49
}
50