Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from bushbaby/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
basz committed Nov 13, 2014
2 parents 1097a1b + f85006f commit a803095
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(EntityManager $entityManager)
/**
* @param string $locale
* @param string $domain
* @return null|TextDomain
* @return TextDomain
* @throws \BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException
*/
public function load($locale, $domain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class DoctrineLoaderFactory implements FactoryInterface
{
/**
* {@inheritdocs}
*
* @todo make EntityManager configurable
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace BsbDoctrineTranslationLoader\Service;

use Zend\I18n\Translator\LoaderPluginManager;
use Zend\Mvc\I18n\Translator;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -13,7 +13,7 @@ class MvcTranslatorDelegatorFactory implements DelegatorFactoryInterface
protected $translator_config = array(
'factories' => array(
'BsbDoctrineTranslationLoader'
=> 'BsbDoctrineTranslationLoader\I18n\Translator\Loader\Factory\DoctrineLoaderFactory',
=> 'BsbDoctrineTranslationLoader\I18n\Translator\Loader\Factory\DoctrineLoaderFactory',
),
);

Expand Down
4 changes: 3 additions & 1 deletion src/BsbDoctrineTranslationLoader/Util/ConfigManipulate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

class ConfigManipulate
{

/**
* Token used as key in configuration 'template' to replace EntityManager 'name'
*/
const EM_REPLACE_TOKEN = '__em_token__';

public static function onMergeConfig(ModuleEvent $e)
Expand Down
4 changes: 1 addition & 3 deletions test/BsbDoctrineTranslationLoaderTest/Entity/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace BsbDoctrineTranslationLoaderTest\I18n\Translator\Loader;

use BsbDoctrineTranslationLoader\Entity\Locale;
use BsbDoctrineTranslationLoader\I18n\Translator\Loader\Doctrine;
use BsbDoctrineTranslationLoader\I18n\Translator\Loader\DoctrineLoader;
use BsbDoctrineTranslationLoaderTest\Framework\TestCase;

class LocaleTest extends TestCase
Expand Down Expand Up @@ -38,4 +36,4 @@ public function testAccessorsPluralForms()

$this->assertEquals($expected, $locale->getPluralForms());
}
}
}
4 changes: 1 addition & 3 deletions test/BsbDoctrineTranslationLoaderTest/Entity/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace BsbDoctrineTranslationLoaderTest\I18n\Translator\Loader;

use BsbDoctrineTranslationLoader\Entity\Locale;
use BsbDoctrineTranslationLoader\Entity\Message;
use BsbDoctrineTranslationLoaderTest\Framework\TestCase;

Expand Down Expand Up @@ -62,5 +61,4 @@ public function testAccessorsTranslation()
$message->setTranslation($expected);
$this->assertEquals($expected, $message->getTranslation());
}

}
}
19 changes: 1 addition & 18 deletions test/BsbDoctrineTranslationLoaderTest/Framework/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace BsbDoctrineTranslationLoaderTest\Framework;

Expand Down Expand Up @@ -51,7 +34,7 @@ public function createDb()
$tool = new SchemaTool($em);
$createSql = $tool->getCreateSchemaSql($meta);

foreach($createSql as $sql) {
foreach ($createSql as $sql) {
$conn->executeQuery($sql);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
use BsbDoctrineTranslationLoader\Entity\Message;
use BsbDoctrineTranslationLoader\I18n\Translator\Loader\DoctrineLoader;
use BsbDoctrineTranslationLoaderTest\Framework\TestCase;
use BsbDoctrineTranslationLoaderTest\Util\ServiceManagerFactory;
use BsbDoctrineTranslationLoader\I18n\Translator\Loader\Doctrine;
use Doctrine\ORM\Tools\SchemaTool;
use Zend\I18n\Translator\TextDomain;
use Zend\ServiceManager\ServiceManager;

class DoctrineLoaderFunctionalTest extends TestCase
{
Expand All @@ -36,7 +32,8 @@ public function tearDown()
unset($this->doctrineLoader);
}

protected function addLocale($locale, $pluralForm = 'nplurals=2; plural=n != 1;') {
protected function addLocale($locale, $pluralForm = 'nplurals=2; plural=n != 1;')
{
$entity = new Locale();
$entity->setLocale($locale);
$entity->setPluralForms($pluralForm);
Expand All @@ -46,9 +43,12 @@ protected function addLocale($locale, $pluralForm = 'nplurals=2; plural=n != 1;'
return $entity;
}

protected function addMessage($locale, $message, $translation, $pluralIndex = null, $domain = 'default') {
protected function addMessage($locale, $message, $translation, $pluralIndex = null, $domain = 'default')
{
if (is_string($locale)) {
$localeEntity = $this->getEntityManager()->getRepository('BsbDoctrineTranslationLoader\Entity\Locale')->findOneBy(array('locale'=>$locale));
$localeEntity = $this->getEntityManager()
->getRepository('BsbDoctrineTranslationLoader\Entity\Locale')
->findOneBy(array('locale'=>$locale));
} else {
$localeEntity = $locale;
}
Expand All @@ -66,7 +66,7 @@ protected function addMessage($locale, $message, $translation, $pluralIndex = nu
return $message;
}

public function test_SingularLoad()
public function testSingularLoad()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, 'key', 'sleutel');
Expand All @@ -83,7 +83,7 @@ public function test_SingularLoad()
$this->assertEquals($textDomain['key'], 'key');
}

public function test_PluralLoad()
public function testPluralLoad()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, '%s key', '%s sleutels', 0);
Expand All @@ -110,30 +110,34 @@ public function test_PluralLoad()
$this->assertEquals($textDomain['%s key'][1], '%s key');
}

public function test_incorrectPluralRuleThrowsException()
public function testIncorrectPluralRuleThrowsException()
{
$locale = $this->addLocale('nl_NL', 'incorrect plural rule');

$this->setExpectedException('BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Incorrect plural rule detected for locale');
$this->setExpectedException(
'BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Incorrect plural rule detected for locale'
);

/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
}

public function test_duplicateLocaleThrowsException()
public function testDuplicateLocaleThrowsException()
{
$locale = $this->addLocale('nl_NL');
$locale = $this->addLocale('nl_NL');

$this->setExpectedException('BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Duplicate locale entry detected');
$this->setExpectedException(
'BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Duplicate locale entry detected'
);

/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
}

public function test_unknownLocaleReturnsEmptyTextDomain()
public function testUnknownLocaleReturnsEmptyTextDomain()
{
/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
Expand All @@ -142,7 +146,7 @@ public function test_unknownLocaleReturnsEmptyTextDomain()
$this->assertCount(0, $textDomain);
}

public function test_unknownDomainReturnsEmptyTextDomain()
public function testUnknownDomainReturnsEmptyTextDomain()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, 'key', 'sleutel');
Expand All @@ -155,55 +159,63 @@ public function test_unknownDomainReturnsEmptyTextDomain()
$this->assertCount(0, $textDomain);
}

public function test_duplicateSingularMessageThrowsException()
public function testDuplicateSingularMessageThrowsException()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, 'key', 'sleutel');
$this->addMessage($locale, 'key', 'sleutel');

$this->setExpectedException('BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Singular entries must be have unique keys from both singular and plural forms');
$this->setExpectedException(
'BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Singular entries must be have unique keys from both singular and plural forms'
);

/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
}

public function test_identicalSingularAndPluralMessageThrowsException()
public function testIdenticalSingularAndPluralMessageThrowsException()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, 'key', 'sleutel');
$this->addMessage($locale, 'key', 'sleutel', 1);

$this->setExpectedException('BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Plural entries must be have unique keys from singular forms');
$this->setExpectedException(
'BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Plural entries must be have unique keys from singular forms'
);

/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
}

public function test_IdenticalPluralAndSingularMessageThrowsException()
public function testIdenticalPluralAndSingularMessageThrowsException()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, 'key', 'sleutel', 1);
$this->addMessage($locale, 'key', 'sleutel');

$this->setExpectedException('BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Singular entries must be have unique keys from both singular and plural forms');
$this->setExpectedException(
'BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Singular entries must be have unique keys from both singular and plural forms'
);

/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
}

public function test_duplicatePluralIndexThrowsException()
public function testDuplicatePluralIndexThrowsException()
{
$locale = $this->addLocale('nl_NL');
$this->addMessage($locale, '%s key', 'sleutels', 1);
$this->addMessage($locale, '%s key', 'sleutel', 1);

$this->setExpectedException('BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Duplicate plural entry detected');
$this->setExpectedException(
'BsbDoctrineTranslationLoader\I18n\Exception\InvalidArgumentException',
'Duplicate plural entry detected'
);

/** @var TextDomain $textDomain */
$textDomain = $this->doctrineLoader->load('nl_NL', 'default');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ public function testInstanceOf()

$this->assertInstanceOf('Zend\I18n\Translator\Loader\RemoteLoaderInterface', $loader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace BsbDoctrineTranslationLoaderTest\I18n\Translator\Loader;

use BsbDoctrineTranslationLoader\I18n\Translator\Loader\Doctrine;
use BsbDoctrineTranslationLoader\I18n\Translator\Loader\Factory\DoctrineLoaderFactory;
use BsbDoctrineTranslationLoaderTest\Framework\TestCase;

Expand Down Expand Up @@ -40,4 +39,4 @@ public function testCanCreateService()

$this->assertInstanceOf('BsbDoctrineTranslationLoader\I18n\Translator\Loader\DoctrineLoader', $service);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace BsbDoctrineTranslationLoaderTest\Service;

use BsbDoctrineTranslationLoader\Service\MvcTranslatorDelegatorFactory;
use BsbDoctrineTranslationLoaderTest\Framework\TestCase;

class MvcTranslatorDelegatorFactoryTest extends TestCase
{
public function testConfiguresTranslatorService()
{
$serviceLocator = $this->getMock('Zend\ServiceManager\ServiceManager');

$loaderPluginManager = $this->getMockBuilder('Zend\I18n\Translator\LoaderPluginManager')
->disableOriginalConstructor()
->getMock();

$translator = $this->getMockBuilder('Zend\I18n\Translator\Translator')
->disableOriginalConstructor()
->getMock();

$translator->expects($this->once())->method('getPluginManager')->willReturn($loaderPluginManager);
$loaderPluginManager->expects($this->once())->method('setServiceLocator')->with($serviceLocator);

$loaderPluginManager
->expects($this->once())
->method('setFactory')
->with(
'BsbDoctrineTranslationLoader',
'BsbDoctrineTranslationLoader\I18n\Translator\Loader\Factory\DoctrineLoaderFactory'
);

$callback = function () use ($translator) {
return $translator;
};

$delegator = new MvcTranslatorDelegatorFactory();

$delegator->createDelegatorWithName($serviceLocator, 'xxx', 'xxx', $callback);

}
}
Loading

0 comments on commit a803095

Please sign in to comment.