Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

SetExceptionsTrait::setInvalidArgumentException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Testing;
6
7
use VGirol\JsonApiStructure\Exception\InvalidArgumentException;
8
use VGirol\JsonApiStructure\Exception\InvalidArgumentHelper;
9
use VGirol\JsonApiStructure\Exception\ValidationException;
10
11
/**
12
 * Some helpers for testing
13
 *
14
 * @codeCoverageIgnore
15
 */
16
trait SetExceptionsTrait
17
{
18
    /**
19
     * Set the class name of the expected exception
20
     *
21
     * @see \PHPUnit\Framework\TestCase::expectException
22
     *
23
     * @param string $exception
24
     *
25
     * @return void
26
     */
27
    abstract public function expectException(string $exception): void;
28
29
    /**
30
     * Set the message of the expected exception
31
     *
32
     * @see \PHPUnit\Framework\TestCase::expectExceptionMessage
33
     *
34
     * @param string $message
35
     *
36
     * @return void
37
     */
38
    abstract public function expectExceptionMessage(string $message): void;
39
40
    /**
41
     * Set the a regular expression for the message of the expected exception
42
     *
43
     * @see \PHPUnit\Framework\TestCase::expectExceptionMessageRegExp
44
     *
45
     * @param string $messageRegExp
46
     *
47
     * @return void
48
     */
49
    abstract public function expectExceptionMessageRegExp(string $messageRegExp): void;
50
51
52
    /**
53
     * Set the expected exception and message when defining a test that will fail.
54
     *
55
     * @param string|null $message The failure message could be either a string or a regular expression.
56
     *
57
     * @return void
58
     */
59
    protected function setFailure(?string $message = null, $code = null, $className = ValidationException::class): void
60
    {
61
        $method = (($message !== null) && (strpos($message, '/') === 0))
62
            ? 'setFailureExceptionRegex' : 'setFailureException';
63
        $this->{$method}($className, $message, $code);
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 $className, ?string $message = null, $code = null): void
74
    {
75
        $this->expectException($className);
76
        if ($message !== null) {
77
            $this->expectExceptionMessage($message);
78
        }
79
        if ($code !== null) {
80
            $this->expectExceptionCode($code);
0 ignored issues
show
Bug introduced by
The method expectExceptionCode() does not exist on VGirol\JsonApiStructure\Testing\SetExceptionsTrait. Did you maybe mean expectException()? ( Ignorable by Annotation )

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

80
            $this->/** @scrutinizer ignore-call */ 
81
                   expectExceptionCode($code);

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...
81
        }
82
    }
83
84
    /**
85
     * Set the expected exception and message when defining a test that will fail.
86
     *
87
     * @param string|null $message
88
     *
89
     * @return void
90
     */
91
    protected function setFailureExceptionRegex(string $className, ?string $message = null, $code = null): void
92
    {
93
        $this->expectException($className);
94
        if ($message !== null) {
95
            $this->expectExceptionMessageRegExp($message);
96
        }
97
        if ($code !== null) {
98
            $this->expectExceptionCode($code);
99
        }
100
    }
101
102
    /**
103
     * Set the expected exception and message when testing a call with invalid arguments to a method.
104
     *
105
     * @param integer $arg
106
     * @param string  $type
107
     * @param mixed   $value
108
     *
109
     * @return void
110
     */
111
    protected function setInvalidArgumentException(int $arg, string $type, $value = null): void
112
    {
113
        $this->expectException(InvalidArgumentException::class);
114
        $this->expectExceptionMessageRegExp(InvalidArgumentHelper::messageRegex($arg, $type, $value));
115
    }
116
117
    /**
118
     * Format the failure message as a regular expression.
119
     *
120
     * @param string $message
121
     *
122
     * @return string
123
     */
124
    protected function formatAsRegex(string $message): string
125
    {
126
        return '/' . preg_replace(
127
            "!\%(\+?)('.|[0 ]|)(-?)([1-9][0-9]*|)(\.[1-9][0-9]*|)([%a-zA-Z])!u",
128
            '.*',
129
            preg_quote($message)
130
        ) . '/s';
131
    }
132
}
133