diff --git a/src/Locale.php b/src/Locale.php index 1706673..75f346e 100644 --- a/src/Locale.php +++ b/src/Locale.php @@ -22,6 +22,7 @@ final class Locale implements MiddlewareInterface private string $localeArgument = '_language'; public function __construct( + private LocaleRouteHelper $localeRouteHelper, private readonly TranslatorInterface $translator, private readonly UrlGeneratorInterface $urlGenerator, private readonly array $languages = [], @@ -42,7 +43,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $path = $uri->getPath(); $language = $this->getLanguage($path); - new LocaleRouteHelper($path); + $this->localeRouteHelper->setPath($path); if ($this->shouldIgnoreUrl($request) === false) { $this->setLocale($language); diff --git a/src/LocaleRouteHelper.php b/src/LocaleRouteHelper.php index 5870334..d79a389 100644 --- a/src/LocaleRouteHelper.php +++ b/src/LocaleRouteHelper.php @@ -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; } /** diff --git a/tests/Locale/ImmutableTest.php b/tests/Locale/ImmutableTest.php index 9e0cc50..39d90f4 100644 --- a/tests/Locale/ImmutableTest.php +++ b/tests/Locale/ImmutableTest.php @@ -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')); } diff --git a/tests/Locale/LocaleRouteHelperTest.php b/tests/Locale/LocaleRouteHelperTest.php index 33ac792..305fe5e 100644 --- a/tests/Locale/LocaleRouteHelperTest.php +++ b/tests/Locale/LocaleRouteHelperTest.php @@ -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()); } } diff --git a/tests/Locale/MiddlewareTest.php b/tests/Locale/MiddlewareTest.php index c4f93f2..f809eed 100644 --- a/tests/Locale/MiddlewareTest.php +++ b/tests/Locale/MiddlewareTest.php @@ -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); @@ -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); @@ -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); @@ -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); @@ -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()); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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()); @@ -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()); @@ -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); @@ -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); @@ -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); @@ -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()); @@ -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()); @@ -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()); @@ -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); @@ -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); diff --git a/tests/Support/TestTrait.php b/tests/Support/TestTrait.php index b3d1c10..13cf27c 100644 --- a/tests/Support/TestTrait.php +++ b/tests/Support/TestTrait.php @@ -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; @@ -34,6 +35,7 @@ trait TestTrait { + private LocaleRouteHelper $localeRouteHelper; private RequestHandlerInterface $handler; private ResponseFactoryInterface $responseFactory; private ServerRequestInterface $request; @@ -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);