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

namespace Engelsystem\Test\Unit\Middleware;

use Engelsystem\Application;
use Engelsystem\Middleware\CallableHandler;
use Engelsystem\Test\Unit\Middleware\Stub\HasStaticMethod;
use Engelsystem\Test\Unit\Middleware\Stub\ResolvesMiddlewareTraitImplementation;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Server\MiddlewareInterface;

class ResolvesMiddlewareTraitTest extends TestCase
{
    /**
     * @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::isMiddleware
     * @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::resolveMiddleware
     */
    public function testResolveMiddleware()
    {
        /** @var Application|MockObject $container */
        $container = $this->createMock(Application::class);
        $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
        $callable = [HasStaticMethod::class, 'foo'];

        $container->expects($this->exactly(3))
            ->method('make')
            ->withConsecutive(
                ['FooBarClass'],
                [CallableHandler::class, ['callable' => $callable]],
                ['UnresolvableClass']
            )
            ->willReturnOnConsecutiveCalls(
                $middlewareInterface,
                $middlewareInterface,
                null
            );

        $middleware = new ResolvesMiddlewareTraitImplementation($container);

        $return = $middleware->callResolveMiddleware('FooBarClass');
        $this->assertEquals($middlewareInterface, $return);

        $return = $middleware->callResolveMiddleware($callable);
        $this->assertEquals($middlewareInterface, $return);

        $this->expectException(InvalidArgumentException::class);
        $middleware->callResolveMiddleware('UnresolvableClass');
    }

    /**
     * @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::resolveMiddleware
     */
    public function testResolveMiddlewareNoContainer()
    {
        $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);

        $middleware = new ResolvesMiddlewareTraitImplementation();
        $return = $middleware->callResolveMiddleware($middlewareInterface);

        $this->assertEquals($middlewareInterface, $return);

        $this->expectException(InvalidArgumentException::class);
        $middleware->callResolveMiddleware('FooBarClass');
    }
}