Test Failed
Push — master ( 22d245...e46311 )
by Hannes
02:55
created

OutputExpectation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A handles() 0 3 1
A handle() 0 9 2
A __construct() 0 3 1
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 output is produced
12
 */
13
final class OutputExpectation implements ExpectationInterface
14
{
15
    /** @var Regexp */
16
    private $regexp;
17
18
    public function __construct(Regexp $regexp)
19
    {
20
        $this->regexp = $regexp;
21
    }
22
23
    public function getDescription(): string
24
    {
25
        return "expecting output to match regexp {$this->regexp->getRegexp()}";
26
    }
27
28
    public function handles(OutcomeInterface $outcome): bool
29
    {
30
        return $outcome->getType() == OutcomeInterface::TYPE_OUTPUT;
31
    }
32
33
    public function handle(OutcomeInterface $outcome): StatusInterface
34
    {
35
        if (!$this->regexp->isMatch($outcome->getContent())) {
36
            return new Failure(
37
                "Failed asserting that output '{$outcome->getContent()}' matches {$this->regexp->getRegexp()}"
38
            );
39
        }
40
41
        return new Success("Asserted that output '{$outcome->getContent()}' matches {$this->regexp->getRegexp()}");
42
    }
43
}
44