|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DigiTicketsTests\PHPUnit\ErrorHandler; |
|
4
|
|
|
|
|
5
|
|
|
use DigiTickets\PHPUnit\ErrorHandler; |
|
6
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
7
|
|
|
|
|
8
|
|
|
class ErrorTest extends \PHPUnit\Framework\TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
use ErrorHandler; |
|
11
|
|
|
|
|
12
|
|
|
public function provideUserErrors() |
|
13
|
|
|
{ |
|
14
|
|
|
return [ |
|
15
|
|
|
'E_USER_ERROR' => ['Generated E_USER_ERROR', E_USER_ERROR], |
|
16
|
|
|
'E_USER_WARNING' => ['Generated E_USER_WARNING', E_USER_WARNING], |
|
17
|
|
|
'E_USER_NOTICE' => ['Generated E_USER_NOTICE', E_USER_NOTICE], |
|
18
|
|
|
'E_USER_DEPRECATED' => ['Generated E_USER_DEPRECATED', E_USER_DEPRECATED], |
|
19
|
|
|
]; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function testAssertError() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->expectException(AssertionFailedError::class); |
|
25
|
|
|
$this->expectExceptionMessage('Error with level 1024 and message \'Unknown E_USER_WARNING\' not found in array'); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertError('Unknown E_USER_WARNING', E_USER_NOTICE); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testPHPGeneratedNoticeIsCaptured() |
|
31
|
|
|
{ |
|
32
|
|
|
$keys = [1, 2, 3]; |
|
33
|
|
|
$keys[1] = $keys[4]; |
|
34
|
|
|
|
|
35
|
|
|
$this->assertError( |
|
36
|
|
|
defined('HHVM_VERSION') |
|
37
|
|
|
? 'Undefined index: 4' |
|
38
|
|
|
: 'Undefined offset: 4', |
|
39
|
|
|
E_NOTICE |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testPHPGeneratedRecoverableErrorIsCaptured() |
|
44
|
|
|
{ |
|
45
|
|
|
$x = function () { |
|
46
|
|
|
return 1; |
|
47
|
|
|
}; |
|
48
|
|
|
print (string) $x; |
|
49
|
|
|
$this->assertError( |
|
50
|
|
|
defined('HHVM_VERSION') |
|
51
|
|
|
? 'Object of class Closure$tests::testPHPGeneratedRecoverableErrorIsCaptured;4 could not be converted to string' |
|
52
|
|
|
: 'Object of class Closure could not be converted to string', |
|
53
|
|
|
E_RECOVERABLE_ERROR |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testPHPGeneratedWarningIsCaptured() |
|
58
|
|
|
{ |
|
59
|
|
|
$keys = [1, 2, 3]; |
|
60
|
|
|
$values = [1, 2, 3, 4]; |
|
61
|
|
|
array_combine($keys, $values); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertError('array_combine(): Both parameters should have an equal number of elements', E_WARNING); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @dataProvider provideUserErrors |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $message |
|
70
|
|
|
* @param int $errorType |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testUserErrorsAreCaptured($message, $errorType) |
|
73
|
|
|
{ |
|
74
|
|
|
trigger_error($message, $errorType); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertError($message, $errorType); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testNoErrorsAreCapturedWhenNoErrorsAreGenerated() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assertNoErrors(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testAssertNoErrorsShouldFailWhenErrorRaised() |
|
85
|
|
|
{ |
|
86
|
|
|
// First error generated |
|
87
|
|
|
trigger_error('This is an error.', E_USER_WARNING); |
|
88
|
|
|
|
|
89
|
|
|
$this->expectException(AssertionFailedError::class); |
|
90
|
|
|
$this->expectExceptionMessage('1 error generated'); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertNoErrors(); |
|
93
|
|
|
|
|
94
|
|
|
// Second error generated |
|
95
|
|
|
trigger_error('This is another error.', E_USER_WARNING); |
|
96
|
|
|
|
|
97
|
|
|
$this->expectException(AssertionFailedError::class); |
|
98
|
|
|
$this->expectExceptionMessage('2 errors generated'); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertNoErrors(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|