-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check that requested locale is supported
- Loading branch information
Showing
4 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Chialab\FrontendKit\Test\TestCase\Middleware; | ||
|
||
use Cake\Core\Configure; | ||
use Cake\Http\ServerRequest; | ||
use Cake\TestSuite\TestCase; | ||
use Chialab\FrontendKit\Middleware\LocaleMiddleware; | ||
use Laminas\Diactoros\Response\EmptyResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* Test {@see \Chialab\FrontendKit\Middleware\LocaleMiddleware}. | ||
* | ||
* @covers \Chialab\FrontendKit\Middleware\LocaleMiddleware | ||
*/ | ||
class LocaleMiddlewareTest extends TestCase | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function localeProvider(): array | ||
{ | ||
return [ | ||
'invalid language code (ISO 639-1)' => [null, 'xk'], | ||
'invalid language code (ISO 639-2)' => [null, 'xkc'], | ||
'invalid language code (ISO 639-1 + ISO 3166-1 alpha-2)' => [null, 'xk_CD'], | ||
'correct language code (ISO 639-1)' => ['en', 'en'], | ||
'correct language code (ISO 639-2)' => ['eng', 'eng'], | ||
'correct language code (ISO 639-1 + ISO 3166-1 alpha-2)' => ['en_GB', 'en_GB'], | ||
]; | ||
} | ||
|
||
/** | ||
* Test {@see \Chialab\FrontendKit\Middleware\LocaleMiddleware::process()}. | ||
* | ||
* @param string|null $expectedLocale Expected configured locale. | ||
* @param string $locale Requested locale. | ||
* @return void | ||
* @dataProvider localeProvider()) | ||
*/ | ||
public function testLocale(?string $expectedLocale, string $locale): void | ||
{ | ||
$middleware = new LocaleMiddleware(); | ||
$request = new ServerRequest(['params' => compact('locale')]); | ||
$handler = new class implements RequestHandlerInterface { | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
return new EmptyResponse(); | ||
} | ||
}; | ||
$middleware->process($request, $handler); | ||
$actualLocale = Configure::read('I18n.lang'); | ||
|
||
static::assertEquals($expectedLocale, $actualLocale); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters