Skip to content

Commit

Permalink
Complete tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBrauner committed May 17, 2022
1 parent 076b270 commit 83be0ae
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/EventSubscriber/LocaleRewriteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Functional/DefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,17 +29,16 @@ 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');

$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() {
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/app/src/Resources/config/framework.yaml
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
parameters:
app_locales: fr|es
locale: en
locale: es
43 changes: 43 additions & 0 deletions tests/Unit/UnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Braunstetter\LocalizedRoutes\EventSubscriber\LocaleRewriteSubscriber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

class UnitTest extends TestCase
{

/**
* @throws ReflectionException
*/
public function test_is_locale_supported()
{
$routerMock = $this->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);
}
}
24 changes: 12 additions & 12 deletions tests/coverage.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1652730267">
<project timestamp="1652730267">
<coverage generated="1652799211">
<project timestamp="1652799211">
<file name="/Users/michaelbrauner/Bundles/Symfony/LocalizedRoutesBundle/src/DependencyInjection/LocalizedRoutesExtension.php">
<class name="Braunstetter\LocalizedRoutes\DependencyInjection\LocalizedRoutesExtension" namespace="global">
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="2" coveredstatements="2" elements="3" coveredelements="3"/>
Expand All @@ -12,12 +12,12 @@
</file>
<file name="/Users/michaelbrauner/Bundles/Symfony/LocalizedRoutesBundle/src/EventSubscriber/LocaleRewriteSubscriber.php">
<class name="Braunstetter\LocalizedRoutes\EventSubscriber\LocaleRewriteSubscriber" namespace="global">
<metrics complexity="11" methods="6" coveredmethods="5" conditionals="0" coveredconditionals="0" statements="20" coveredstatements="19" elements="26" coveredelements="24"/>
<metrics complexity="11" methods="6" coveredmethods="6" conditionals="0" coveredconditionals="0" statements="20" coveredstatements="20" elements="26" coveredelements="26"/>
</class>
<line num="23" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="3"/>
<line num="25" type="stmt" count="3"/>
<line num="26" type="stmt" count="3"/>
<line num="27" type="stmt" count="3"/>
<line num="23" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="4"/>
<line num="25" type="stmt" count="4"/>
<line num="26" type="stmt" count="4"/>
<line num="27" type="stmt" count="4"/>
<line num="30" type="method" name="onKernelRequest" visibility="public" complexity="3" crap="3" count="3"/>
<line num="32" type="stmt" count="3"/>
<line num="33" type="stmt" count="3"/>
Expand All @@ -27,9 +27,9 @@
<line num="42" type="stmt" count="2"/>
<line num="43" type="stmt" count="1"/>
<line num="46" type="stmt" count="2"/>
<line num="51" type="method" name="isLocaleSupported" visibility="private" complexity="3" crap="3.07" count="2"/>
<line num="53" type="stmt" count="2"/>
<line num="54" type="stmt" count="0"/>
<line num="51" type="method" name="isLocaleSupported" visibility="private" complexity="3" crap="3" count="3"/>
<line num="53" type="stmt" count="3"/>
<line num="54" type="stmt" count="1"/>
<line num="57" type="stmt" count="2"/>
<line num="58" type="stmt" count="2"/>
<line num="61" type="stmt" count="2"/>
Expand All @@ -40,7 +40,7 @@
<line num="72" type="stmt" count="3"/>
<line num="76" type="method" name="getSubscribedEvents" visibility="public" complexity="1" crap="1" count="2"/>
<line num="79" type="stmt" count="2"/>
<metrics loc="83" ncloc="81" classes="1" methods="6" coveredmethods="5" conditionals="0" coveredconditionals="0" statements="20" coveredstatements="19" elements="26" coveredelements="24"/>
<metrics loc="83" ncloc="81" classes="1" methods="6" coveredmethods="6" conditionals="0" coveredconditionals="0" statements="20" coveredstatements="20" elements="26" coveredelements="26"/>
</file>
<file name="/Users/michaelbrauner/Bundles/Symfony/LocalizedRoutesBundle/src/LocalizedRoutesBundle.php">
<class name="Braunstetter\LocalizedRoutes\LocalizedRoutesBundle" namespace="global">
Expand All @@ -50,6 +50,6 @@
<line num="12" type="stmt" count="1"/>
<metrics loc="14" ncloc="14" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="1" coveredstatements="1" elements="2" coveredelements="2"/>
</file>
<metrics files="3" loc="117" ncloc="111" classes="3" methods="8" coveredmethods="7" conditionals="0" coveredconditionals="0" statements="23" coveredstatements="22" elements="31" coveredelements="29"/>
<metrics files="3" loc="117" ncloc="111" classes="3" methods="8" coveredmethods="8" conditionals="0" coveredconditionals="0" statements="23" coveredstatements="23" elements="31" coveredelements="31"/>
</project>
</coverage>

0 comments on commit 83be0ae

Please sign in to comment.