1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use DigiTickets\PHPUnit\ErrorHandler; |
4
|
|
|
|
5
|
|
|
class tests extends \PHPUnit\Framework\TestCase |
|
|
|
|
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 testAssertError() |
20
|
|
|
{ |
21
|
|
|
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
22
|
|
|
$this->expectExceptionMessage('Error with level 1024 and message \'Unknown E_USER_WARNING\' not found in array'); |
23
|
|
|
|
24
|
|
|
$this->assertError('Unknown E_USER_WARNING', E_USER_NOTICE); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testPHPGeneratedNoticeIsCaptured() |
28
|
|
|
{ |
29
|
|
|
$keys = [1, 2, 3]; |
30
|
|
|
$keys[1] = $keys[4]; |
31
|
|
|
|
32
|
|
|
$this->assertError('Undefined offset: 4', E_NOTICE); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testPHPGeneratedRecoverableErrorIsCaptured() |
36
|
|
|
{ |
37
|
|
|
require __DIR__ . '/_files/testClassForRecoverableError.php'; |
38
|
|
|
|
39
|
|
|
$it = new ArrayIterator(new testClassForRecoverableError()); |
40
|
|
|
$it->append('will not work'); |
41
|
|
|
|
42
|
|
|
$this->assertError('ArrayIterator::append(): Cannot append properties to objects, use ArrayIterator::offsetSet() instead', E_RECOVERABLE_ERROR); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testPHPGeneratedWarningIsCaptured() |
46
|
|
|
{ |
47
|
|
|
$keys = [1, 2, 3]; |
48
|
|
|
$values = [1, 2, 3, 4]; |
49
|
|
|
array_combine($keys, $values); |
50
|
|
|
|
51
|
|
|
$this->assertError('array_combine(): Both parameters should have an equal number of elements', E_WARNING); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @dataProvider provideUserErrors |
56
|
|
|
* |
57
|
|
|
* @param string $message |
58
|
|
|
* @param int $errorType |
59
|
|
|
*/ |
60
|
|
|
public function testUserErrorsAreCaptured($message, $errorType) |
61
|
|
|
{ |
62
|
|
|
trigger_error($message, $errorType); |
63
|
|
|
|
64
|
|
|
$this->assertError($message, $errorType); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testNoErrorsAreCapturedWhenNoErrorsAreGenerated() |
68
|
|
|
{ |
69
|
|
|
$this->assertNoErrors(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.