Completed
Push — master ( 9a2e94...dbda34 )
by Vincent
05:01 queued 12s
created

SetExceptionsTrait::setFailureException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert;
6
7
use PHPUnit\Framework\ExpectationFailedException;
8
9
/**
10
 * Some helpers for testing
11
 *
12
 * @codeCoverageIgnore
13
 */
14
trait SetExceptionsTrait
15
{
16
    /**
17
     * Set the class name of the expected exception
18
     *
19
     * @see \PHPUnit\Framework\TestCase::expectException
20
     *
21
     * @param string $exception
22
     *
23
     * @return void
24
     */
25
    abstract public function expectException(string $exception): void;
26
27
    /**
28
     * Set the message of the expected exception
29
     *
30
     * @see \PHPUnit\Framework\TestCase::expectExceptionMessage
31
     *
32
     * @param string $message
33
     *
34
     * @return void
35
     */
36
    abstract public function expectExceptionMessage(string $message): void;
37
38
    /**
39
     * Set the a regular expression for the message of the expected exception
40
     *
41
     * @see \PHPUnit\Framework\TestCase::expectExceptionMessageRegExp
42
     *
43
     * @param string $messageRegExp
44
     *
45
     * @return void
46
     */
47
    abstract public function expectExceptionMessageRegExp(string $messageRegExp): void;
48
49
50
    /**
51
     * Set the expected exception and message when defining a test that will fail.
52
     *
53
     * @param string|null $message The failure message could be either a string or a regular expression.
54
     *
55
     * @return void
56
     */
57
    protected function setFailure(?string $message = null)
58
    {
59
        if (($message !== null) && (strpos($message, '/') === 0)) {
60
            $this->setFailureExceptionRegex($message);
61
        } else {
62
            $this->setFailureException($message);
63
        }
64
    }
65
66
    /**
67
     * Set the expected exception and message when defining a test that will fail.
68
     *
69
     * @param string|null $message
70
     *
71
     * @return void
72
     */
73
    protected function setFailureException(?string $message = null)
74
    {
75
        $this->expectException(ExpectationFailedException::class);
76
        if ($message !== null) {
77
            $this->expectExceptionMessage($message);
78
        }
79
    }
80
81
    /**
82
     * Set the expected exception and message when defining a test that will fail.
83
     *
84
     * @param string|null $message
85
     *
86
     * @return void
87
     */
88
    protected function setFailureExceptionRegex(?string $message = null)
89
    {
90
        $this->expectException(ExpectationFailedException::class);
91
        if ($message !== null) {
92
            $this->expectExceptionMessageRegExp($message);
93
        }
94
    }
95
96
    /**
97
     * Set the expected exception and message when testing a call with invalid arguments to a method.
98
     *
99
     * @param integer $arg
100
     * @param string  $type
101
     * @param mixed   $value
102
     *
103
     * @return void
104
     */
105
    protected function setInvalidArgumentException(int $arg, string $type, $value = null)
106
    {
107
        $this->expectException(InvalidArgumentException::class);
108
        $this->expectExceptionMessageRegExp(
109
            \sprintf(
110
                '/' . \preg_quote(InvalidArgumentException::MESSAGE) . '/',
111
                $arg,
112
                ($value === null) ?
113
                    '[\s\S]*' : ' \(' . \gettype($value) . '#' . \preg_quote(\var_export($value, true)) . '\)',
114
                '.*',
115
                '.*',
116
                \preg_quote($type)
117
            )
118
        );
119
    }
120
121
    /**
122
     * Format the failure message as a regular expression.
123
     *
124
     * @param string $message
125
     *
126
     * @return string
127
     */
128
    protected function formatAsRegex(string $message): string
129
    {
130
        return '/' . preg_replace(
131
            "!\%(\+?)('.|[0 ]|)(-?)([1-9][0-9]*|)(\.[1-9][0-9]*|)([%a-zA-Z])!u",
132
            '.*',
133
            preg_quote($message)
134
        ) . '/s';
135
    }
136
}
137