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

Refactored the switcher extension to get all deps via injection #201

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion Resources/config/switcher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
</service>

<service id="lunetics_locale.twig.switcher" class="%lunetics_locale.twig.switcher.class%">
<argument type="service" id="service_container"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="router"/>
<argument type="service" id="lunetics_locale.allowed_locales_provider"/>
<argument type="service" id="lunetics_locale.switcher_helper"/>
<argument>%lunetics_locale.switcher.use_controller%</argument>
<argument>%lunetics_locale.switcher.show_current_locale%</argument>
<tag name="twig.extension"/>
</service>

Expand Down
1 change: 0 additions & 1 deletion Templating/Helper/LocaleSwitchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function __construct(EngineInterface $templating, $template)
}

/**
*
* @param array $viewParams
*/
public function renderSwitch(array $viewParams = array(), $template = null)
Expand Down
6 changes: 4 additions & 2 deletions Tests/Controller/LocaleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

use Lunetics\LocaleBundle\Controller\LocaleController;
use Symfony\Component\Routing\RouterInterface;
use Lunetics\LocaleBundle\Validator\MetaValidator;

class LocaleControllerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -95,7 +97,7 @@ private function getRequestWithBrowserPreferences($route = "/")

private function getRouterMock()
{
$routerMock = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor()->getMock();
$routerMock = $this->createMock(RouterInterface::class);
$routerMock->expects($this->any())
->method('generate')
->with($this->equalTo('fallback_route'), $this->anything())
Expand All @@ -106,7 +108,7 @@ private function getRouterMock()

private function getMetaValidatorMock($expectTrue = true)
{
$metaValidator = $this->getMockBuilder('Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock();
$metaValidator = $this->createMock(MetaValidator::class);
if ($expectTrue) {
$metaValidator->expects($this->atLeastOnce())
->method('isAllowed')
Expand Down
49 changes: 16 additions & 33 deletions Tests/EventListener/LocaleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
use Symfony\Component\HttpKernel\KernelEvents;
use Lunetics\LocaleBundle\Matcher\DefaultBestLocaleMatcher;
use Lunetics\LocaleBundle\LocaleInformation\AllowedLocalesProvider;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;

class LocaleListenerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -147,7 +151,7 @@ public function testThatGuesserIsNotCalledIfNotInGuessingOrder()

public function testDispatcherIsFired()
{
$dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
$dispatcherMock = $this->createMock(EventDispatcher::class);
$dispatcherMock->expects($this->once())
->method('dispatch')
->with($this->equalTo(LocaleBundleEvents::onLocaleChange), $this->isInstanceOf('Lunetics\LocaleBundle\Event\FilterLocaleSwitchEvent'));
Expand All @@ -162,7 +166,7 @@ public function testDispatcherIsFired()

public function testDispatcherIsNotFired()
{
$dispatcherMock = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
$dispatcherMock = $this->createMock(EventDispatcher::class);
$dispatcherMock->expects($this->never())
->method('dispatch');

Expand Down Expand Up @@ -306,7 +310,11 @@ public function testGetSubscribedEvents()

private function getEvent(Request $request)
{
return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
return new GetResponseEvent(
$this->createMock(HttpKernelInterface::class),
$request,
HttpKernelInterface::MASTER_REQUEST
);
}

private function getListener($locale = 'en', $manager = null, $logger = null, $matcher = null)
Expand Down Expand Up @@ -352,31 +360,15 @@ private function getGuesserManager($order = array(1 => 'router', 2 => 'browser')

private function getMockGuesserManager()
{
return $this
->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager')
->disableOriginalConstructor()
->getMock()
;
}

/**
* @return LocaleGuesserInterface
*/
private function getMockGuesser()
{
$mock = $this->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(LocaleGuesserManager::class);
}

/**
* @return MetaValidator
*/
private function getMetaValidatorMock()
{
$mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(MetaValidator::class);
}

private function getRequestWithRouterParam($routerLocale = 'es')
Expand Down Expand Up @@ -411,27 +403,18 @@ private function getEmptyRequest()
return $request;
}

private function getMockRequest()
{
return $this->getMock('Symfony\Component\HttpFoundation\Request');
}

private function getMockResponse()
{
return $this->getMock('Symfony\Component\HttpFoundation\Response');
return $this->createMock(Response::class);
}

private function getMockFilterResponseEvent()
{
return $this
->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')
->disableOriginalConstructor()
->getMock()
;
return $this->createMock(FilterResponseEvent::class);
}

private function getMockLogger()
{
return $this->getMock('Psr\Log\LoggerInterface');
return $this->createMock(LoggerInterface::class);
}
}
10 changes: 8 additions & 2 deletions Tests/EventListener/LocaleUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Lunetics\LocaleBundle\EventListener\LocaleUpdateListener;
use Lunetics\LocaleBundle\Session\LocaleSession;
use Lunetics\LocaleBundle\Cookie\LocaleCookie;
use Psr\Log\LoggerInterface;

class LocaleUpdateTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -179,7 +180,12 @@ private function getLocaleUpdateListener($registeredGuessers = array(), $updateC

private function getEvent(Request $request)
{
return new FilterResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST, new Response);
return new FilterResponseEvent(
$this->createMock(HttpKernelInterface::class),
$request,
HttpKernelInterface::MASTER_REQUEST,
new Response
);
}


Expand All @@ -199,6 +205,6 @@ private function getRequest($withCookieSet = false)

private function getMockLogger()
{
return $this->getMock('Psr\Log\LoggerInterface');
return $this->createMock(LoggerInterface::class);
}
}
3 changes: 2 additions & 1 deletion Tests/Form/Extension/ChoiceList/LocaleChoiceListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Lunetics\LocaleBundle\Tests\Form\Extension\ChoiceList;

use Lunetics\LocaleBundle\Form\Extension\ChoiceList\LocaleChoiceList;
use Lunetics\LocaleBundle\LocaleInformation\LocaleInformation;

/**
* Test for the LocaleInformation
Expand Down Expand Up @@ -104,6 +105,6 @@ public function testPreferredLocalesSorted()

public function getLocaleInformation()
{
return $this->getMockBuilder('Lunetics\LocaleBundle\LocaleInformation\LocaleInformation')->disableOriginalConstructor()->getMock();
return $this->createMock(LocaleInformation::class);
}
}
10 changes: 4 additions & 6 deletions Tests/Form/Extension/Type/LocaleTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Lunetics\LocaleBundle\Tests\Form\Extension\Type;

use Lunetics\LocaleBundle\Form\Extension\Type\LocaleType;
use Lunetics\LocaleBundle\Form\Extension\ChoiceList\LocaleChoiceList;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Kevin Archer <[email protected]>
Expand Down Expand Up @@ -46,15 +48,11 @@ public function testGetName()

protected function getMockLocaleChoiceList()
{
return $this
->getMockBuilder('Lunetics\LocaleBundle\Form\Extension\ChoiceList\LocaleChoiceList')
->disableOriginalConstructor()
->getMock()
;
return $this->createMock(LocaleChoiceList::class);
}

protected function getMockOptionsResolverInterface()
{
return $this->getMock('Symfony\Component\OptionsResolver\OptionsResolver');
return $this->createMock(OptionsResolver::class);
}
}
11 changes: 5 additions & 6 deletions Tests/LocaleGuesser/BrowserLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Lunetics\LocaleBundle\LocaleGuesser\BrowserLocaleGuesser;
use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface;
use Symfony\Component\HttpFoundation\Request;
use Lunetics\LocaleBundle\Validator\MetaValidator;

class BrowserLocaleGuesserTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -116,7 +117,7 @@ public function testEnsureCorrectLocaleForAllowedLocales($allowedLocales, $resul
{
$metaValidator = $this->getMetaValidatorMock();
$request = $this->getRequestWithBrowserPreferencesMultipleLangLocales();
$guesser = $this->getGuesser($metaValidator, $allowedLocales);
$guesser = $this->getGuesser($metaValidator);

// Emulate a simple validator for strict mode
$metaValidator->expects($this->atLeastOnce())
Expand All @@ -138,9 +139,9 @@ public function testEnsureCorrectLocaleForAllowedLocales($allowedLocales, $resul
$this->assertEquals($result, $guesser->getIdentifiedLocale());
}

private function getGuesser($metaValidator, $allowedLocales = array('en', 'fr', 'de'))
private function getGuesser($metaValidator)
{
$guesser = new BrowserLocaleGuesser($metaValidator, $allowedLocales);
$guesser = new BrowserLocaleGuesser($metaValidator);

return $guesser;
}
Expand All @@ -163,8 +164,6 @@ private function getRequestWithBrowserPreferencesMultipleLangLocales()

private function getMetaValidatorMock()
{
$mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(MetaValidator::class);
}
}
5 changes: 2 additions & 3 deletions Tests/LocaleGuesser/CookieLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Lunetics\LocaleBundle\LocaleGuesser\CookieLocaleGuesser;
use Symfony\Component\HttpFoundation\Request;
use Lunetics\LocaleBundle\Validator\MetaValidator;

class CookieLocaleGuesserTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -73,8 +74,6 @@ private function getRequest($withLocaleCookie = true)

public function getMetaValidatorMock()
{
$mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(MetaValidator::class);
}
}
17 changes: 7 additions & 10 deletions Tests/LocaleGuesser/DomainLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
namespace Lunetics\LocaleBundle\Tests\LocaleGuesser;

use Lunetics\LocaleBundle\LocaleGuesser\DomainLocaleGuesser;
use Symfony\Component\HttpFoundation\Request;
use Lunetics\LocaleBundle\Validator\MetaValidator;
use Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap;

/**
* @author Jachim Coudenys <[email protected]>
Expand Down Expand Up @@ -67,22 +70,16 @@ public function dataDomains()

private function getMockDomainLocaleMap()
{
return $this
->getMockBuilder('\Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap')
->disableOriginalConstructor()
->getMock();
return $this->createMock(DomainLocaleMap::class);
}

private function getMockMetaValidator()
{
return $this
->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')
->disableOriginalConstructor()
->getMock();
return $this->createMock(MetaValidator::class);
}

private function getMockRequest()
{
return $this->getMock('Symfony\Component\HttpFoundation\Request');
return $this->createMock(Request::class);
}
}
}
14 changes: 6 additions & 8 deletions Tests/LocaleGuesser/LocaleGuesserManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager;
use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface;
use Lunetics\LocaleBundle\Validator\MetaValidator;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

class LocaleGuesserManagerTest extends \PHPUnit_Framework_TestCase
{
public function testLocaleGuessingInvalidGuesser()
{
$this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException');
$this->expectException(InvalidConfigurationException::class);
$guesserManager = new LocaleGuesserManager(array(0 => 'foo'));
$guesserManager->addGuesser($this->getGuesserMock(), 'bar');
$guesserManager->runLocaleGuessing($this->getRequestWithoutLocaleQuery());
Expand Down Expand Up @@ -137,24 +139,20 @@ private function getRequestWithoutLocaleQuery()
*/
private function getGuesserMock()
{
$mock = $this->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(LocaleGuesserInterface::class);
}

/**
* @return MetaValidator
*/
private function getMetaValidatorMock()
{
$mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(MetaValidator::class);
}

private function getMockLogger()
{
return $this->getMock('Psr\Log\LoggerInterface');
return $this->createMock(LoggerInterface::class);
}

}
13 changes: 2 additions & 11 deletions Tests/LocaleGuesser/QueryLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Lunetics\LocaleBundle\LocaleGuesser\QueryLocaleGuesser;
use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface;
use Symfony\Component\HttpFoundation\Request;
use Lunetics\LocaleBundle\Validator\MetaValidator;

class QueryLocaleGuesserTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -51,14 +52,6 @@ public function testLocaleIsNotIdentifiedFromRequestQuery()
$this->assertFalse($guesser->getIdentifiedLocale());
}

private function getRequestWithLocaleParameter($locale = 'en')
{
$request = Request::create('/hello-world', 'GET');
$request->attributes->set('_locale', $locale);

return $request;
}

private function getRequestWithLocaleQuery($locale = 'en')
{
$request = Request::create('/hello-world', 'GET');
Expand All @@ -69,8 +62,6 @@ private function getRequestWithLocaleQuery($locale = 'en')

public function getMetaValidatorMock()
{
$mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock();

return $mock;
return $this->createMock(MetaValidator::class);
}
}
Loading