Test Failed
Push — master ( 57b7d4...e042c7 )
by Hannes
02:15
created

ExpectationEvaluator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A evaluate() 0 19 4
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 an outcome
11
 */
12
class ExpectationEvaluator
13
{
14
    /**
15
     * Evaluate expectations against an outcome
16
     *
17
     * If outcome is marked as mustBeHandled it must be handled by at least one
18
     * expectation. Expectations that does not handle an outcome triggers failure.
19
     *
20
     * @param  ExpectationInterface[] $expectations
21
     * @param  OutcomeInterface       $outcome
22
     * @return Status[]
23
     */
24
    public function evaluate(array $expectations, OutcomeInterface $outcome): array
25
    {
26
        $statuses = [];
27
        $isHandled = !$outcome->mustBeHandled();
28
29
        foreach ($expectations as $index => $expectation) {
30
            if ($expectation->handles($outcome)) {
31
                $statuses[$index] = $expectation->handle($outcome);
32
                $isHandled = true;
33
            } else {
34
                $statuses[$index] = new Failure("Failed $expectation");
35
            }
36
        }
37
38
        if (!$isHandled) {
39
            $statuses[] = new Failure("Unhandled $outcome");
40
        }
41
42
        return $statuses;
43
    }
44
}
45