Passed
Pull Request — master (#33)
by Melech
05:54 queued 02:40
created

Exceptions::verify()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 15
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 28
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Test\Assert\Asserters;
15
16
use Throwable;
17
use Valkyrja\Test\Assert\Exceptions as Contract;
18
use Valkyrja\Test\Exceptions\AssertFailureException;
19
20
/**
21
 * Class Exceptions.
22
 *
23
 * @author Melech Mizrachi
24
 */
25
class Exceptions extends Asserter implements Contract
26
{
27
    /**
28
     * Whether an exception is expected to be thrown.
29
     */
30
    protected bool $expecting = false;
31
32
    /**
33
     * The expected class name.
34
     *
35
     * @var class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
36
     */
37
    protected string $className;
38
39
    /**
40
     * The expected message.
41
     */
42
    protected string $message;
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public static function getExpectedErrorMessage(): string
48
    {
49
        return 'An exception was expected. Got none.';
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public static function getUnexpectedErrorMessage(string $actualClassName, string $actualMessage): string
56
    {
57
        return "An unexpected exception {$actualClassName} with message {$actualMessage} was thrown.";
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public static function getIncorrectClassNameErrorMessage(string $expected, string $actual): string
64
    {
65
        return "Expected {$expected} exception does not match actual {$actual}.";
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public static function getIncorrectMessageErrorMessage(string $expected, string $actual): string
72
    {
73
        return "Expected {$expected} message does not match actual {$actual}.";
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function className(string $className): void
80
    {
81
        $this->assertions[] = $className;
82
83
        $this->className = $className;
84
85
        $this->expecting();
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function message(string $message): void
92
    {
93
        $this->assertions[] = $message;
94
95
        $this->message = $message;
96
97
        $this->expecting();
98
    }
99
100
    /**
101
     * @inheritDoc
102
     */
103
    public function expecting(): void
104
    {
105
        $this->expecting = true;
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function verify(Throwable $exception = null): void
112
    {
113
        if ($exception === null && $this->expecting) {
114
            $this->errors[] = new AssertFailureException(self::getExpectedErrorMessage());
115
116
            return;
117
        }
118
119
        $actualClassName = $exception::class;
120
        $actualMessage   = $exception->getMessage();
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
        /** @scrutinizer ignore-call */ 
121
        $actualMessage   = $exception->getMessage();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
122
        if (! $this->expecting) {
123
            $this->errors[] = new AssertFailureException(
124
                self::getUnexpectedErrorMessage($actualClassName, $actualMessage)
125
            );
126
127
            return;
128
        }
129
130
        if (isset($this->className) && ($className = $this->className) !== $actualClassName) {
131
            $this->errors[] = new AssertFailureException(
132
                self::getIncorrectClassNameErrorMessage($className, $actualClassName)
133
            );
134
        }
135
136
        if (isset($this->message) && ($message = $this->message) !== $actualMessage) {
137
            $this->errors[] = new AssertFailureException(
138
                self::getIncorrectMessageErrorMessage($message, $actualMessage)
139
            );
140
        }
141
    }
142
}
143