Completed
Push — master ( c08493...f697a2 )
by Vincent
02:37
created

SetExceptionsTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setFailureException() 0 5 2
A setInvalidArgumentException() 0 11 2
1
<?php
2
3
namespace VGirol\JsonApiAssert;
4
5
use PHPUnit\Framework\Exception;
6
use PHPUnit\Framework\ExpectationFailedException;
7
8
/**
9
 * Some helpers for testing
10
 */
11
trait SetExceptionsTrait
12
{
13
    /**
14
     * Set the expected exception and message when defining a test that will fail.
15
     *
16
     * @param string|null $message
17
     * @return void
18
     */
19
    protected function setFailureException($message)
20
    {
21
        $this->expectException(ExpectationFailedException::class);
0 ignored issues
show
Bug introduced by
It seems like expectException() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

21
        $this->/** @scrutinizer ignore-call */ 
22
               expectException(ExpectationFailedException::class);
Loading history...
22
        if (!is_null($message)) {
23
            $this->expectExceptionMessage($message);
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
            $this->/** @scrutinizer ignore-call */ 
24
                   expectExceptionMessage($message);
Loading history...
24
        }
25
    }
26
27
    /**
28
     * Set the expected exception and message when testing a call with invalid arguments to a method.
29
     *
30
     * @param integer $arg
31
     * @param string $type
32
     * @param mixed $value
33
     * @return void
34
     */
35
    protected function setInvalidArgumentException(int $arg, string $type, $value = null)
36
    {
37
        $this->expectException(Exception::class);
38
        $this->expectExceptionMessageRegExp(
0 ignored issues
show
Bug introduced by
It seems like expectExceptionMessageRegExp() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
        $this->/** @scrutinizer ignore-call */ 
39
               expectExceptionMessageRegExp(
Loading history...
39
            \sprintf(
40
                '/Argument #%d%sof %s::%s\(\) must be a %s/',
41
                $arg,
42
                is_null($value) ? '[\s\S]*' : ' \(' . \gettype($value) . '#' . $value . '\)',
43
                '.*',
44
                '.*',
45
                \preg_quote($type)
46
            )
47
        );
48
    }
49
}
50