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

Commit

Permalink
Merge branch 'fix/i18n-view-helper-translate-text-domain' of https://…
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/View/Helper/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ class Translate extends AbstractTranslatorHelper
* @return string
* @throws Exception\RuntimeException
*/
public function __invoke($message, $textDomain = 'default', $locale = null)
public function __invoke($message, $textDomain = null, $locale = null)
{
$translator = $this->getTranslator();
if (null === $translator) {
throw new Exception\RuntimeException('Translator has not been set');
}
if (null === $textDomain) {
$textDomain = $this->getTranslatorTextDomain();
}
return $translator->translate($message, $textDomain, $locale);
}
}
5 changes: 4 additions & 1 deletion src/View/Helper/TranslatePlural.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ public function __invoke(
$singular,
$plural,
$number,
$textDomain = 'default',
$textDomain = null,
$locale = null
)
{
$translator = $this->getTranslator();
if (null === $translator) {
throw new Exception\RuntimeException('Translator has not been set');
}
if (null === $textDomain) {
$textDomain = $this->getTranslatorTextDomain();
}
return $translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
}
}
108 changes: 108 additions & 0 deletions test/View/Helper/TranslatePluralTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace ZendTest\I18n\View\Helper;

use Zend\I18n\View\Helper\TranslatePlural as TranslatePluralHelper;

/**
* @category Zend
* @package Zend_View
* @subpackage UnitTests
* @group Zend_View
* @group Zend_View_Helper
*/
class TranslatePluralTest extends \PHPUnit_Framework_TestCase
{
/**
* @var TranslatePluralHelper
*/
public $helper;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
$this->helper = new TranslatePluralHelper();
}

/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @return void
*/
public function tearDown()
{
unset($this->helper);
}

public function testInvokingWithoutTranslatorWillRaiseException()
{
$this->setExpectedException('Zend\I18n\Exception\RuntimeException');
$this->helper->__invoke('singular', 'plural', 1);
}

public function testDefaultInvokeArguments()
{
$singularInput = 'singular';
$pluralInput = 'plural';
$numberInput = 1;
$expected = 'translated';

$translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
$translatorMock->expects($this->once())
->method('translatePlural')
->with(
$this->equalTo($singularInput),
$this->equalTo($pluralInput),
$this->equalTo($numberInput),
$this->equalTo('default'),
$this->equalTo(null)
)
->will($this->returnValue($expected));

$this->helper->setTranslator($translatorMock);

$this->assertEquals($expected, $this->helper->__invoke($singularInput, $pluralInput, $numberInput));
}

public function testCustomInvokeArguments()
{
$singularInput = 'singular';
$pluralInput = 'plural';
$numberInput = 1;
$expected = 'translated';
$textDomain = 'textDomain';
$locale = 'en_US';

$translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
$translatorMock->expects($this->once())
->method('translatePlural')
->with(
$this->equalTo($singularInput),
$this->equalTo($pluralInput),
$this->equalTo($numberInput),
$this->equalTo($textDomain),
$this->equalTo($locale)
)
->will($this->returnValue($expected));

$this->helper->setTranslator($translatorMock);

$this->assertEquals($expected, $this->helper->__invoke(
$singularInput, $pluralInput, $numberInput, $textDomain, $locale
));
}
}
90 changes: 90 additions & 0 deletions test/View/Helper/TranslateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace ZendTest\I18n\View\Helper;

use Zend\I18n\View\Helper\Translate as TranslateHelper;

/**
* @category Zend
* @package Zend_View
* @subpackage UnitTests
* @group Zend_View
* @group Zend_View_Helper
*/
class TranslateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var TranslateHelper
*/
public $helper;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
$this->helper = new TranslateHelper();
}

/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @return void
*/
public function tearDown()
{
unset($this->helper);
}

public function testInvokingWithoutTranslatorWillRaiseException()
{
$this->setExpectedException('Zend\I18n\Exception\RuntimeException');
$this->helper->__invoke('message');
}

public function testDefaultInvokeArguments()
{
$input = 'input';
$expected = 'translated';

$translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
$translatorMock->expects($this->once())
->method('translate')
->with($this->equalTo($input), $this->equalTo('default'), $this->equalTo(null))
->will($this->returnValue($expected));

$this->helper->setTranslator($translatorMock);

$this->assertEquals($expected, $this->helper->__invoke($input));
}

public function testCustomInvokeArguments()
{
$input = 'input';
$expected = 'translated';
$textDomain = 'textDomain';
$locale = 'en_US';

$translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
$translatorMock->expects($this->once())
->method('translate')
->with($this->equalTo($input), $this->equalTo($textDomain), $this->equalTo($locale))
->will($this->returnValue($expected));

$this->helper->setTranslator($translatorMock);

$this->assertEquals($expected, $this->helper->__invoke($input, $textDomain, $locale));
}
}

0 comments on commit ef20fa1

Please # to comment.