ExpectationEvaluator::evaluate()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
rs 9.9
cc 4
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester\Expectation;
6
7
use hanneskod\readmetester\Runner\OutcomeInterface;
8
9
class ExpectationEvaluator
10
{
11
    /**
12
     * Evaluate expectations against an outcome
13
     *
14
     * If outcome is marked as mustBeHandled it must be handled by at least one
15
     * expectation. Expectations that does not handle an outcome triggers failure.
16
     *
17
     * @return array<StatusInterface>
18
     */
19
    public function evaluate(OutcomeInterface $outcome): array
20
    {
21
        $statuses = [];
22
        $isHandled = !$outcome->mustBeHandled();
23
24
        foreach ($outcome->getExample()->getExpectations() as $index => $expectation) {
25
            if ($expectation->handles($outcome)) {
26
                $statuses[$index] = $expectation->handle($outcome);
27
                $isHandled = true;
28
            } else {
29
                $statuses[$index] = new Failure($outcome, "Failed {$expectation->getDescription()}");
30
            }
31
        }
32
33
        if (!$isHandled) {
34
            $statuses[] = new Failure($outcome, 'Unhandled ' . $outcome->getDescription());
35
        }
36
37
        return $statuses;
38
    }
39
}
40