Skip to content

Commit

Permalink
Ignore phpunit.xml and coverage dir report/
Browse files Browse the repository at this point in the history
Integration test for GuesserCompilerPass

Add tests for LocaleExtension

Add tests for LocaleListener

Add tests for EventListener\LocaleUpdate

Add tests for LocaleType

Remove unused private method getPrimaryLanguage

Add argument to test docblock

Add tests for LocaleGuesserManager

Add tests for SessionLocaleGuesser

Add tests for LocaleInformation

Add LocaleSession tests

Add LocaleSwitchHelper tests

Add integration tests for LocaleSwitcher

Add tests for LocaleAllowedValidator

Add LocaleValidator tests

Add LuneticsLocaleBundle test

Removed refernces to the intl extension
  • Loading branch information
kevinarcher authored and lunetics committed May 5, 2013
1 parent b22d844 commit efb497f
Show file tree
Hide file tree
Showing 22 changed files with 754 additions and 145 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
vendor/
composer.phar
composer.lock
phpunit.xml
report/
28 changes: 2 additions & 26 deletions LocaleGuesser/BrowserLocaleGuesser.php
Expand Up @@ -25,11 +25,6 @@ class BrowserLocaleGuesser implements LocaleGuesserInterface
*/
private $identifiedLocale;

/**
* @var bool
*/
private $intlExtension;

/**
* @var MetaValidator
*/
Expand All @@ -38,13 +33,11 @@ class BrowserLocaleGuesser implements LocaleGuesserInterface
/**
* Constructor
*
* @param MetaValidator $metaValidator MetaValidator
* @param bool $intlExtensionInstalled Wether the intl extension is installed
* @param MetaValidator $metaValidator MetaValidator
*/
public function __construct(MetaValidator $metaValidator, $intlExtensionInstalled = false)
public function __construct(MetaValidator $metaValidator)
{
$this->metaValidator = $metaValidator;
$this->intlExtension = $intlExtensionInstalled;
}

/**
Expand Down Expand Up @@ -87,23 +80,6 @@ public function guessLocale(Request $request)
return false;
}

/**
* Fallback function for fetching the primary language, if no intl extension is installed.
*
* @param string $locale
*
* @return null|string
*/
private function getPrimaryLanguage($locale)
{
if ($this->intlExtension) {
return \Locale::getPrimaryLanguage($locale);
}
$splittedLocale = explode('_', $locale);

return count($splittedLocale) > 1 ? $splittedLocale[0] : $locale;
}

/**
* {@inheritDoc}
*/
Expand Down
1 change: 1 addition & 0 deletions LocaleGuesser/LocaleGuesserManager.php
Expand Up @@ -40,6 +40,7 @@ class LocaleGuesserManager
* @var array
*/
private $preferredLocales;

/**
* @var LoggerInterface
*/
Expand Down
5 changes: 0 additions & 5 deletions LocaleGuesser/QueryLocaleGuesser.php
Expand Up @@ -28,11 +28,6 @@ class QueryLocaleGuesser implements LocaleGuesserInterface
*/
private $identifiedLocale;

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

/**
* Constructor
*
Expand Down
5 changes: 3 additions & 2 deletions Session/LocaleSession.php
Expand Up @@ -64,11 +64,12 @@ public function setLocale($locale)
/**
* Returns the locale
*
* @param string $locale
* @param $locale
* @return string
*/
public function getLocale($locale)
{
$this->session->get($this->sessionVar, $locale);
return $this->session->get($this->sessionVar, $locale);
}

/**
Expand Down
63 changes: 63 additions & 0 deletions Tests/DependencyInjection/Compiler/GuesserCompilerPassTest.php
@@ -0,0 +1,63 @@
<?php
/**
* This file is part of the LuneticsLocaleBundle package.
*
* <https://github.com/lunetics/LocaleBundle/>
*
* For the full copyright and license information, please view the LICENSE
* file that is distributed with this source code.
*/
namespace Lunetics\LocaleBundle\Tests\DependencyInjection\Compiler;

use Lunetics\LocaleBundle\DependencyInjection\Compiler\GuesserCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Kevin Archer <ka@kevinarcher.ca>
*/
class GuesserCompilerPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();

$container
->register('lunetics_locale.guesser_manager')
;

$container
->register('lunetics_locale.query_guesser')
->addTag('lunetics_locale.guesser', array('alias' => 'query'))
;

$container
->register('lunetics_locale.browser_guesser')
->addTag('lunetics_locale.guesser', array('alias' => 'browser'))
;

$container->setParameter('lunetics_locale.guessing_order', array('query'));

$this->process($container);

$methodCalls = $container
->getDefinition('lunetics_locale.guesser_manager')
->getMethodCalls();

$this->assertCount(
1,
$methodCalls
);

$methodName = $methodCalls[0][0];
$argument = $methodCalls[0][1][1];

$this->assertEquals('addGuesser', $methodName);
$this->assertEquals('query', $argument);
}

protected function process(ContainerBuilder $container)
{
$pass = new GuesserCompilerPass();
$pass->process($container);
}
}
135 changes: 53 additions & 82 deletions Tests/DependencyInjection/LuneticsLocaleExtensionTest.php
Expand Up @@ -13,119 +13,90 @@
use Lunetics\LocaleBundle\DependencyInjection\LuneticsLocaleExtension;
use Symfony\Component\Yaml\Parser;

/**
* @author Kevin Archer <ka@kevinarcher.ca>
*/
class LuneticsLocaleExtensionTest extends \PHPUnit_Framework_TestCase
{
protected $configuration;

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testBundleLoadThrowsExceptionUnlessDetectorsOrderIsSet()
public function testLoad()
{
$loader = new LuneticsLocaleExtension();
$config = $this->getEmptyConfig();
unset($config['guessing_order']);
$loader->load(array($config), new ContainerBuilder());
$container = new ContainerBuilder();

$configs = $this->getFullConfig();

$loader->load($configs, $container);

$this->assertTrue($container->hasParameter('lunetics_locale.allowed_locales'));
$this->assertTrue($container->hasParameter('lunetics_locale.intl_extension_installed'));

if (extension_loaded('intl')) {
$this->assertEquals(array(), $container->getParameter('lunetics_locale.intl_extension_fallback.iso3166'));
$this->assertEquals(array(), $container->getParameter('lunetics_locale.intl_extension_fallback.iso639'));
$this->assertEquals(array(), $container->getParameter('lunetics_locale.intl_extension_fallback.script'));
} else {
$this->assertGreaterThan(0, count($container->getParameter('lunetics_locale.intl_extension_fallback.iso3166')));
$this->assertGreaterThan(0, count($container->getParameter('lunetics_locale.intl_extension_fallback.iso639')));
$this->assertGreaterThan(0, count($container->getParameter('lunetics_locale.intl_extension_fallback.script')));
}

$resources = $container->getResources();

$this->assertContains('validator.xml', $resources[0]->getResource());
$this->assertContains('guessers.xml', $resources[1]->getResource());
$this->assertContains('services.xml', $resources[2]->getResource());
$this->assertContains('switcher.xml', $resources[3]->getResource());
$this->assertContains('form.xml', $resources[4]->getResource());
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The child node "guessing_order" at path "lunetics_locale" must be configured.
*/
public function testBundleLoadThrowsExceptionIfNonBooleanValueIsSet()
public function testBundleLoadThrowsExceptionUnlessGuessingOrderIsSet()
{
$loader = new LuneticsLocaleExtension();
$config = $this->getEmptyConfig();
$config['router_guesser']['check_query'] = 'hello';
$loader->load(array($config), new ContainerBuilder());
$loader->load(array(), new ContainerBuilder());
}

/**
* @return ContainerBuilder
*/
protected function createEmptyConfiguration()
public function testGetAlias()
{
$this->configuration = new ContainerBuilder();
$loader = new LuneticsLocaleExtension();
$config = $this->getEmptyConfig();
$loader->load(array($config), $this->configuration);
$this->assertTrue($this->configuration instanceof ContainerBuilder);
$this->assertEquals('lunetics_locale', $loader->getAlias());
}

/**
* @return ContainerBuilder
*/
protected function createFullConfiguration()
public function testBindParameters()
{
$this->configuration = new ContainerBuilder();
$loader = new LuneticsLocaleExtension();
$config = $this->getFullConfig();
$loader->load(array($config), $this->configuration);
$this->assertTrue($this->configuration instanceof ContainerBuilder);
}
$container = new ContainerBuilder();

/**
* getEmptyConfig
*
* @return array
*/
protected function getEmptyConfig()
{
$yaml = <<<EOF
allowed_locales:
- de
- fr
- en
guessing_order:
- router
- browser
router_guesser:
check_query: true
EOF;
$parser = new Parser();
$config = array(
'key' => 'value',
);

$loader->bindParameters($container, $loader->getAlias(), $config);

return $parser->parse($yaml);
$this->assertTrue($container->hasParameter('lunetics_locale.key'));
$this->assertEquals('value', $container->getParameter('lunetics_locale.key'));
}

protected function getFullConfig()
{
$yaml = <<<EOF
allowed_locales:
- de
- fr
lunetics_locale:
allowed_locales:
- en
guessing_order:
- router
- fr
- de
guessing_order:
- session
- cookie
- browser
router_guesser:
check_query: true
- query
- router
EOF;
$parser = new Parser();

return $parser->parse($yaml);
}

private function assertAlias($value, $key)
{
$this->assertEquals($value, (string) $this->configuration->getAlias($key), sprintf('%s alias is correct', $key));
}

private function assertParameter($value, $key)
{
$this->assertEquals($value, $this->configuration->getParameter($key), sprintf('%s parameter is correct', $key));
}

private function assertHasDefinition($id)
{
$this->assertTrue(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
}

private function assertNotHasDefinition($id)
{
$this->assertFalse(($this->configuration->hasDefinition($id) ?: $this->configuration->hasAlias($id)));
}

protected function tearDown()
{
unset($this->configuration);
}
}

0 comments on commit efb497f

Please sign in to comment.