diff --git a/src/EventSubscriber/LocaleRewriteSubscriber.php b/src/EventSubscriber/LocaleRewriteSubscriber.php index 8001e24..477bda6 100644 --- a/src/EventSubscriber/LocaleRewriteSubscriber.php +++ b/src/EventSubscriber/LocaleRewriteSubscriber.php @@ -68,7 +68,7 @@ private function getPreferredLanguage(Request $request): ?string private function routeExists(string $path): bool { - return !empty(array_filter((array)$this->routeCollection->getIterator(), function($routeObject) use ($path) { + return !empty(array_filter(iterator_to_array($this->routeCollection->getIterator()), function($routeObject) use ($path) { return $routeObject->getPath() === "/{" . $this->localeRouteParam . "}" . $path; })); } diff --git a/tests/Functional/DefaultTest.php b/tests/Functional/DefaultTest.php index 748f00a..5cbd453 100644 --- a/tests/Functional/DefaultTest.php +++ b/tests/Functional/DefaultTest.php @@ -4,6 +4,7 @@ use Braunstetter\LocalizedRoutes\Tests\Functional\app\src\TestKernel; use PHPUnit\Framework\TestCase; use Symfony\Bundle\FrameworkBundle\KernelBrowser; +use Symfony\Component\HttpFoundation\AcceptHeader; class DefaultTest extends TestCase { @@ -28,7 +29,7 @@ public function test_route_gets_redirected() $this->assertSame('http://localhost/en/test', $client->getRequest()->getUri()); } - public function test_route_gets_redirected_even_with_unsupported_locale() + public function test_route_gets_redirected_to_default_locale_when_locale_is_unsupported() { $kernel = new TestKernel(['/Resources/config/parameters-unsupported-locale.yaml']); $kernel->reboot($kernel->getCacheDir() . '/warmup'); @@ -36,9 +37,8 @@ public function test_route_gets_redirected_even_with_unsupported_locale() $client = new KernelBrowser($kernel); $client->followRedirects(); $client->request('GET', '/test-unsupported'); - $this->assertSame('http://localhost/en/test-unsupported', $client->getRequest()->getUri()); - $this->assertFalse($client->getResponse()->isSuccessful()); - $this->assertSame(404, $client->getResponse()->getStatusCode()); + $this->assertSame('http://localhost/es/test-unsupported', $client->getRequest()->getUri()); + $this->assertTrue($client->getResponse()->isSuccessful()); } public function test_home_fallback_works() { diff --git a/tests/Functional/app/src/Resources/config/framework.yaml b/tests/Functional/app/src/Resources/config/framework.yaml index 4498d02..73645c2 100644 --- a/tests/Functional/app/src/Resources/config/framework.yaml +++ b/tests/Functional/app/src/Resources/config/framework.yaml @@ -1,6 +1,6 @@ framework: secret: 'F00' - default_locale: 'en' + default_locale: es router: utf8: true resource: '%kernel.project_dir%/tests/Functional/app/src/Resources/config/routes.yaml' diff --git a/tests/Functional/app/src/Resources/config/parameters-unsupported-locale.yaml b/tests/Functional/app/src/Resources/config/parameters-unsupported-locale.yaml index be1d7c4..0e6633f 100644 --- a/tests/Functional/app/src/Resources/config/parameters-unsupported-locale.yaml +++ b/tests/Functional/app/src/Resources/config/parameters-unsupported-locale.yaml @@ -1,3 +1,3 @@ parameters: app_locales: fr|es - locale: en \ No newline at end of file + locale: es \ No newline at end of file diff --git a/tests/Unit/UnitTest.php b/tests/Unit/UnitTest.php new file mode 100644 index 0000000..7c8be14 --- /dev/null +++ b/tests/Unit/UnitTest.php @@ -0,0 +1,43 @@ +getMockBuilder(RouterInterface::class)->getMock(); + $routerMock->method('getRouteCollection')->willReturn(new RouteCollection()); + $parameterBagMock = $this->getMockBuilder(ParameterBagInterface::class)->getMock(); + $subscriber = new LocaleRewriteSubscriber($routerMock, $parameterBagMock); + + $this->assertFalse($this->invokeMethod($subscriber, 'isLocaleSupported', [null])); + } + + /** + * Call protected/private method of a class. + * + * @param object &$object Instantiated object that we will run method on. + * @param string $methodName Method names to call + * @param array $parameters Array of parameters to pass into method. + * + * @return mixed Method return. + * @throws ReflectionException + */ + public function invokeMethod(object $object, string $methodName, array $parameters = array()): mixed + { + $reflection = new \ReflectionClass(get_class($object)); + $method = $reflection->getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($object, $parameters); + } +} \ No newline at end of file diff --git a/tests/coverage.xml b/tests/coverage.xml index f8a2575..bb28f89 100644 --- a/tests/coverage.xml +++ b/tests/coverage.xml @@ -1,6 +1,6 @@ - - + + @@ -12,12 +12,12 @@ - + - - - - + + + + @@ -27,9 +27,9 @@ - - - + + + @@ -40,7 +40,7 @@ - + @@ -50,6 +50,6 @@ - +