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

namespace Engelsystem\Test\Unit\Middleware;

use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Middleware\LegacyMiddleware;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\HttpFoundation\ParameterBag;

class LegacyMiddlewareTest extends TestCase
{
    /**
     * @covers \Engelsystem\Middleware\LegacyMiddleware::__construct
     * @covers \Engelsystem\Middleware\LegacyMiddleware::process
     */
    public function testRegister()
    {
        /** @var ContainerInterface|MockObject $container */
        $container = $this->getMockForAbstractClass(ContainerInterface::class);
        /** @var Authenticator|MockObject $auth */
        $auth = $this->createMock(Authenticator::class);
        /** @var LegacyMiddleware|MockObject $middleware */
        $middleware = $this->getMockBuilder(LegacyMiddleware::class)
            ->setConstructorArgs([$container, $auth])
            ->onlyMethods(['loadPage', 'renderPage'])
            ->getMock();
        /** @var Request|MockObject $defaultRequest */
        $defaultRequest = $this->createMock(Request::class);
        /** @var ParameterBag|MockObject $parameters */
        $parameters = $this->createMock(ParameterBag::class);
        /** @var ResponseInterface|MockObject $response */
        $response = $this->getMockForAbstractClass(ResponseInterface::class);
        /** @var RequestHandlerInterface|MockObject $handler */
        $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
        /** @var ServerRequestInterface|MockObject $request */
        $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
        /** @var Translator|MockObject $translator */
        $translator = $this->getMockBuilder(Translator::class)
            ->disableOriginalConstructor()
            ->getMock();

        $middleware->expects($this->once())
            ->method('loadPage')
            ->with('user_worklog')
            ->willReturn(['title', 'content']);

        $middleware->expects($this->exactly(2))
            ->method('renderPage')
            ->withConsecutive(
                ['user_worklog', 'title', 'content'],
                ['404', 'Page not found', 'It\'s not available!']
            )
            ->willReturn($response);

        $container->expects($this->exactly(3))
            ->method('get')
            ->withConsecutive(['request'], ['request'], ['translator'])
            ->willReturnOnConsecutiveCalls(
                $defaultRequest,
                $defaultRequest,
                $translator
            );

        $auth->expects($this->atLeastOnce())
            ->method('can')
            ->willReturn(false);

        $translator->expects($this->exactly(2))
            ->method('translate')
            ->willReturnOnConsecutiveCalls(
                'Page not found',
                'It\'s not available!'
            );

        $defaultRequest->query = $parameters;
        $defaultRequest->expects($this->once())
            ->method('path')
            ->willReturn('user-worklog');

        $parameters->expects($this->exactly(2))
            ->method('get')
            ->with('p')
            ->willReturnOnConsecutiveCalls(
                null,
                'foo'
            );

        $middleware->process($request, $handler);
        $middleware->process($request, $handler);
    }
}