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

namespace Engelsystem\Test\Unit\Middleware;

use Engelsystem\Container\Container;
use Engelsystem\Http\Response;
use Engelsystem\Middleware\CallableHandler;
use Engelsystem\Test\Unit\Middleware\Stub\HasStaticMethod;
use InvalidArgumentException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use stdClass;

class CallableHandlerTest extends TestCase
{
    public function provideCallable()
    {
        return [
            [function () {
            }],
            [[$this, 'provideCallable']],
            [[HasStaticMethod::class, 'foo']],
        ];
    }

    /**
     * @dataProvider provideCallable
     * @covers       \Engelsystem\Middleware\CallableHandler::__construct
     * @covers       \Engelsystem\Middleware\CallableHandler::getCallable
     * @param callable $callable
     */
    public function testInit($callable)
    {
        $handler = new CallableHandler($callable);

        $this->assertEquals($callable, $handler->getCallable());
    }

    /**
     * @covers \Engelsystem\Middleware\CallableHandler::process
     */
    public function testProcess()
    {
        /** @var ServerRequestInterface|MockObject $request */
        /** @var ResponseInterface|MockObject $response */
        /** @var callable|MockObject $callable */
        /** @var RequestHandlerInterface|MockObject $handler */
        list($request, $response, $callable, $handler) = $this->getMocks();

        $callable->expects($this->once())
            ->method('__invoke')
            ->with($request, $handler)
            ->willReturn($response);

        $middleware = new CallableHandler($callable);
        $middleware->process($request, $handler);
    }

    /**
     * @covers \Engelsystem\Middleware\CallableHandler::handle
     */
    public function testHandler()
    {
        /** @var ServerRequestInterface|MockObject $request */
        /** @var ResponseInterface|MockObject $response */
        /** @var callable|MockObject $callable */
        list($request, $response, $callable) = $this->getMocks();

        $callable->expects($this->once())
            ->method('__invoke')
            ->with($request)
            ->willReturn($response);

        $middleware = new CallableHandler($callable);
        $middleware->handle($request);
    }

    /**
     * @covers \Engelsystem\Middleware\CallableHandler::execute
     */
    public function testExecute()
    {
        /** @var ServerRequestInterface|MockObject $request */
        /** @var Response|MockObject $response */
        /** @var callable|MockObject $callable */
        list($request, $response, $callable) = $this->getMocks();
        /** @var Container|MockObject $container */
        $container = $this->createMock(Container::class);

        $callable->expects($this->exactly(3))
            ->method('__invoke')
            ->with($request)
            ->willReturnOnConsecutiveCalls($response, 'Lorem ipsum?', 'I\'m not an exception!');

        $container->expects($this->once())
            ->method('get')
            ->with('response')
            ->willReturn($response);

        $response->expects($this->once())
            ->method('withContent')
            ->with('Lorem ipsum?')
            ->willReturn($response);

        $middleware = new CallableHandler($callable, $container);
        $return = $middleware->handle($request);
        $this->assertInstanceOf(ResponseInterface::class, $return);
        $this->assertEquals($response, $return);

        $return = $middleware->handle($request);
        $this->assertInstanceOf(ResponseInterface::class, $return);
        $this->assertEquals($response, $return);

        $middleware = new CallableHandler($callable);
        $this->expectException(InvalidArgumentException::class);
        $middleware->handle($request);
    }

    /**
     * @return array
     */
    protected function getMocks(): array
    {
        /** @var ServerRequestInterface|MockObject $request */
        $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
        /** @var RequestHandlerInterface|MockObject $handler */
        $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
        /** @var Response|MockObject $response */
        $response = $this->createMock(Response::class);
        /** @var callable|MockObject $callable */
        $callable = $this->getMockBuilder(stdClass::class)
            ->setMethods(['__invoke'])
            ->getMock();
        return [$request, $response, $callable, $handler];
    }
}