Passed
Push — master ( 699175...c40f63 )
by Vincent
02:43
created

SetExceptionsTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFailureException() 0 5 2
A setFailureExceptionRegex() 0 5 2
A setInvalidArgumentException() 0 11 2
1
<?php
2
declare (strict_types = 1);
3
4
namespace VGirol\JsonApiAssert;
5
6
use PHPUnit\Framework\ExpectationFailedException;
7
8
/**
9
 * Some helpers for testing
10
 *
11
 * @codeCoverageIgnore
12
 */
13
trait SetExceptionsTrait
14
{
15
    /**
16
     * Set the class name of the expected exception
17
     *
18
     * @see \PHPUnit\Framework\TestCase::expectException
19
     * @param string $exception
20
     * @return void
21
     */
22
    abstract public function expectException(string $exception): void;
23
24
    /**
25
     * Set the message of the expected exception
26
     *
27
     * @see \PHPUnit\Framework\TestCase::expectExceptionMessage
28
     * @param string $message
29
     * @return void
30
     */
31
    abstract public function expectExceptionMessage(string $message): void;
32
33
    /**
34
     * Set the a regular expression for the message of the expected exception
35
     *
36
     * @see \PHPUnit\Framework\TestCase::expectExceptionMessageRegExp
37
     * @param string $messageRegExp
38
     * @return void
39
     */
40
    abstract public function expectExceptionMessageRegExp(string $messageRegExp): void;
41
42
    /**
43
     * Set the expected exception and message when defining a test that will fail.
44
     *
45
     * @param string|null $message
46
     * @return void
47
     */
48
    protected function setFailureException($message)
49
    {
50
        $this->expectException(ExpectationFailedException::class);
51
        if (!is_null($message)) {
52
            $this->expectExceptionMessage($message);
53
        }
54
    }
55
56
    /**
57
     * Set the expected exception and message when defining a test that will fail.
58
     *
59
     * @param string|null $message
60
     * @return void
61
     */
62
    protected function setFailureExceptionRegex($message)
63
    {
64
        $this->expectException(ExpectationFailedException::class);
65
        if (!is_null($message)) {
66
            $this->expectExceptionMessageRegExp($message);
67
        }
68
    }
69
70
    /**
71
     * Set the expected exception and message when testing a call with invalid arguments to a method.
72
     *
73
     * @param integer $arg
74
     * @param string $type
75
     * @param mixed $value
76
     * @return void
77
     */
78
    protected function setInvalidArgumentException(int $arg, string $type, $value = null)
79
    {
80
        $this->expectException(Exception::class);
81
        $this->expectExceptionMessageRegExp(
82
            \sprintf(
83
                '/' . Exception::INVALID_ARGUMENT . '/',
84
                $arg,
85
                is_null($value) ? '[\s\S]*' : ' \(' . \gettype($value) . '#' . $value . '\)',
86
                '.*',
87
                '.*',
88
                \preg_quote($type)
89
            )
90
        );
91
    }
92
}
93