summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware/RouteDispatcherServiceProviderTest.php
blob: 3947ac372ae99139e683a1e594dd8d5e5b2c8e7e (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
<?php

namespace Engelsystem\Test\Unit\Middleware;

use Engelsystem\Config\Config;
use Engelsystem\Middleware\LegacyMiddleware;
use Engelsystem\Middleware\RouteDispatcher;
use Engelsystem\Middleware\RouteDispatcherServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use FastRoute\Dispatcher as FastRouteDispatcher;
use Illuminate\Contracts\Container\ContextualBindingBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Server\MiddlewareInterface;

class RouteDispatcherServiceProviderTest extends ServiceProviderTest
{
    /**
     * @covers \Engelsystem\Middleware\RouteDispatcherServiceProvider::register()
     */
    public function testRegister()
    {
        /** @var ContextualBindingBuilder|MockObject $bindingBuilder */
        $bindingBuilder = $this->createMock(ContextualBindingBuilder::class);
        /** @var FastRouteDispatcher|MockObject $routeDispatcher */
        $routeDispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class);
        $config = new Config(['environment' => 'development']);

        $app = $this->getApp(['alias', 'when', 'get']);

        $app->expects($this->exactly(2))
            ->method('get')
            ->withConsecutive(['config'], ['path.cache.routes'])
            ->willReturn($config, '/foo/routes.cache');

        $app->expects($this->once())
            ->method('alias')
            ->with(RouteDispatcher::class, 'route.dispatcher');

        $app->expects($this->exactly(2))
            ->method('when')
            ->with(RouteDispatcher::class)
            ->willReturn($bindingBuilder);

        $bindingBuilder->expects($this->exactly(2))
            ->method('needs')
            ->withConsecutive(
                [FastRouteDispatcher::class],
                [MiddlewareInterface::class]
            )
            ->willReturn($bindingBuilder);

        $bindingBuilder->expects($this->exactly(2))
            ->method('give')
            ->with($this->callback(function ($subject) {
                if (is_callable($subject)) {
                    $subject();
                }

                return is_callable($subject) || $subject == LegacyMiddleware::class;
            }));

        /** @var RouteDispatcherServiceProvider|MockObject $serviceProvider */
        $serviceProvider = $this->getMockBuilder(RouteDispatcherServiceProvider::class)
            ->setConstructorArgs([$app])
            ->setMethods(['generateRouting'])
            ->getMock();

        $serviceProvider->expects($this->once())
            ->method('generateRouting')
            ->willReturn($routeDispatcher);

        $serviceProvider->register();
    }
}