ErrorExpectation::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
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\Expectation;
6
7
use hanneskod\readmetester\Runner\OutcomeInterface;
8
use hanneskod\readmetester\Utils\Regexp;
9
10
/**
11
 * Validate that correct error is produced
12
 */
13
final class ErrorExpectation implements ExpectationInterface
14
{
15
    private Regexp $regexp;
16
17
    public function __construct(Regexp $regexp)
18
    {
19
        $this->regexp = $regexp;
20
    }
21
22
    public function getDescription(): string
23
    {
24
        return "expecting error to match regexp {$this->regexp->getRegexp()}";
25
    }
26
27
    public function handles(OutcomeInterface $outcome): bool
28
    {
29
        return $outcome->isError();
30
    }
31
32
    public function handle(OutcomeInterface $outcome): StatusInterface
33
    {
34
        $msg = "that error '{$outcome->getContent()}' matches '{$this->regexp->getRegexp()}'";
35
36
        if (!$this->regexp->matches($outcome->getContent())) {
37
            return new Failure($outcome, "Failed asserting $msg");
38
        }
39
40
        return new Success($outcome, "Asserted $msg");
41
    }
42
}
43