summaryrefslogtreecommitdiff
path: root/tests/Unit/Exceptions/HandlerTest.php
blob: 5a6ffe166cd1259f20bc2b7a66b31e64f3c9265d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

namespace Engelsystem\Test\Unit\Exceptions;

use Engelsystem\Exceptions\Handler;
use Engelsystem\Exceptions\Handlers\HandlerInterface;
use Engelsystem\Http\Request;
use ErrorException;
use Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as Mock;

class HandlerTest extends TestCase
{
    /**
     * @covers \Engelsystem\Exceptions\Handler::__construct()
     */
    public function testCreate()
    {
        /** @var Handler|Mock $handler */
        $handler = new Handler();
        $this->assertInstanceOf(Handler::class, $handler);
        $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment());

        $anotherHandler = new Handler(Handler::ENV_DEVELOPMENT);
        $this->assertEquals(Handler::ENV_DEVELOPMENT, $anotherHandler->getEnvironment());
    }

    /**
     * @covers \Engelsystem\Exceptions\Handler::errorHandler()
     */
    public function testErrorHandler()
    {
        /** @var Handler|Mock $handler */
        $handler = $this->getMockBuilder(Handler::class)
            ->setMethods(['exceptionHandler'])
            ->getMock();

        $handler->expects($this->once())
            ->method('exceptionHandler')
            ->with($this->isInstanceOf(ErrorException::class));

        $handler->errorHandler(1, 'Foo and bar!', '/lo/rem.php', 123);
    }

    /**
     * @covers \Engelsystem\Exceptions\Handler::exceptionHandler()
     */
    public function testExceptionHandler()
    {
        $exception = new Exception();

        /** @var HandlerInterface|Mock $handlerMock */
        $handlerMock = $this->getMockForAbstractClass(HandlerInterface::class);
        $handlerMock->expects($this->once())
            ->method('report')
            ->with($exception);
        $handlerMock->expects($this->once())
            ->method('render')
            ->with($this->isInstanceOf(Request::class), $exception);

        /** @var Handler|Mock $handler */
        $handler = $this->getMockBuilder(Handler::class)
            ->setMethods(['terminateApplicationImmediately'])
            ->getMock();
        $handler->expects($this->once())
            ->method('terminateApplicationImmediately');

        $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock);

        $handler->exceptionHandler($exception);
    }

    /**
     * @covers \Engelsystem\Exceptions\Handler::register()
     */
    public function testRegister()
    {
        /** @var Handler|Mock $handler */
        $handler = $this->getMockForAbstractClass(Handler::class);
        $handler->register();

        set_error_handler($errorHandler = set_error_handler('var_dump'));
        $this->assertEquals($handler, array_shift($errorHandler));

        set_exception_handler($exceptionHandler = set_error_handler('var_dump'));
        $this->assertEquals($handler, array_shift($exceptionHandler));

        restore_error_handler();
        restore_exception_handler();
    }

    /**
     * @covers \Engelsystem\Exceptions\Handler::setEnvironment()
     * @covers \Engelsystem\Exceptions\Handler::getEnvironment()
     */
    public function testEnvironment()
    {
        $handler = new Handler();

        $handler->setEnvironment(Handler::ENV_DEVELOPMENT);
        $this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment());

        $handler->setEnvironment(Handler::ENV_PRODUCTION);
        $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment());
    }

    /**
     * @covers \Engelsystem\Exceptions\Handler::setHandler()
     * @covers \Engelsystem\Exceptions\Handler::getHandler()
     */
    public function testHandler()
    {
        $handler = new Handler();
        /** @var HandlerInterface|Mock $devHandler */
        $devHandler = $this->getMockForAbstractClass(HandlerInterface::class);
        /** @var HandlerInterface|Mock $prodHandler */
        $prodHandler = $this->getMockForAbstractClass(HandlerInterface::class);

        $handler->setHandler(Handler::ENV_DEVELOPMENT, $devHandler);
        $handler->setHandler(Handler::ENV_PRODUCTION, $prodHandler);
        $this->assertEquals($devHandler, $handler->getHandler(Handler::ENV_DEVELOPMENT));
        $this->assertEquals($prodHandler, $handler->getHandler(Handler::ENV_PRODUCTION));
        $this->assertCount(2, $handler->getHandler());
    }

    /**
     * @covers \Engelsystem\Exceptions\Handler::setRequest()
     * @covers \Engelsystem\Exceptions\Handler::getRequest()
     */
    public function testRequest()
    {
        $handler = new Handler();
        /** @var Request|Mock $request */
        $request = $this->createMock(Request::class);

        $handler->setRequest($request);
        $this->assertEquals($request, $handler->getRequest());
    }
}