Passed
Branch master (f022b4)
by Adrian Florin
02:29
created

ErrorToExceptionConverterTest::testHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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