Test Failed
Push — master ( 397588...614ce5 )
by Hannes
02:13
created

ReturnExpectation::__construct()   A

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 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\Expectation;
6
7
use hanneskod\readmetester\Runner\OutcomeInterface;
8
9
/**
10
 * Validate that correct data is returned
11
 */
12
class ReturnExpectation implements ExpectationInterface
13
{
14
    /**
15
     * @var Regexp Expression matching return value
16
     */
17
    private $regexp;
18
19
    /**
20
     * Set regular expression matching return value
21
     */
22
    public function __construct(Regexp $regexp)
23
    {
24
        $this->regexp = $regexp;
25
    }
26
27
    public function __tostring(): string
28
    {
29
        return "expecting return value to match regexp {$this->regexp}";
30
    }
31
32
    public function handles(OutcomeInterface $outcome): bool
33
    {
34
        return $outcome->getType() == OutcomeInterface::TYPE_RETURN;
35
    }
36
37
    public function handle(OutcomeInterface $outcome): Status
38
    {
39
        $returnValue = $outcome->getPayload()['value'] ?? '';
40
41
        if (!$this->regexp->isMatch($returnValue)) {
42
            return new Failure(
43
                "Failed asserting that return value '$returnValue' matches {$this->regexp}"
44
            );
45
        }
46
47
        return new Success("Asserted that return value '$returnValue' matches {$this->regexp}");
48
    }
49
}
50