2018-08-28 22:23:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
2021-01-02 17:05:18 +01:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2019-07-08 01:31:59 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2018-08-28 22:23:59 +02:00
|
|
|
use Engelsystem\Middleware\SetLocale;
|
2021-01-02 17:05:18 +01:00
|
|
|
use Engelsystem\Models\User\Settings;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-08-28 22:23:59 +02:00
|
|
|
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
|
|
|
|
{
|
2021-01-02 17:05:18 +01:00
|
|
|
use HasDatabase;
|
|
|
|
|
2018-08-28 22:23:59 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\SetLocale::__construct
|
|
|
|
* @covers \Engelsystem\Middleware\SetLocale::process
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2018-08-28 22:23:59 +02:00
|
|
|
{
|
2021-01-02 17:05:18 +01:00
|
|
|
$this->initDatabase();
|
|
|
|
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
2018-08-28 22:23:59 +02:00
|
|
|
/** @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);
|
|
|
|
|
2021-06-29 00:27:57 +02:00
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory([
|
|
|
|
'name' => 'user',
|
|
|
|
'email' => 'foo@bar.baz',
|
|
|
|
])
|
|
|
|
->has(Settings::factory(['language' => 'uf_UF']))
|
|
|
|
->create();
|
2021-01-02 17:05:18 +01:00
|
|
|
|
|
|
|
$locale = 'te_ST';
|
2018-08-28 22:23:59 +02:00
|
|
|
|
|
|
|
$request->expects($this->exactly(3))
|
|
|
|
->method('getQueryParams')
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
[],
|
2021-01-02 17:05:18 +01:00
|
|
|
['set-locale' => 'en_US'],
|
2018-08-28 22:23:59 +02:00
|
|
|
['set-locale' => $locale]
|
|
|
|
);
|
|
|
|
|
|
|
|
$translator->expects($this->exactly(2))
|
|
|
|
->method('hasLocale')
|
|
|
|
->withConsecutive(
|
2021-01-02 17:05:18 +01:00
|
|
|
['en_US'],
|
2018-08-28 22:23:59 +02:00
|
|
|
[$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);
|
|
|
|
|
2021-01-02 17:05:18 +01:00
|
|
|
$this->setExpects($auth, 'user', null, $user);
|
|
|
|
|
|
|
|
$middleware = new SetLocale($translator, $session, $auth);
|
2018-08-28 22:23:59 +02:00
|
|
|
$middleware->process($request, $handler);
|
2021-01-02 17:05:18 +01:00
|
|
|
|
2018-08-28 22:23:59 +02:00
|
|
|
$middleware->process($request, $handler);
|
2021-01-02 17:05:18 +01:00
|
|
|
$this->assertEquals('uf_UF', $user->settings->language);
|
|
|
|
|
2018-08-28 22:23:59 +02:00
|
|
|
$middleware->process($request, $handler);
|
2021-01-02 17:05:18 +01:00
|
|
|
$this->assertEquals('te_ST', $user->settings->language);
|
2018-08-28 22:23:59 +02:00
|
|
|
}
|
|
|
|
}
|