Skip to content

Commit

Permalink
Merge pull request Kunstmaan#8 from JoakimLofgren/use-request-pathinfo
Browse files Browse the repository at this point in the history
Use request pathinfo
  • Loading branch information
Wim Vandersmissen committed May 8, 2014
2 parents eef8094 + dc7e0d4 commit 908e6dd
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ public function __construct(MetaValidator $metaValidator)
public function guessLocale(Request $request)
{
$localeValidator = $this->metaValidator;
if ($path = $request->attributes->get('path')) {
$parts = array_filter(explode("/", $path));
$locale = array_shift($parts);

if ($localeValidator->isAllowed($locale)) {
$this->identifiedLocale = $locale;
$path = $request->getPathInfo();
if ($request->attributes->has('path')) {
$path = $request->attributes->get('path');
}

if (!$path) {
return false;
}

$parts = array_filter(explode("/", $path));
$locale = array_shift($parts);

if ($localeValidator->isAllowed($locale)) {
$this->identifiedLocale = $locale;

return true;
}
return true;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Kunstmaan\LanguageChooserBundle\Tests\LocaleGuesser;

use Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser;
use Lunetics\LocaleBundle\Validator\MetaValidator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* UrlLocaleGuesserTest
*/
class UrlLocaleGuesserTest extends \PHPUnit_Framework_TestCase
{
/**
* @var MetaValidator
*/
protected $metaValidator;

/**
* @var Request
*/
protected $request;

/**
* @var UrlLocaleGuesser
*/
protected $object;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->metaValidator = $this->getMockBuilder('Lunetics\LocaleBundle\Validator\MetaValidator')
->disableOriginalConstructor()
->getMock();

$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();

$this->request->attributes = new ParameterBag();

$this->object = new UrlLocaleGuesser($this->metaValidator);
}

/**
* @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale
*/
public function testFoundLocalePathAttribute()
{
$this->metaValidator->expects($this->once())
->method('isAllowed')
->will($this->returnValue(true));

$this->request->expects($this->once())
->method('getPathInfo')
->will($this->returnValue('/de/some-path'));

$this->request->attributes->set('path', '/de/some-path');

$this->assertTrue($this->object->guessLocale($this->request));
$this->assertEquals('de', $this->object->getIdentifiedLocale());
}

/**
* @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale
*/
public function testFoundLocalePathInfo()
{
$this->metaValidator->expects($this->once())
->method('isAllowed')
->will($this->returnValue(true));

$this->request->expects($this->once())
->method('getPathInfo')
->will($this->returnValue('/fr/some-other-path'));

$this->assertTrue($this->object->guessLocale($this->request));
$this->assertEquals('fr', $this->object->getIdentifiedLocale());
}

/**
* @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale
*/
public function testNullPath()
{
$this->metaValidator->expects($this->never())
->method('isAllowed');

$this->request->expects($this->once())
->method('getPathInfo')
->will($this->returnValue(null));

$this->assertFalse($this->object->guessLocale($this->request));
$this->assertEquals(null, $this->object->getIdentifiedLocale());
}

/**
* @covers Kunstmaan\LanguageChooserBundle\LocaleGuesser\UrlLocaleGuesser::guessLocale
*/
public function testInvalidLocaleFound()
{
$this->metaValidator->expects($this->once())
->method('isAllowed')
->will($this->returnValue(false));

$this->request->expects($this->once())
->method('getPathInfo')
->will($this->returnValue('/en/another-path'));

$this->assertFalse($this->object->guessLocale($this->request));
$this->assertEquals(null, $this->object->getIdentifiedLocale());
}
}

0 comments on commit 908e6dd

Please sign in to comment.