summaryrefslogtreecommitdiff
path: root/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php
blob: c056908660b0bf4040c5e712ae65e6249b638198 (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
<?php

namespace Engelsystem\Test\Unit\Exceptions;

use Engelsystem\Exceptions\ExceptionsServiceProvider;
use Engelsystem\Exceptions\Handler;
use Engelsystem\Exceptions\Handlers\HandlerInterface;
use Engelsystem\Exceptions\Handlers\Legacy;
use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
use Engelsystem\Exceptions\Handlers\Whoops;
use Engelsystem\Http\Request;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;

class ExceptionsServiceProviderTest extends ServiceProviderTest
{
    /**
     * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addDevelopmentHandler()
     * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addProductionHandler()
     * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register()
     */
    public function testRegister()
    {
        $app = $this->getApp(['make', 'instance', 'bind']);

        /** @var Handler|MockObject $handler */
        $handler = $this->createMock(Handler::class);
        $this->setExpects($handler, 'register');
        /** @var Legacy|MockObject $legacyHandler */
        $legacyHandler = $this->createMock(Legacy::class);
        /** @var LegacyDevelopment|MockObject $developmentHandler */
        $developmentHandler = $this->createMock(LegacyDevelopment::class);

        $whoopsHandler = $this->getMockBuilder(Whoops::class)
            ->setConstructorArgs([$app])
            ->getMock();

        $app->expects($this->exactly(3))
            ->method('instance')
            ->withConsecutive(
                ['error.handler.production', $legacyHandler],
                ['error.handler.development', $whoopsHandler],
                ['error.handler', $handler]
            );

        $app->expects($this->exactly(4))
            ->method('make')
            ->withConsecutive(
                [Handler::class],
                [Legacy::class],
                [LegacyDevelopment::class],
                [Whoops::class]
            )
            ->willReturnOnConsecutiveCalls(
                $handler,
                $legacyHandler,
                $developmentHandler,
                $whoopsHandler
            );

        $app->expects($this->exactly(2))
            ->method('bind')
            ->withConsecutive(
                [HandlerInterface::class, 'error.handler.production'],
                [Handler::class, 'error.handler']
            );

        $handler->expects($this->exactly(2))
            ->method('setHandler')
            ->withConsecutive(
                [Handler::ENV_PRODUCTION, $legacyHandler],
                [Handler::ENV_DEVELOPMENT, $whoopsHandler]
            );

        $serviceProvider = new ExceptionsServiceProvider($app);
        $serviceProvider->register();
    }

    /**
     * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::boot()
     */
    public function testBoot()
    {
        /** @var Handler|MockObject $handler */
        $handler = $this->createMock(Handler::class);

        /** @var Request|MockObject $request */
        $request = $this->createMock(Request::class);

        $handler->expects($this->once())
            ->method('setRequest')
            ->with($request);

        $app = $this->getApp(['get']);
        $app->expects($this->exactly(2))
            ->method('get')
            ->withConsecutive(
                ['error.handler'],
                ['request']
            )
            ->willReturnOnConsecutiveCalls(
                $handler,
                $request
            );

        $provider = new ExceptionsServiceProvider($app);
        $provider->boot();
    }
}