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

ExceptionExpectation::handles()   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 the correct exception is thrown
11
 */
12
class ExceptionExpectation implements ExpectationInterface
13
{
14
    /**
15
     * @var string Name of expected exception class
16
     */
17
    private $exceptionClass;
18
19
    public function __construct(string $exceptionClass)
20
    {
21
        $this->exceptionClass = $exceptionClass;
22
    }
23
24
    public function __tostring(): string
25
    {
26
        return "expecting an exception of class {$this->exceptionClass} to be thrown";
27
    }
28
29
    public function handles(OutcomeInterface $outcome): bool
30
    {
31
        return $outcome->getType() == OutcomeInterface::TYPE_EXCEPTION;
32
    }
33
34
    public function handle(OutcomeInterface $outcome): Status
35
    {
36
        $thrownClass = $outcome->getPayload()['class'] ?? '';
37
38
        if ($thrownClass != $this->exceptionClass) {
39
            return new Failure(
40
                "Failed asserting that exception {$this->exceptionClass} was thrown, found: $thrownClass"
41
            );
42
        }
43
44
        return new Success("Asserted that exception {$this->exceptionClass} was thrown");
45
    }
46
}
47