Skip to content

Commit

Permalink
Merge pull request #202 from ElectricMaxxx/inject_services
Browse files Browse the repository at this point in the history
do not use container directly
  • Loading branch information
lunetics authored Feb 3, 2018
2 parents 11de2c1 + 7085000 commit aae3170
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 247 deletions.
2 changes: 1 addition & 1 deletion LocaleInformation/AllowedLocalesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* For the full copyright and license information, please view the LICENSE
* file that is distributed with this source code.
*/
namespace Lunetics\LocaleBundle\LocaleInformation;

namespace Lunetics\LocaleBundle\LocaleInformation;

class AllowedLocalesProvider
{
Expand Down
10 changes: 9 additions & 1 deletion Resources/config/switcher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
</service>

<service id="lunetics_locale.twig.switcher" class="%lunetics_locale.twig.switcher.class%">
<argument type="service" id="service_container"/>
<argument type="service" id="lunetics_locale.switcher.target_information_builder"/>
<argument type="service" id="lunetics_locale.switcher_helper"/>
<tag name="twig.extension"/>
</service>

Expand All @@ -30,5 +31,12 @@
<argument>%lunetics_locale.switcher.redirect_statuscode%</argument>
</service>

<service id="lunetics_locale.switcher.target_information_builder" class="Lunetics\LocaleBundle\Switcher\TargetInformationBuilder">
<argument type="service" id="request_stack" />
<argument type="service" id="router.default"/>

This comment has been minimized.

Copy link
@ElectricMaxxx

ElectricMaxxx Mar 13, 2018

Collaborator

Oh, it was me .... :-(

<argument type="service" id="lunetics_locale.allowed_locales_provider"/>
<argument>'%lunetics_locale.switcher.show_current_locale%'</argument>
<argument>'%lunetics_locale.switcher.use_controlle%'</argument>
</service>
</services>
</container>
63 changes: 42 additions & 21 deletions Switcher/TargetInformationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* For the full copyright and license information, please view the LICENSE
* file that is distributed with this source code.
*/

namespace Lunetics\LocaleBundle\Switcher;

use Symfony\Component\HttpFoundation\Request;
use Lunetics\LocaleBundle\LocaleInformation\AllowedLocalesProvider;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
Expand All @@ -24,24 +26,44 @@
*/
class TargetInformationBuilder
{
private $request;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var RouterInterface
*/
private $router;
/**
* @var bool
*/
private $showCurrentLocale;
/**
* @var bool
*/
private $useController;
private $allowedLocales;
/**
* @var AllowedLocalesProvider
*/
private $allowedLocalesProvider;

/**
* @param Request $request Request
* @param RouterInterface $router Router
* @param array $allowedLocales Config Var
* @param bool $showCurrentLocale Config Var
* @param bool $useController Config Var
* @param RequestStack $requestStack Request
* @param RouterInterface $router Router
* @param AllowedLocalesProvider $allowedLocalesProvider
* @param bool $showCurrentLocale Config Var
* @param bool $useController Config Var
*/
public function __construct(Request $request, RouterInterface $router, $allowedLocales = array(), $showCurrentLocale = false, $useController = false)
{
$this->request = $request;
public function __construct(
RequestStack $requestStack,
RouterInterface $router,
AllowedLocalesProvider $allowedLocalesProvider,
$showCurrentLocale = false,
$useController = false
) {
$this->requestStack = $requestStack;
$this->router = $router;
$this->allowedLocales = $allowedLocales;
$this->allowedLocalesProvider = $allowedLocalesProvider;
$this->showCurrentLocale = $showCurrentLocale;
$this->useController = $useController;
}
Expand All @@ -62,13 +84,13 @@ public function __construct(Request $request, RouterInterface $router, $allowedL
* locale_current_language: Anglais
*
* @param string|null $targetRoute The target route
* @param array $parameters Parameters
* @param array $parameters Parameters
*
* @return array Informations for the switcher template
*/
public function getTargetInformations($targetRoute = null, $parameters = array())
public function getTargetInformations($targetRoute = null, $parameters = [])
{
$request = $this->request;
$request = $this->requestStack->getCurrentRequest();
$router = $this->router;
$route = $request->attributes->get('_route');

Expand All @@ -83,12 +105,11 @@ public function getTargetInformations($targetRoute = null, $parameters = array()

$infos['current_locale'] = $request->getLocale();
$infos['current_route'] = $route;
$infos['locales'] = array();
$infos['locales'] = [];

$parameters = array_merge((array) $request->attributes->get('_route_params'), $request->query->all(), (array) $parameters);

$targetLocales = $this->allowedLocales;
foreach ($targetLocales as $locale) {
foreach ($this->allowedLocalesProvider->getAllowedLocales() as $locale) {
$strpos = 0 === strpos($request->getLocale(), $locale);
if ($this->showCurrentLocale && $strpos || !$strpos) {
$targetLocaleTargetLang = Intl::getLanguageBundle()->getLanguageName($locale, null, $locale);
Expand All @@ -98,7 +119,7 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
if (null !== $targetRoute && "" !== $targetRoute) {
$switchRoute = $router->generate($targetRoute, $parameters);
} elseif ($this->useController) {
$switchRoute = $router->generate('lunetics_locale_switcher', array('_locale' => $locale));
$switchRoute = $router->generate('lunetics_locale_switcher', ['_locale' => $locale]);
} elseif ($route) {
$switchRoute = $router->generate($route, $parameters);
} else {
Expand All @@ -118,12 +139,12 @@ public function getTargetInformations($targetRoute = null, $parameters = array()
throw $e;
}

$infos['locales'][$locale] = array(
$infos['locales'][$locale] = [
'locale_current_language' => $targetLocaleCurrentLang,
'locale_target_language' => $targetLocaleTargetLang,
'link' => $switchRoute,
'locale' => $locale,
);
];
}
}

Expand Down
39 changes: 16 additions & 23 deletions Tests/EventListener/LocaleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

namespace Lunetics\LocaleBundle\Tests\EventListener;

use Lunetics\LocaleBundle\LocaleGuesser\LocaleGuesserInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

Expand Down Expand Up @@ -147,7 +152,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 +167,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 +311,7 @@ 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 All @@ -316,7 +321,7 @@ private function getListener($locale = 'en', $manager = null, $logger = null, $m
}

$listener = new LocaleListener($locale, $manager, $matcher, $logger);
$listener->setEventDispatcher(new \Symfony\Component\EventDispatcher\EventDispatcher());
$listener->setEventDispatcher(new EventDispatcher());

return $listener;
}
Expand Down Expand Up @@ -352,31 +357,23 @@ private function getGuesserManager($order = array(1 => 'router', 2 => 'browser')

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

/**
* @return LocaleGuesserInterface
*/
private function getMockGuesser()
{
$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 getRequestWithRouterParam($routerLocale = 'es')
Expand Down Expand Up @@ -413,25 +410,21 @@ private function getEmptyRequest()

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

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);
}
}
5 changes: 3 additions & 2 deletions Tests/EventListener/LocaleUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Lunetics\LocaleBundle\Tests\EventListener;

use Lunetics\LocaleBundle\LocaleBundleEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -179,7 +180,7 @@ 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 +200,6 @@ private function getRequest($withCookieSet = false)

private function getMockLogger()
{
return $this->getMock('Psr\Log\LoggerInterface');
return $this->createMock(LoggerInterface::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 @@ -9,7 +9,9 @@
*/
namespace Lunetics\LocaleBundle\Tests\Form\Extension\Type;

use Lunetics\LocaleBundle\Form\Extension\ChoiceList\LocaleChoiceList;
use Lunetics\LocaleBundle\Form\Extension\Type\LocaleType;
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);
}
}
20 changes: 10 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 Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap;
use Lunetics\LocaleBundle\Validator\MetaValidator;
use Symfony\Component\HttpFoundation\Request;

/**
* @author Jachim Coudenys <[email protected]>
Expand Down Expand Up @@ -65,24 +68,21 @@ public function dataDomains()
);
}

/**
* @return mixed
*/
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);
}
}
}
Loading

0 comments on commit aae3170

Please sign in to comment.