2017-11-03 18:16:49 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Tests\Unit\Http\Middleware;
|
2017-11-03 18:16:49 -05:00
|
|
|
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Http\Middleware\LanguageMiddleware;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\User;
|
2017-11-03 18:16:49 -05:00
|
|
|
use Illuminate\Foundation\Application;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Mockery as m;
|
|
|
|
|
use Mockery\MockInterface;
|
2017-11-03 18:16:49 -05:00
|
|
|
|
|
|
|
|
class LanguageMiddlewareTest extends MiddlewareTestCase
|
|
|
|
|
{
|
2022-10-14 10:59:20 -06:00
|
|
|
private MockInterface $appMock;
|
2017-11-03 18:16:49 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Setup tests.
|
|
|
|
|
*/
|
2024-03-19 21:09:16 -04:00
|
|
|
protected function setUp(): void
|
2017-11-03 18:16:49 -05:00
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->appMock = m::mock(Application::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-06-01 16:22:06 +02:00
|
|
|
* Test that a language is defined via the middleware for guests.
|
2017-11-03 18:16:49 -05:00
|
|
|
*/
|
2025-02-25 14:22:07 +01:00
|
|
|
public function test_language_is_set_for_guest(): void
|
2017-11-03 18:16:49 -05:00
|
|
|
{
|
2018-06-01 16:22:06 +02:00
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->andReturnNull();
|
2017-11-03 18:16:49 -05:00
|
|
|
$this->appMock->shouldReceive('setLocale')->with('en')->once()->andReturnNull();
|
|
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 16:22:06 +02:00
|
|
|
/**
|
|
|
|
|
* Test that a language is defined via the middleware for a user.
|
|
|
|
|
*/
|
2025-02-25 14:22:07 +01:00
|
|
|
public function test_language_is_set_with_authenticated_user(): void
|
2018-09-03 15:17:53 -07:00
|
|
|
{
|
2021-01-23 12:09:16 -08:00
|
|
|
$user = User::factory()->make(['language' => 'de']);
|
2018-06-01 16:22:06 +02:00
|
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->andReturn($user);
|
|
|
|
|
$this->appMock->shouldReceive('setLocale')->with('de')->once()->andReturnNull();
|
|
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 18:16:49 -05:00
|
|
|
/**
|
|
|
|
|
* Return an instance of the middleware using mocked dependencies.
|
|
|
|
|
*/
|
|
|
|
|
private function getMiddleware(): LanguageMiddleware
|
|
|
|
|
{
|
2018-09-03 15:17:53 -07:00
|
|
|
return new LanguageMiddleware($this->appMock);
|
2017-11-03 18:16:49 -05:00
|
|
|
}
|
|
|
|
|
}
|