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( |
33
|
|
|
defined('HHVM_VERSION') |
34
|
|
|
? 'Undefined index: 4' |
35
|
|
|
: 'Undefined offset: 4', |
36
|
|
|
E_NOTICE |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testPHPGeneratedRecoverableErrorIsCaptured() |
41
|
|
|
{ |
42
|
|
|
$x = function () { |
43
|
|
|
return 1; |
44
|
|
|
}; |
45
|
|
|
print (string)$x; |
46
|
|
|
$this->assertError( |
47
|
|
|
defined('HHVM_VERSION') |
48
|
|
|
? 'Object of class Closure$tests::testPHPGeneratedRecoverableErrorIsCaptured;4 could not be converted to string' |
49
|
|
|
: 'Object of class Closure could not be converted to string', |
50
|
|
|
E_RECOVERABLE_ERROR |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testPHPGeneratedWarningIsCaptured() |
55
|
|
|
{ |
56
|
|
|
$keys = [1, 2, 3]; |
57
|
|
|
$values = [1, 2, 3, 4]; |
58
|
|
|
array_combine($keys, $values); |
59
|
|
|
|
60
|
|
|
$this->assertError('array_combine(): Both parameters should have an equal number of elements', E_WARNING); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider provideUserErrors |
65
|
|
|
* |
66
|
|
|
* @param string $message |
67
|
|
|
* @param int $errorType |
68
|
|
|
*/ |
69
|
|
|
public function testUserErrorsAreCaptured($message, $errorType) |
70
|
|
|
{ |
71
|
|
|
trigger_error($message, $errorType); |
72
|
|
|
|
73
|
|
|
$this->assertError($message, $errorType); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testNoErrorsAreCapturedWhenNoErrorsAreGenerated() |
77
|
|
|
{ |
78
|
|
|
$this->assertNoErrors(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testAssertNoErrorsShouldFailWhenErrorRaised() |
82
|
|
|
{ |
83
|
|
|
// First error generated |
84
|
|
|
trigger_error('This is an error.', E_USER_WARNING); |
85
|
|
|
|
86
|
|
|
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
87
|
|
|
$this->expectExceptionMessage('1 error generated'); |
88
|
|
|
|
89
|
|
|
$this->assertNoErrors(); |
90
|
|
|
|
91
|
|
|
// Second error generated |
92
|
|
|
trigger_error('This is another error.', E_USER_WARNING); |
93
|
|
|
|
94
|
|
|
$this->expectException(\PHPUnit\Framework\AssertionFailedError::class); |
95
|
|
|
$this->expectExceptionMessage('2 errors generated'); |
96
|
|
|
|
97
|
|
|
$this->assertNoErrors(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.