From 0b18763abefe5fb3b88030a4c22fde6eef9201cc Mon Sep 17 00:00:00 2001 From: thanosp Date: Wed, 6 Dec 2017 12:56:40 +0100 Subject: [PATCH 1/2] Refactored the switcher extension to get all deps via injection --- Resources/config/switcher.xml | 7 +- Templating/Helper/LocaleSwitchHelper.php | 1 - Tests/EventListener/LocaleListenerTest.php | 15 ++- Tests/EventListener/LocaleUpdateTest.php | 9 +- Tests/Form/Extension/Type/LocaleTypeTest.php | 2 +- .../LocaleGuesser/DomainLocaleGuesserTest.php | 4 +- .../LocaleGuesserManagerTest.php | 12 +- .../SubdomainLocaleGuesserTest.php | 4 +- .../TopleveldomainLocaleGuesserTest.php | 4 +- Tests/LuneticsLocaleBundleTest.php | 2 +- Tests/Session/LocaleSessionTest.php | 4 +- .../Helper/LocaleSwitchHelperTest.php | 2 +- .../Extension/LocaleSwitcherExtensionTest.php | 107 +++++++++++------- .../Validator/LocaleAllowedValidatorTest.php | 4 +- Tests/Validator/LocaleValidatorTest.php | 2 +- Twig/Extension/LocaleSwitcherExtension.php | 102 ++++++++++++----- 16 files changed, 181 insertions(+), 100 deletions(-) diff --git a/Resources/config/switcher.xml b/Resources/config/switcher.xml index 3c637f1..d7e19c2 100644 --- a/Resources/config/switcher.xml +++ b/Resources/config/switcher.xml @@ -18,7 +18,12 @@ - + + + + + %lunetics_locale.switcher.use_controller% + %lunetics_locale.switcher.show_current_locale% diff --git a/Templating/Helper/LocaleSwitchHelper.php b/Templating/Helper/LocaleSwitchHelper.php index 229b74f..bebb1c8 100644 --- a/Templating/Helper/LocaleSwitchHelper.php +++ b/Templating/Helper/LocaleSwitchHelper.php @@ -40,7 +40,6 @@ public function __construct(EngineInterface $templating, $template) } /** - * * @param array $viewParams */ public function renderSwitch(array $viewParams = array(), $template = null) diff --git a/Tests/EventListener/LocaleListenerTest.php b/Tests/EventListener/LocaleListenerTest.php index 2547313..d3ceba8 100644 --- a/Tests/EventListener/LocaleListenerTest.php +++ b/Tests/EventListener/LocaleListenerTest.php @@ -306,7 +306,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('Symfony\Component\HttpKernel\HttpKernelInterface'), + $request, + HttpKernelInterface::MASTER_REQUEST + ); } private function getListener($locale = 'en', $manager = null, $logger = null, $matcher = null) @@ -411,14 +415,9 @@ 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('Symfony\Component\HttpFoundation\Response'); } private function getMockFilterResponseEvent() @@ -432,6 +431,6 @@ private function getMockFilterResponseEvent() private function getMockLogger() { - return $this->getMock('Psr\Log\LoggerInterface'); + return $this->createMock('Psr\Log\LoggerInterface'); } } diff --git a/Tests/EventListener/LocaleUpdateTest.php b/Tests/EventListener/LocaleUpdateTest.php index 47e89ad..a815d55 100644 --- a/Tests/EventListener/LocaleUpdateTest.php +++ b/Tests/EventListener/LocaleUpdateTest.php @@ -179,7 +179,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('Symfony\Component\HttpKernel\HttpKernelInterface'), + $request, + HttpKernelInterface::MASTER_REQUEST, + new Response + ); } @@ -199,6 +204,6 @@ private function getRequest($withCookieSet = false) private function getMockLogger() { - return $this->getMock('Psr\Log\LoggerInterface'); + return $this->createMock('Psr\Log\LoggerInterface'); } } diff --git a/Tests/Form/Extension/Type/LocaleTypeTest.php b/Tests/Form/Extension/Type/LocaleTypeTest.php index bedb95a..a7b59cc 100644 --- a/Tests/Form/Extension/Type/LocaleTypeTest.php +++ b/Tests/Form/Extension/Type/LocaleTypeTest.php @@ -55,6 +55,6 @@ protected function getMockLocaleChoiceList() protected function getMockOptionsResolverInterface() { - return $this->getMock('Symfony\Component\OptionsResolver\OptionsResolver'); + return $this->createMock('Symfony\Component\OptionsResolver\OptionsResolver'); } } diff --git a/Tests/LocaleGuesser/DomainLocaleGuesserTest.php b/Tests/LocaleGuesser/DomainLocaleGuesserTest.php index 46c76fe..da2cfb5 100644 --- a/Tests/LocaleGuesser/DomainLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/DomainLocaleGuesserTest.php @@ -83,6 +83,6 @@ private function getMockMetaValidator() private function getMockRequest() { - return $this->getMock('Symfony\Component\HttpFoundation\Request'); + return $this->createMock('Symfony\Component\HttpFoundation\Request'); } -} +} diff --git a/Tests/LocaleGuesser/LocaleGuesserManagerTest.php b/Tests/LocaleGuesser/LocaleGuesserManagerTest.php index ad71e45..e47f39a 100644 --- a/Tests/LocaleGuesser/LocaleGuesserManagerTest.php +++ b/Tests/LocaleGuesser/LocaleGuesserManagerTest.php @@ -21,7 +21,7 @@ class LocaleGuesserManagerTest extends \PHPUnit_Framework_TestCase { public function testLocaleGuessingInvalidGuesser() { - $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException'); + $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException'); $guesserManager = new LocaleGuesserManager(array(0 => 'foo')); $guesserManager->addGuesser($this->getGuesserMock(), 'bar'); $guesserManager->runLocaleGuessing($this->getRequestWithoutLocaleQuery()); @@ -137,7 +137,9 @@ private function getRequestWithoutLocaleQuery() */ private function getGuesserMock() { - $mock = $this->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface')->disableOriginalConstructor()->getMock(); + $mock = $this->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface') + ->disableOriginalConstructor() + ->getMock(); return $mock; } @@ -147,14 +149,16 @@ private function getGuesserMock() */ private function getMetaValidatorMock() { - $mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock(); + $mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator') + ->disableOriginalConstructor() + ->getMock(); return $mock; } private function getMockLogger() { - return $this->getMock('Psr\Log\LoggerInterface'); + return $this->createMock('Psr\Log\LoggerInterface'); } } diff --git a/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php b/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php index 4dca957..2dd7834 100644 --- a/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php @@ -59,7 +59,7 @@ public function dataDomains() array(false, 'de-DE.domain', false, '_'), ); } - + private function getMockMetaValidator() { return $this @@ -71,6 +71,6 @@ private function getMockMetaValidator() private function getMockRequest() { - return $this->getMock('Symfony\Component\HttpFoundation\Request'); + return $this->createMock('Symfony\Component\HttpFoundation\Request'); } } diff --git a/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php b/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php index 3aa7685..d6a85b6 100644 --- a/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php @@ -84,6 +84,6 @@ private function getMockMetaValidator() private function getMockRequest() { - return $this->getMock('Symfony\Component\HttpFoundation\Request'); + return $this->createMock('Symfony\Component\HttpFoundation\Request'); } -} \ No newline at end of file +} diff --git a/Tests/LuneticsLocaleBundleTest.php b/Tests/LuneticsLocaleBundleTest.php index 922ff88..89052cb 100644 --- a/Tests/LuneticsLocaleBundleTest.php +++ b/Tests/LuneticsLocaleBundleTest.php @@ -40,6 +40,6 @@ public function testBuild() protected function getMockContainer() { - return $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + return $this->createMock('Symfony\Component\DependencyInjection\ContainerBuilder'); } } diff --git a/Tests/Session/LocaleSessionTest.php b/Tests/Session/LocaleSessionTest.php index 81da578..a063c49 100644 --- a/Tests/Session/LocaleSessionTest.php +++ b/Tests/Session/LocaleSessionTest.php @@ -74,6 +74,6 @@ public function testGetSessionVar() public function getMockSession() { - return $this->getMock('Symfony\Component\HttpFoundation\Session\Session'); + return $this->createMock('Symfony\Component\HttpFoundation\Session\Session'); } -} \ No newline at end of file +} diff --git a/Tests/Templating/Helper/LocaleSwitchHelperTest.php b/Tests/Templating/Helper/LocaleSwitchHelperTest.php index cc213c8..5d0c58f 100644 --- a/Tests/Templating/Helper/LocaleSwitchHelperTest.php +++ b/Tests/Templating/Helper/LocaleSwitchHelperTest.php @@ -44,6 +44,6 @@ public function testGetName() protected function getMockEngineInterface() { - return $this->getMock('Symfony\Component\Templating\EngineInterface'); + return $this->createMock('Symfony\Component\Templating\EngineInterface'); } } diff --git a/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php b/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php index 80ad63c..c883e4e 100644 --- a/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php +++ b/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php @@ -12,22 +12,61 @@ namespace Lunetics\LocaleBundle\Tests\Twig\Extension; use Lunetics\LocaleBundle\LocaleInformation\AllowedLocalesProvider; +use Lunetics\LocaleBundle\Templating\Helper\LocaleSwitchHelper; use Lunetics\LocaleBundle\Twig\Extension\LocaleSwitcherExtension; -use Symfony\Component\DependencyInjection\ContainerBuilder; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use PHPUnit_Framework_TestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\HttpFoundation\ParameterBag; /** * @covers \Lunetics\LocaleBundle\Twig\Extension\LocaleSwitcherExtension * * @author Kevin Archer */ -class LocaleSwitcherExtensionTest extends \PHPUnit_Framework_TestCase +class LocaleSwitcherExtensionTest extends PHPUnit_Framework_TestCase { - public function testGetFunctions() + /** @var RequestStack|MockObject */ + private $mockRequestStack; + + /** @var RouterInterface|MockObject */ + private $mockRouter; + + /** @var AllowedLocalesProvider|MockObject */ + private $mockLocalesProvider; + + /** @var LocaleSwitchHelper|MockObject */ + private $mockSwitcherHelper; + + /** @var bool */ + private $showCurrentLocaleParam; + + /** @var bool */ + private $useControllerParam; + + /** @var LocaleSwitcherExtension */ + private $extension; + + /** + * {@inheritDoc} + */ + protected function setUp() { - $container = new ContainerBuilder(); - $extension = new LocaleSwitcherExtension($container); + $this->mockRequestStack = $this->createMock(RequestStack::class); + $this->mockRouter = $this->createMock(RouterInterface::class); + $this->mockLocalesProvider = $this->createMock(AllowedLocalesProvider::class); + $this->mockSwitcherHelper = $this->createMock(LocaleSwitchHelper::class); + $this->showCurrentLocaleParam = true; + $this->useControllerParam = true; + + $this->constructExtension(); + } - $functions = $extension->getFunctions(); + public function testGetFunctions() + { + $functions = $this->extension->getFunctions(); /** @var \Twig_SimpleFunction $twigExtension */ $twigExtension = current($functions); @@ -40,18 +79,13 @@ public function testGetFunctions() public function testGetName() { - $container = new ContainerBuilder(); - $extension = new LocaleSwitcherExtension($container); - - $this->assertEquals('locale_switcher', $extension->getName()); + $this->assertEquals('locale_switcher', $this->extension->getName()); } public function testRenderSwitcher() { $template = uniqid('template:'); - $router = $this->getMockRouter(); - $request = $this->getMockRequest(); $request->attributes = $this->getMockParameterBag(); @@ -62,54 +96,43 @@ public function testRenderSwitcher() ->will($this->returnValue(array())) ; + $this->mockRequestStack->expects($this->once()) + ->method('getMasterRequest') + ->will($this->returnValue($request)); + $request->query = $query; - $switcherHelper = $this->getMockSwitcherHelper(); - $switcherHelper + $this->mockSwitcherHelper ->expects($this->once()) ->method('renderSwitch') ->will($this->returnValue($template)) ; - $container = new ContainerBuilder(); - $container->setParameter('lunetics_locale.switcher.show_current_locale', true); - $container->setParameter('lunetics_locale.switcher.use_controller', true); - $container->set('lunetics_locale.allowed_locales_provider', new AllowedLocalesProvider(array('en', 'fr'))); - $container->set('request', $request); - $container->set('router', $router); - - $container->set('lunetics_locale.switcher_helper', $switcherHelper); - - $extension = new LocaleSwitcherExtension($container); + $this->mockLocalesProvider->expects($this->once()) + ->method('getAllowedLocales')->will($this->returnValue(array('en', 'fr'))); - $this->assertEquals($template, $extension->renderSwitcher()); + $this->assertEquals($template, $this->extension->renderSwitcher()); } protected function getMockRequest() { - return $this->getMock('Symfony\Component\HttpFoundation\Request'); - } - - protected function getMockRouter() - { - return $this - ->getMockBuilder('Symfony\Component\Routing\Router') - ->disableOriginalConstructor() - ->getMock() - ; + return $this->createMock(Request::class); } protected function getMockParameterBag() { - return $this->getMock('Symfony\Component\HttpFoundation\ParameterBag'); + return $this->createMock(ParameterBag::class); } - protected function getMockSwitcherHelper() + private function constructExtension() { - return $this - ->getMockBuilder('Lunetics\LocaleBundle\Templating\Helper\LocaleSwitchHelper') - ->disableOriginalConstructor() - ->getMock() - ; + $this->extension = new LocaleSwitcherExtension( + $this->mockRequestStack, + $this->mockRouter, + $this->mockLocalesProvider, + $this->mockSwitcherHelper, + $this->showCurrentLocaleParam, + $this->useControllerParam + ); } } diff --git a/Tests/Validator/LocaleAllowedValidatorTest.php b/Tests/Validator/LocaleAllowedValidatorTest.php index f1a5bb9..525383b 100644 --- a/Tests/Validator/LocaleAllowedValidatorTest.php +++ b/Tests/Validator/LocaleAllowedValidatorTest.php @@ -123,9 +123,9 @@ public function testValidateEmptyLocale() $validator->validate(null, $this->getMockConstraint()); $validator->validate('', $this->getMockConstraint()); } - + protected function getMockConstraint() { - return $this->getMock('Symfony\Component\Validator\Constraint'); + return $this->createMock('Symfony\Component\Validator\Constraint'); } } diff --git a/Tests/Validator/LocaleValidatorTest.php b/Tests/Validator/LocaleValidatorTest.php index f186441..ca38ab6 100644 --- a/Tests/Validator/LocaleValidatorTest.php +++ b/Tests/Validator/LocaleValidatorTest.php @@ -134,6 +134,6 @@ public function testValidateEmptyLocale() protected function getMockConstraint() { - return $this->getMock('Symfony\Component\Validator\Constraint'); + return $this->createMock('Symfony\Component\Validator\Constraint'); } } diff --git a/Twig/Extension/LocaleSwitcherExtension.php b/Twig/Extension/LocaleSwitcherExtension.php index a474130..3ce39f4 100644 --- a/Twig/Extension/LocaleSwitcherExtension.php +++ b/Twig/Extension/LocaleSwitcherExtension.php @@ -11,8 +11,12 @@ namespace Lunetics\LocaleBundle\Twig\Extension; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Lunetics\LocaleBundle\LocaleInformation\AllowedLocalesProvider; use Lunetics\LocaleBundle\Switcher\TargetInformationBuilder; +use Lunetics\LocaleBundle\Templating\Helper\LocaleSwitchHelper; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Routing\RouterInterface; +use Twig_SimpleFunction; /** * @author Christophe Willemsen @@ -21,27 +25,70 @@ class LocaleSwitcherExtension extends \Twig_Extension { /** - * @var ContainerInterface + * @var RequestStack */ - protected $container; + private $requestStack; /** - * Constructor. - * - * @param ContainerInterface $container + * @var RouterInterface */ - public function __construct(ContainerInterface $container) - { - $this->container = $container; + private $router; + + /** + * @var AllowedLocalesProvider + */ + private $allowedLocalesProvider; + + /** + * @var LocaleSwitchHelper + */ + private $switcherHelper; + + /** + * @var bool + */ + private $useControllerParam; + + /** + * @var bool + */ + private $showCurrentLocaleParam; + + /** + * @param RequestStack $requestStack + * @param RouterInterface $router + * @param AllowedLocalesProvider $allowedLocalesProvider + * @param LocaleSwitchHelper $switcherHelper + * @param bool $useControllerParam + * @param bool $showCurrentLocaleParam + */ + public function __construct( + RequestStack $requestStack, + RouterInterface $router, + AllowedLocalesProvider $allowedLocalesProvider, + LocaleSwitchHelper $switcherHelper, + $useControllerParam, + $showCurrentLocaleParam + ) { + $this->requestStack = $requestStack; + $this->router = $router; + $this->allowedLocalesProvider = $allowedLocalesProvider; + $this->useControllerParam = $useControllerParam; + $this->showCurrentLocaleParam = $showCurrentLocaleParam; + $this->switcherHelper = $switcherHelper; } /** - * @return array The added functions + * {@inheritDoc} */ public function getFunctions() { return array( - new \Twig_SimpleFunction('locale_switcher', array($this, 'renderSwitcher'), array('is_safe' => array('html'))), + new Twig_SimpleFunction( + 'locale_switcher', + array($this, 'renderSwitcher'), + array('is_safe' => array('html')) + ), ); } @@ -58,25 +105,24 @@ public function getName() * @param array $parameters * @param string $template * - * @return mixed + * @return string */ public function renderSwitcher($route = null, $parameters = array(), $template = null) { - $showCurrentLocale = $this->container->getParameter('lunetics_locale.switcher.show_current_locale'); - $useController = $this->container->getParameter('lunetics_locale.switcher.use_controller'); - $allowedLocales = $this->container->get('lunetics_locale.allowed_locales_provider')->getAllowedLocales(); - // Use the request stack if it exists (Symfony 2.4+); otherwise use the request "service" - // @see http://symfony.com/blog/new-in-symfony-2-4-the-request-stack - $request = $this->container->has('request_stack') - ? $this->container->get('request_stack')->getMasterRequest() - : $this->container->get('request') - ; - $router = $this->container->get('router'); - - $infosBuilder = new TargetInformationBuilder($request, $router, $allowedLocales, $showCurrentLocale, $useController); - - $infos = $infosBuilder->getTargetInformations($route, $parameters); - - return $this->container->get('lunetics_locale.switcher_helper')->renderSwitch($infos, $template); + $showCurrentLocale = $this->showCurrentLocaleParam; + $useController = $this->useControllerParam; + $allowedLocales = $this->allowedLocalesProvider->getAllowedLocales(); + + $informationBuilder = new TargetInformationBuilder( + $this->requestStack->getMasterRequest(), + $this->router, + $allowedLocales, + $showCurrentLocale, + $useController + ); + + $viewParams = $informationBuilder->getTargetInformations($route, $parameters); + + return $this->switcherHelper->renderSwitch($viewParams, $template); } } From 5852268d5d1083c74825954c65e8f2228de5fc38 Mon Sep 17 00:00:00 2001 From: thanosp Date: Wed, 6 Dec 2017 15:14:48 +0100 Subject: [PATCH 2/2] PR feedback. Replaced class strings with ::class calls where appropriate Replaced mockBuilder calls with createMock --- Tests/Controller/LocaleControllerTest.php | 6 ++- Tests/EventListener/LocaleListenerTest.php | 40 ++++++------------- Tests/EventListener/LocaleUpdateTest.php | 5 ++- .../ChoiceList/LocaleChoiceListTest.php | 3 +- Tests/Form/Extension/Type/LocaleTypeTest.php | 10 ++--- .../BrowserLocaleGuesserTest.php | 11 +++-- .../LocaleGuesser/CookieLocaleGuesserTest.php | 5 +-- .../LocaleGuesser/DomainLocaleGuesserTest.php | 15 +++---- .../LocaleGuesserManagerTest.php | 18 +++------ .../LocaleGuesser/QueryLocaleGuesserTest.php | 13 +----- .../LocaleGuesser/RouterLocaleGuesserTest.php | 11 +++-- .../SessionLocaleGuesserTest.php | 5 +-- .../SubdomainLocaleGuesserTest.php | 10 ++--- .../TopleveldomainLocaleGuesserTest.php | 15 +++---- .../LocaleInformationTest.php | 12 ++++-- Tests/LuneticsLocaleBundleTest.php | 3 +- Tests/Session/LocaleSessionTest.php | 3 +- .../Switcher/TargetInformationBuilderTest.php | 2 +- .../Helper/LocaleSwitchHelperTest.php | 3 +- .../Extension/LocaleSwitcherExtensionTest.php | 23 +++++------ Tests/Validator/BaseMetaValidator.php | 4 +- Tests/Validator/LocaleValidatorTest.php | 3 +- Twig/Extension/LocaleSwitcherExtension.php | 10 ++--- 23 files changed, 96 insertions(+), 134 deletions(-) diff --git a/Tests/Controller/LocaleControllerTest.php b/Tests/Controller/LocaleControllerTest.php index ce0688a..c09636c 100644 --- a/Tests/Controller/LocaleControllerTest.php +++ b/Tests/Controller/LocaleControllerTest.php @@ -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 { @@ -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()) @@ -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') diff --git a/Tests/EventListener/LocaleListenerTest.php b/Tests/EventListener/LocaleListenerTest.php index d3ceba8..860d6d1 100644 --- a/Tests/EventListener/LocaleListenerTest.php +++ b/Tests/EventListener/LocaleListenerTest.php @@ -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 { @@ -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')); @@ -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'); @@ -307,7 +311,7 @@ public function testGetSubscribedEvents() private function getEvent(Request $request) { return new GetResponseEvent( - $this->createMock('Symfony\Component\HttpKernel\HttpKernelInterface'), + $this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST ); @@ -356,21 +360,7 @@ 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); } /** @@ -378,9 +368,7 @@ private function getMockGuesser() */ private function getMetaValidatorMock() { - $mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock(); - - return $mock; + return $this->createMock(MetaValidator::class); } private function getRequestWithRouterParam($routerLocale = 'es') @@ -417,20 +405,16 @@ private function getEmptyRequest() private function getMockResponse() { - return $this->createMock('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->createMock('Psr\Log\LoggerInterface'); + return $this->createMock(LoggerInterface::class); } } diff --git a/Tests/EventListener/LocaleUpdateTest.php b/Tests/EventListener/LocaleUpdateTest.php index a815d55..ae3416d 100644 --- a/Tests/EventListener/LocaleUpdateTest.php +++ b/Tests/EventListener/LocaleUpdateTest.php @@ -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 { @@ -180,7 +181,7 @@ private function getLocaleUpdateListener($registeredGuessers = array(), $updateC private function getEvent(Request $request) { return new FilterResponseEvent( - $this->createMock('Symfony\Component\HttpKernel\HttpKernelInterface'), + $this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST, new Response @@ -204,6 +205,6 @@ private function getRequest($withCookieSet = false) private function getMockLogger() { - return $this->createMock('Psr\Log\LoggerInterface'); + return $this->createMock(LoggerInterface::class); } } diff --git a/Tests/Form/Extension/ChoiceList/LocaleChoiceListTest.php b/Tests/Form/Extension/ChoiceList/LocaleChoiceListTest.php index 507fedf..11c68ab 100644 --- a/Tests/Form/Extension/ChoiceList/LocaleChoiceListTest.php +++ b/Tests/Form/Extension/ChoiceList/LocaleChoiceListTest.php @@ -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 @@ -104,6 +105,6 @@ public function testPreferredLocalesSorted() public function getLocaleInformation() { - return $this->getMockBuilder('Lunetics\LocaleBundle\LocaleInformation\LocaleInformation')->disableOriginalConstructor()->getMock(); + return $this->createMock(LocaleInformation::class); } } diff --git a/Tests/Form/Extension/Type/LocaleTypeTest.php b/Tests/Form/Extension/Type/LocaleTypeTest.php index a7b59cc..cfaf29a 100644 --- a/Tests/Form/Extension/Type/LocaleTypeTest.php +++ b/Tests/Form/Extension/Type/LocaleTypeTest.php @@ -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 @@ -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->createMock('Symfony\Component\OptionsResolver\OptionsResolver'); + return $this->createMock(OptionsResolver::class); } } diff --git a/Tests/LocaleGuesser/BrowserLocaleGuesserTest.php b/Tests/LocaleGuesser/BrowserLocaleGuesserTest.php index 00f65ee..8bc1d23 100644 --- a/Tests/LocaleGuesser/BrowserLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/BrowserLocaleGuesserTest.php @@ -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 { @@ -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()) @@ -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; } @@ -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); } } diff --git a/Tests/LocaleGuesser/CookieLocaleGuesserTest.php b/Tests/LocaleGuesser/CookieLocaleGuesserTest.php index 053d36e..e71dc0a 100644 --- a/Tests/LocaleGuesser/CookieLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/CookieLocaleGuesserTest.php @@ -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 { @@ -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); } } diff --git a/Tests/LocaleGuesser/DomainLocaleGuesserTest.php b/Tests/LocaleGuesser/DomainLocaleGuesserTest.php index da2cfb5..29ad7bc 100644 --- a/Tests/LocaleGuesser/DomainLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/DomainLocaleGuesserTest.php @@ -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 @@ -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->createMock('Symfony\Component\HttpFoundation\Request'); + return $this->createMock(Request::class); } } diff --git a/Tests/LocaleGuesser/LocaleGuesserManagerTest.php b/Tests/LocaleGuesser/LocaleGuesserManagerTest.php index e47f39a..1867098 100644 --- a/Tests/LocaleGuesser/LocaleGuesserManagerTest.php +++ b/Tests/LocaleGuesser/LocaleGuesserManagerTest.php @@ -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->expectException('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()); @@ -137,11 +139,7 @@ private function getRequestWithoutLocaleQuery() */ private function getGuesserMock() { - $mock = $this->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface') - ->disableOriginalConstructor() - ->getMock(); - - return $mock; + return $this->createMock(LocaleGuesserInterface::class); } /** @@ -149,16 +147,12 @@ private function getGuesserMock() */ private function getMetaValidatorMock() { - $mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator') - ->disableOriginalConstructor() - ->getMock(); - - return $mock; + return $this->createMock(MetaValidator::class); } private function getMockLogger() { - return $this->createMock('Psr\Log\LoggerInterface'); + return $this->createMock(LoggerInterface::class); } } diff --git a/Tests/LocaleGuesser/QueryLocaleGuesserTest.php b/Tests/LocaleGuesser/QueryLocaleGuesserTest.php index 6cbf4db..0cffc5f 100644 --- a/Tests/LocaleGuesser/QueryLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/QueryLocaleGuesserTest.php @@ -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 { @@ -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'); @@ -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); } } diff --git a/Tests/LocaleGuesser/RouterLocaleGuesserTest.php b/Tests/LocaleGuesser/RouterLocaleGuesserTest.php index 2d045d4..ce65040 100644 --- a/Tests/LocaleGuesser/RouterLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/RouterLocaleGuesserTest.php @@ -12,6 +12,7 @@ use Lunetics\LocaleBundle\LocaleGuesser\RouterLocaleGuesser; use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface; use Symfony\Component\HttpFoundation\Request; +use Lunetics\LocaleBundle\Validator\MetaValidator; class RouterLocaleGuesserTest extends \PHPUnit_Framework_TestCase { @@ -25,7 +26,7 @@ public function testLocaleIsIdentified() { $request = $this->getRequestWithLocaleParameter(); $metaValidator = $this->getMetaValidatorMock(); - $guesser = new RouterLocaleGuesser($metaValidator, false); + $guesser = new RouterLocaleGuesser($metaValidator); $metaValidator->expects($this->once()) ->method('isAllowed') @@ -40,7 +41,7 @@ public function testLocaleIsNotAllowed() { $request = $this->getRequestWithLocaleParameter(); $metaValidator = $this->getMetaValidatorMock(); - $guesser = new RouterLocaleGuesser($metaValidator, false); + $guesser = new RouterLocaleGuesser($metaValidator); $metaValidator->expects($this->once()) ->method('isAllowed') @@ -55,7 +56,7 @@ public function testLocaleIsNotIdentified() { $request = $this->getRequestWithLocaleQuery('fr'); $metaValidator = $this->getMetaValidatorMock(); - $guesser = new RouterLocaleGuesser($metaValidator, false); + $guesser = new RouterLocaleGuesser($metaValidator); $metaValidator->expects($this->never()) ->method('isAllowed'); @@ -82,8 +83,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); } } diff --git a/Tests/LocaleGuesser/SessionLocaleGuesserTest.php b/Tests/LocaleGuesser/SessionLocaleGuesserTest.php index 53e9820..8941927 100644 --- a/Tests/LocaleGuesser/SessionLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/SessionLocaleGuesserTest.php @@ -14,6 +14,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Session; +use Lunetics\LocaleBundle\Validator\MetaValidator; class SessionLocaleGuesserTest extends \PHPUnit_Framework_TestCase { @@ -102,9 +103,7 @@ private function getSession() public function getMetaValidatorMock() { - $mock = $this->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator')->disableOriginalConstructor()->getMock(); - - return $mock; + return $this->createMock(MetaValidator::class); } /** diff --git a/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php b/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php index 2dd7834..6425c6c 100644 --- a/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php @@ -10,6 +10,8 @@ namespace Lunetics\LocaleBundle\Tests\LocaleGuesser; use Lunetics\LocaleBundle\LocaleGuesser\SubdomainLocaleGuesser; +use Lunetics\LocaleBundle\Validator\MetaValidator; +use Symfony\Component\HttpFoundation\Request; /** * @author Kevin Archer @@ -62,15 +64,11 @@ public function dataDomains() private function getMockMetaValidator() { - return $this - ->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator') - ->disableOriginalConstructor() - ->getMock() - ; + return $this->createMock(MetaValidator::class); } private function getMockRequest() { - return $this->createMock('Symfony\Component\HttpFoundation\Request'); + return $this->createMock(Request::class); } } diff --git a/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php b/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php index d6a85b6..e4f785a 100644 --- a/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php +++ b/Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php @@ -11,6 +11,8 @@ use Lunetics\LocaleBundle\LocaleGuesser\TopleveldomainLocaleGuesser; use Lunetics\LocaleBundle\LocaleInformation\TopleveldomainLocaleMap; +use Lunetics\LocaleBundle\Validator\MetaValidator; +use Symfony\Component\HttpFoundation\Request; /** * @author Ivo Bathke @@ -21,6 +23,7 @@ class TopleveldomainLocaleGuesserTest extends \PHPUnit_Framework_TestCase * @dataProvider dataDomains * * @param bool $expected + * @param string $expectedLocale * @param string $host * @param bool $allowed * @param string $mappedLocale @@ -68,22 +71,16 @@ public function dataDomains() private function getMockTopleveldomainLocaleMap() { - return $this - ->getMockBuilder('\Lunetics\LocaleBundle\LocaleInformation\TopleveldomainLocaleMap') - ->disableOriginalConstructor() - ->getMock(); + return $this->createMock(TopleveldomainLocaleMap::class); } private function getMockMetaValidator() { - return $this - ->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator') - ->disableOriginalConstructor() - ->getMock(); + return $this->createMock(MetaValidator::class); } private function getMockRequest() { - return $this->createMock('Symfony\Component\HttpFoundation\Request'); + return $this->createMock(Request::class); } } diff --git a/Tests/LocaleInformation/LocaleInformationTest.php b/Tests/LocaleInformation/LocaleInformationTest.php index 1a13637..513adf2 100644 --- a/Tests/LocaleInformation/LocaleInformationTest.php +++ b/Tests/LocaleInformation/LocaleInformationTest.php @@ -11,6 +11,7 @@ use Lunetics\LocaleBundle\LocaleInformation\AllowedLocalesProvider; use Lunetics\LocaleBundle\Tests\Validator\BaseMetaValidator; use Lunetics\LocaleBundle\LocaleInformation\LocaleInformation; +use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager; /** * Test for the LocaleInformation @@ -121,8 +122,11 @@ private function getLocaleInformation(array $preferredLocale) ->will($this->returnValue($preferredLocale)) ; - return new LocaleInformation($this->getMetaValidator($allowedLocales), $guesserManager, new AllowedLocalesProvider($allowedLocales)); - + return new LocaleInformation( + $this->getMetaValidator($allowedLocales), + $guesserManager, + new AllowedLocalesProvider($allowedLocales) + ); } public function testGetPreferredLocales() @@ -143,6 +147,6 @@ public function testGetPreferredLocalesNoneDefined() protected function getGuesserManagerMock() { - return $this->getMockBuilder('Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserManager')->disableOriginalConstructor()->getMock(); + return $this->createMock(LocaleGuesserManager::class); } -} \ No newline at end of file +} diff --git a/Tests/LuneticsLocaleBundleTest.php b/Tests/LuneticsLocaleBundleTest.php index 89052cb..34f5da3 100644 --- a/Tests/LuneticsLocaleBundleTest.php +++ b/Tests/LuneticsLocaleBundleTest.php @@ -12,6 +12,7 @@ use Lunetics\LocaleBundle\DependencyInjection\Compiler\GuesserCompilerPass; use Lunetics\LocaleBundle\DependencyInjection\Compiler\RouterResourcePass; use Lunetics\LocaleBundle\LuneticsLocaleBundle; +use Symfony\Component\DependencyInjection\ContainerBuilder; /** * @author Kevin Archer @@ -40,6 +41,6 @@ public function testBuild() protected function getMockContainer() { - return $this->createMock('Symfony\Component\DependencyInjection\ContainerBuilder'); + return $this->createMock(ContainerBuilder::class); } } diff --git a/Tests/Session/LocaleSessionTest.php b/Tests/Session/LocaleSessionTest.php index a063c49..897e4d1 100644 --- a/Tests/Session/LocaleSessionTest.php +++ b/Tests/Session/LocaleSessionTest.php @@ -10,6 +10,7 @@ namespace Lunetics\LocaleBundle\Tests\LocaleInformation; use Lunetics\LocaleBundle\Session\LocaleSession; +use Symfony\Component\HttpFoundation\Session\Session; /** * @author Kevin Archer @@ -74,6 +75,6 @@ public function testGetSessionVar() public function getMockSession() { - return $this->createMock('Symfony\Component\HttpFoundation\Session\Session'); + return $this->createMock(Session::class); } } diff --git a/Tests/Switcher/TargetInformationBuilderTest.php b/Tests/Switcher/TargetInformationBuilderTest.php index d8be642..b9db43c 100644 --- a/Tests/Switcher/TargetInformationBuilderTest.php +++ b/Tests/Switcher/TargetInformationBuilderTest.php @@ -206,6 +206,6 @@ private function getRequestWithBrowserPreferences($route = "/") private function getRouter() { - return $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor()->getMock(); + return $this->createMock(RouterInterface::class); } } diff --git a/Tests/Templating/Helper/LocaleSwitchHelperTest.php b/Tests/Templating/Helper/LocaleSwitchHelperTest.php index 5d0c58f..df7abad 100644 --- a/Tests/Templating/Helper/LocaleSwitchHelperTest.php +++ b/Tests/Templating/Helper/LocaleSwitchHelperTest.php @@ -10,6 +10,7 @@ namespace Lunetics\LocaleBundle\Tests\Templating\Helper; use Lunetics\LocaleBundle\Templating\Helper\LocaleSwitchHelper; +use Symfony\Component\Templating\EngineInterface; /** * @author Kevin Archer @@ -44,6 +45,6 @@ public function testGetName() protected function getMockEngineInterface() { - return $this->createMock('Symfony\Component\Templating\EngineInterface'); + return $this->createMock(EngineInterface::class); } } diff --git a/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php b/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php index c883e4e..fb3691b 100644 --- a/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php +++ b/Tests/Twig/Extension/LocaleSwitcherExtensionTest.php @@ -20,6 +20,7 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\HttpFoundation\ParameterBag; +use Twig_SimpleFunction; /** * @covers \Lunetics\LocaleBundle\Twig\Extension\LocaleSwitcherExtension @@ -68,7 +69,7 @@ public function testGetFunctions() { $functions = $this->extension->getFunctions(); - /** @var \Twig_SimpleFunction $twigExtension */ + /** @var Twig_SimpleFunction $twigExtension */ $twigExtension = current($functions); $this->assertInstanceOf('Twig_SimpleFunction', $twigExtension); @@ -86,7 +87,7 @@ public function testRenderSwitcher() { $template = uniqid('template:'); - $request = $this->getMockRequest(); + $request = $this->createMock(Request::class); $request->attributes = $this->getMockParameterBag(); $query = $this->getMockParameterBag(); @@ -114,16 +115,6 @@ public function testRenderSwitcher() $this->assertEquals($template, $this->extension->renderSwitcher()); } - protected function getMockRequest() - { - return $this->createMock(Request::class); - } - - protected function getMockParameterBag() - { - return $this->createMock(ParameterBag::class); - } - private function constructExtension() { $this->extension = new LocaleSwitcherExtension( @@ -135,4 +126,12 @@ private function constructExtension() $this->useControllerParam ); } + + /** + * @return MockObject|ParameterBag + */ + private function getMockParameterBag() + { + return $this->createMock(ParameterBag::class); + } } diff --git a/Tests/Validator/BaseMetaValidator.php b/Tests/Validator/BaseMetaValidator.php index d58e098..c8514e6 100644 --- a/Tests/Validator/BaseMetaValidator.php +++ b/Tests/Validator/BaseMetaValidator.php @@ -76,10 +76,10 @@ public function getContext() { // Run tests against non-deprecated ExecutionContext, if possible if (class_exists('\Symfony\Component\Validator\Context\ExecutionContext')) { - return $this->getMockBuilder('\Symfony\Component\Validator\Context\ExecutionContext')->disableOriginalConstructor()->getMock(); + return $this->createMock('\Symfony\Component\Validator\Context\ExecutionContext'); } else { // use deprecated ExecutionContext otherwise - return $this->getMockBuilder('\Symfony\Component\Validator\ExecutionContext')->disableOriginalConstructor()->getMock(); + return $this->createMock('\Symfony\Component\Validator\ExecutionContext'); } } diff --git a/Tests/Validator/LocaleValidatorTest.php b/Tests/Validator/LocaleValidatorTest.php index ca38ab6..cf597cf 100644 --- a/Tests/Validator/LocaleValidatorTest.php +++ b/Tests/Validator/LocaleValidatorTest.php @@ -16,6 +16,7 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\Yaml\Parser as YamlParser; +use Symfony\Component\Validator\Constraint; /** * Test for the LocaleValidator @@ -134,6 +135,6 @@ public function testValidateEmptyLocale() protected function getMockConstraint() { - return $this->createMock('Symfony\Component\Validator\Constraint'); + return $this->createMock(Constraint::class); } } diff --git a/Twig/Extension/LocaleSwitcherExtension.php b/Twig/Extension/LocaleSwitcherExtension.php index 3ce39f4..1b6616a 100644 --- a/Twig/Extension/LocaleSwitcherExtension.php +++ b/Twig/Extension/LocaleSwitcherExtension.php @@ -109,16 +109,12 @@ public function getName() */ public function renderSwitcher($route = null, $parameters = array(), $template = null) { - $showCurrentLocale = $this->showCurrentLocaleParam; - $useController = $this->useControllerParam; - $allowedLocales = $this->allowedLocalesProvider->getAllowedLocales(); - $informationBuilder = new TargetInformationBuilder( $this->requestStack->getMasterRequest(), $this->router, - $allowedLocales, - $showCurrentLocale, - $useController + $this->allowedLocalesProvider->getAllowedLocales(), + $this->showCurrentLocaleParam, + $this->useControllerParam ); $viewParams = $informationBuilder->getTargetInformations($route, $parameters);