Completed
Push — master ( 6e6006...017118 )
by Richard
02:39
created

tests   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 75
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A provideUserErrors() 0 9 1
A testAssertErrorHandler() 0 6 1
A testAssertError() 0 7 1
A testPHPGeneratedNoticeIsCaptured() 0 7 1
A testPHPGeneratedRecoverableErrorIsCaptured() 0 9 1
A testPHPGeneratedWarningIsCaptured() 0 8 1
A testUserErrorsAreCaptured() 0 6 1
A setUp() 0 5 1
1
<?php
2
3
use DigiTickets\PHPUnit\ErrorHandler;
4
5
class tests extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    use ErrorHandler;
8
9
    public function provideUserErrors()
10
    {
11
        return [
12
            ['Generated E_USER_ERROR', E_USER_ERROR],
13
            ['Generated E_USER_WARNING', E_USER_WARNING],
14
            ['Generated E_USER_NOTICE', E_USER_NOTICE],
15
            ['Generated E_USER_DEPRECATED', E_USER_DEPRECATED],
16
        ];
17
    }
18
19
    public function testAssertErrorHandler()
20
    {
21
        $this->errorHandler(E_USER_NOTICE, 'Testing ErrorHandler', __FILE__, __LINE__, []);
22
23
        $this->assertError('Testing ErrorHandler', E_USER_NOTICE);
24
    }
25
26
    public function testAssertError()
27
    {
28
        $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
29
        $this->expectExceptionMessage('Error with level 1024 and message \'Unknown E_USER_WARNING\' not found in array');
30
31
        $this->assertError('Unknown E_USER_WARNING', E_USER_NOTICE);
32
    }
33
34
    public function testPHPGeneratedNoticeIsCaptured()
35
    {
36
        $keys = [1, 2, 3];
37
        $keys[1] = $keys[4];
38
39
        $this->assertError('Undefined offset: 4', E_NOTICE);
40
    }
41
42
    public function testPHPGeneratedRecoverableErrorIsCaptured()
43
    {
44
        require __DIR__ . '/_files/testClassForRecoverableError.php';
45
46
        $it = new ArrayIterator(new testClassForRecoverableError());
47
        $it->append('will not work');
48
49
        $this->assertError('ArrayIterator::append(): Cannot append properties to objects, use ArrayIterator::offsetSet() instead', E_RECOVERABLE_ERROR);
50
    }
51
52
    public function testPHPGeneratedWarningIsCaptured()
53
    {
54
        $keys = [1, 2, 3];
55
        $values = [1, 2, 3, 4];
56
        array_combine($keys, $values);
57
58
        $this->assertError('array_combine(): Both parameters should have an equal number of elements', E_WARNING);
59
    }
60
61
    /**
62
     * @dataProvider provideUserErrors
63
     *
64
     * @param string $message
65
     * @param int $errorType
66
     */
67
    public function testUserErrorsAreCaptured($message, $errorType)
68
    {
69
        trigger_error($message, $errorType);
70
71
        $this->assertError($message, $errorType);
72
    }
73
74
    protected function setUp()
75
    {
76
        // Set the error handler.
77
        $this->setUpErrorHandler();
78
    }
79
}
80