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

namespace Engelsystem\Test\Unit\Middleware;

use Engelsystem\Helpers\Translator;
use Engelsystem\Middleware\SetLocale;
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 Symfony\Component\HttpFoundation\Session\Session;

class SetLocaleTest extends TestCase
{
    /**
     * @covers \Engelsystem\Middleware\SetLocale::__construct
     * @covers \Engelsystem\Middleware\SetLocale::process
     */
    public function testRegister()
    {
        /** @var Translator|MockObject $translator */
        $translator = $this->createMock(Translator::class);
        /** @var Session|MockObject $session */
        $session = $this->createMock(Session::class);
        /** @var ServerRequestInterface|MockObject $request */
        $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
        /** @var RequestHandlerInterface|MockObject $handler */
        $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
        /** @var ResponseInterface|MockObject $response */
        $response = $this->getMockForAbstractClass(ResponseInterface::class);

        $locale = 'te_ST.UTF8';

        $request->expects($this->exactly(3))
            ->method('getQueryParams')
            ->willReturnOnConsecutiveCalls(
                [],
                ['set-locale' => 'en_US.UTF8'],
                ['set-locale' => $locale]
            );

        $translator->expects($this->exactly(2))
            ->method('hasLocale')
            ->withConsecutive(
                ['en_US.UTF8'],
                [$locale]
            )
            ->willReturnOnConsecutiveCalls(
                false,
                true
            );
        $translator->expects($this->once())
            ->method('setLocale')
            ->with($locale);

        $session->expects($this->once())
            ->method('set')
            ->with('locale', $locale);

        $handler->expects($this->exactly(3))
            ->method('handle')
            ->with($request)
            ->willReturn($response);

        $middleware = new SetLocale($translator, $session);
        $middleware->process($request, $handler);
        $middleware->process($request, $handler);
        $middleware->process($request, $handler);
    }
}