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 1 commit
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
15 changes: 7 additions & 8 deletions Tests/EventListener/LocaleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -432,6 +431,6 @@ private function getMockFilterResponseEvent()

private function getMockLogger()
{
return $this->getMock('Psr\Log\LoggerInterface');
return $this->createMock('Psr\Log\LoggerInterface');
}
}
9 changes: 7 additions & 2 deletions Tests/EventListener/LocaleUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}


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

private function getMockLogger()
{
return $this->getMock('Psr\Log\LoggerInterface');
return $this->createMock('Psr\Log\LoggerInterface');
}
}
2 changes: 1 addition & 1 deletion Tests/Form/Extension/Type/LocaleTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ protected function getMockLocaleChoiceList()

protected function getMockOptionsResolverInterface()
{
return $this->getMock('Symfony\Component\OptionsResolver\OptionsResolver');
return $this->createMock('Symfony\Component\OptionsResolver\OptionsResolver');
}
}
4 changes: 2 additions & 2 deletions Tests/LocaleGuesser/DomainLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ private function getMockMetaValidator()

private function getMockRequest()
{
return $this->getMock('Symfony\Component\HttpFoundation\Request');
return $this->createMock('Symfony\Component\HttpFoundation\Request');
}
}
}
12 changes: 8 additions & 4 deletions Tests/LocaleGuesser/LocaleGuesserManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just createMock would be enough, it disables the constructor automatically

->disableOriginalConstructor()
->getMock();

return $mock;
}
Expand All @@ -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');
}

}
4 changes: 2 additions & 2 deletions Tests/LocaleGuesser/SubdomainLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function dataDomains()
array(false, 'de-DE.domain', false, '_'),
);
}

private function getMockMetaValidator()
{
return $this
Expand All @@ -71,6 +71,6 @@ private function getMockMetaValidator()

private function getMockRequest()
{
return $this->getMock('Symfony\Component\HttpFoundation\Request');
return $this->createMock('Symfony\Component\HttpFoundation\Request');
}
}
4 changes: 2 additions & 2 deletions Tests/LocaleGuesser/TopleveldomainLocaleGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ private function getMockMetaValidator()

private function getMockRequest()
{
return $this->getMock('Symfony\Component\HttpFoundation\Request');
return $this->createMock('Symfony\Component\HttpFoundation\Request');
}
}
}
2 changes: 1 addition & 1 deletion Tests/LuneticsLocaleBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function testBuild()

protected function getMockContainer()
{
return $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
return $this->createMock('Symfony\Component\DependencyInjection\ContainerBuilder');
}
}
4 changes: 2 additions & 2 deletions Tests/Session/LocaleSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
2 changes: 1 addition & 1 deletion Tests/Templating/Helper/LocaleSwitchHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ public function testGetName()

protected function getMockEngineInterface()
{
return $this->getMock('Symfony\Component\Templating\EngineInterface');
return $this->createMock('Symfony\Component\Templating\EngineInterface');
}
}
107 changes: 65 additions & 42 deletions Tests/Twig/Extension/LocaleSwitcherExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
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);
Expand All @@ -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();

Expand All @@ -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
);
}
}
4 changes: 2 additions & 2 deletions Tests/Validator/LocaleAllowedValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion Tests/Validator/LocaleValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ public function testValidateEmptyLocale()

protected function getMockConstraint()
{
return $this->getMock('Symfony\Component\Validator\Constraint');
return $this->createMock('Symfony\Component\Validator\Constraint');
}
}
Loading