Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LocaleRouteHelper::class. #8

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
private string $localeArgument = '_language';

public function __construct(
private LocaleRouteHelper $localeRouteHelper,
private readonly TranslatorInterface $translator,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly array $languages = [],
Expand All @@ -42,7 +43,7 @@
$path = $uri->getPath();
$language = $this->getLanguage($path);

new LocaleRouteHelper($path);
$this->localeRouteHelper->setPath($path);

Check warning on line 46 in src/Locale.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $uri = $request->getUri(); $path = $uri->getPath(); $language = $this->getLanguage($path); - $this->localeRouteHelper->setPath($path); + if ($this->shouldIgnoreUrl($request) === false) { $this->setLocale($language); }

Check warning on line 46 in src/Locale.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ $uri = $request->getUri(); $path = $uri->getPath(); $language = $this->getLanguage($path); - $this->localeRouteHelper->setPath($path); + if ($this->shouldIgnoreUrl($request) === false) { $this->setLocale($language); }

if ($this->shouldIgnoreUrl($request) === false) {
$this->setLocale($language);
Expand Down
10 changes: 9 additions & 1 deletion src/LocaleRouteHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
*/
final class LocaleRouteHelper
{
public function __construct(private readonly string $path)
private string $path = '';

/**
* Sets the path of the current request without locale part for default language.
*
* @param string $path the path of the current request without locale part for default language.
*/
public function setPath(string $path): void
{
$this->path = $path;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Locale/ImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testImmutable(): void
{
$this->createContainer();

$locale = new Locale($this->translator, $this->urlGenerator, [], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, [], []);
$this->assertNotSame($locale, $locale->withDefaultLanguage('en'));
$this->assertNotSame($locale, $locale->withLocaleArgument('_language'));
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Locale/LocaleRouteHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class LocaleRouteHelperTest extends TestCase
{
public function testLocaleRouteHelper(): void
{
$this->assertSame('/contact', (new LocaleRouteHelper('/contact'))->getPath());
$localeRouteHelper = new LocaleRouteHelper();
$localeRouteHelper->setPath('/contact');

$this->assertSame('/contact', $localeRouteHelper->getPath());
}
}
38 changes: 19 additions & 19 deletions tests/Locale/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testProcessSetsDefaultLanguageForInvalidLanguage(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -36,7 +36,7 @@ public function testProcessSetsDefaultLanguageForInvalidLanguage(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -48,7 +48,7 @@ public function testProcessSetsDefaultLanguageForInvalidLanguage(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -60,7 +60,7 @@ public function testProcessSetsDefaultLanguageForInvalidLanguage(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -75,7 +75,7 @@ public function testProcessSetsDefaultLanguageForInvalidPath(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
Expand All @@ -90,7 +90,7 @@ public function testProcessRedirectWithPathRootLanguage(): void

$request = new ServerRequest(method: 'GET', uri: $path);
$request = $request->withUri($request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -101,7 +101,7 @@ public function testProcessRedirectWithPathRootLanguage(): void
$path = '/en';

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -118,7 +118,7 @@ public function testProcessRedirectWithPathRootLanguageWithMethodPost(): void

$request = new ServerRequest(method: 'POST', uri: $path);
$request = $request->withUri($request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -130,7 +130,7 @@ public function testProcessRedirectWithPathRootLanguageWithMethodPost(): void

$request = new ServerRequest(method: 'POST', uri: $path);
$request = $request->withUri($request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -143,7 +143,7 @@ public function testProcessReturnsResponseInterface(): void
{
$this->createContainer();

$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($this->request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
Expand All @@ -158,7 +158,7 @@ public function testProcessWithDefaultLanguage(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
Expand All @@ -173,7 +173,7 @@ public function testProcessWithLanguageIsValid(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -184,7 +184,7 @@ public function testProcessWithLanguageIsValid(): void
$path = '/aa/login';

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -196,7 +196,7 @@ public function testProcessWithLanguageIsValid(): void
$path = '/DE/login';

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertInstanceOf(ResponseInterface::class, $response);
Expand All @@ -209,7 +209,7 @@ public function testProcessWithNotLocalesReturnsRequestHandlerResponse(): void
{
$this->createContainer();

$locale = new Locale($this->translator, $this->urlGenerator, [], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, [], []);
$response = $locale->process($this->request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
Expand All @@ -223,7 +223,7 @@ public function testProcessWithoutLanguage(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
Expand All @@ -239,7 +239,7 @@ public function testUrlGeneratorInterfaceSetDefaultArguments(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$response = $locale->process($request, $this->handler);

$this->assertSame(302, $response->getStatusCode());
Expand All @@ -252,7 +252,7 @@ public function testWithDefaultLocale(): void
{
$this->createContainer();

$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = $locale->withDefaultLanguage('ru');
$response = $locale->process($this->request, $this->handler);

Expand All @@ -268,7 +268,7 @@ public function testWithLocaleArgument(): void
$this->createContainer();

$request = $this->request->withUri($this->request->getUri()->withPath($path));
$locale = new Locale($this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = new Locale($this->localeRouteHelper, $this->translator, $this->urlGenerator, ['en', 'ru'], []);
$locale = $locale->withLocaleArgument('_lang');
$response = $locale->process($request, $this->handler);

Expand Down
3 changes: 3 additions & 0 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yii\Middleware\LocaleRouteHelper;
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Config\Config;
use Yiisoft\Config\ConfigPaths;
Expand All @@ -34,6 +35,7 @@

trait TestTrait
{
private LocaleRouteHelper $localeRouteHelper;
private RequestHandlerInterface $handler;
private ResponseFactoryInterface $responseFactory;
private ServerRequestInterface $request;
Expand All @@ -50,6 +52,7 @@ private function createContainer(): void
$container = new Container($containerConfig);

$this->handler = $container->get(RequestHandlerInterface::class);
$this->localeRouteHelper = $container->get(LocaleRouteHelper::class);
$this->request = $container->get(ServerRequestInterface::class);
$this->responseFactory = $container->get(ResponseFactoryInterface::class);
$this->translator = $container->get(TranslatorInterface::class);
Expand Down