|
1
|
|
|
<?php |
|
2
|
|
|
namespace NeedleProject\Common\Util; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use PHPUnit\Framework\Error\Error; |
|
6
|
|
|
|
|
7
|
|
|
class ErrorToExceptionConverterTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Restore base error handler after each test |
|
11
|
|
|
*/ |
|
12
|
|
|
public function tearDown() |
|
13
|
|
|
{ |
|
14
|
|
|
\restore_error_handler(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @expectedException \Exception |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testHandler() |
|
21
|
|
|
{ |
|
22
|
|
|
$exceptionConverter = new ErrorToExceptionConverter(); |
|
23
|
|
|
$exceptionConverter->convertErrorsToExceptions(); |
|
24
|
|
|
trigger_error("Dummy", E_USER_ERROR); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @expectedException \RuntimeException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testCustomException() |
|
31
|
|
|
{ |
|
32
|
|
|
$exceptionConverter = new ErrorToExceptionConverter(); |
|
33
|
|
|
$exceptionConverter->convertErrorsToExceptions(E_ALL, \RuntimeException::class); |
|
34
|
|
|
trigger_error("Dummy", E_USER_ERROR); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* //expectedException \ |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testRestore() |
|
41
|
|
|
{ |
|
42
|
|
|
$exceptionConverter = new ErrorToExceptionConverter(); |
|
43
|
|
|
$exceptionConverter->convertErrorsToExceptions(E_ALL, \RuntimeException::class); |
|
44
|
|
|
try { |
|
45
|
|
|
trigger_error("Dummy", E_USER_ERROR); |
|
46
|
|
|
} catch (\Exception $e) { |
|
47
|
|
|
$this->assertEquals( |
|
48
|
|
|
sprintf("Dummy in %s on line %s!", __FILE__, __LINE__ - 3), |
|
49
|
|
|
$e->getMessage() |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$exceptionConverter->restoreErrorHandler(); |
|
54
|
|
|
$this->expectException(Error::class); |
|
55
|
|
|
$this->expectExceptionMessage('Dummy'); |
|
56
|
|
|
trigger_error("Dummy", E_USER_ERROR); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|