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

Commit

Permalink
Update unit tests to use prophecy
Browse files Browse the repository at this point in the history
Not all versions of PHPUnit are compatible with PHP 7.4, so we are using
prophecy for mocking.
  • Loading branch information
michalbundyra committed Dec 12, 2019
1 parent efd81bb commit f433f75
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 59 deletions.
33 changes: 10 additions & 23 deletions test/Translator/TranslatorServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,27 @@ class TranslatorServiceFactoryTest extends TestCase
{
public function testCreateServiceWithNoTranslatorKeyDefined()
{
$pluginManagerMock = $this->createMock(LoaderPluginManager::class);
$slContents = [
['config', []],
['TranslatorPluginManager', $pluginManagerMock]
];
$pluginManagerMock = $this->prophesize(LoaderPluginManager::class)->reveal();

$serviceLocator = $this->createMock(ContainerInterface::class);
$serviceLocator
->expects($this->once())
->method('has')
->with($this->equalTo('TranslatorPluginManager'))
->willReturn(true);
$serviceLocator
->expects($this->exactly(2))
->method('get')
->willReturnMap($slContents);
$serviceLocator = $this->prophesize(ContainerInterface::class);
$serviceLocator->has('TranslatorPluginManager')->willReturn(true)->shouldBeCalledTimes(1);
$serviceLocator->get('TranslatorPluginManager')->willReturn($pluginManagerMock)->shouldBeCalledTimes(1);
$serviceLocator->get('config')->willReturn([])->shouldBeCalledTimes(1);

$factory = new TranslatorServiceFactory();
$translator = $factory($serviceLocator, Translator::class);
$translator = $factory($serviceLocator->reveal(), Translator::class);
$this->assertInstanceOf(Translator::class, $translator);
$this->assertSame($pluginManagerMock, $translator->getPluginManager());
}

public function testCreateServiceWithNoTranslatorPluginManagerDefined()
{
$serviceLocator = $this->createMock(ContainerInterface::class);
$serviceLocator
->expects($this->once())
->method('get')
->with($this->equalTo('config'))
->willReturn([]);
$serviceLocator = $this->prophesize(ContainerInterface::class);
$serviceLocator->has('TranslatorPluginManager')->willReturn(false)->shouldBeCalledTimes(1);
$serviceLocator->get('config')->willReturn([])->shouldBeCalledTimes(1);

$factory = new TranslatorServiceFactory();
$translator = $factory($serviceLocator, Translator::class);
$translator = $factory($serviceLocator->reveal(), Translator::class);
$this->assertInstanceOf(Translator::class, $translator);
}
}
35 changes: 11 additions & 24 deletions test/View/Helper/TranslatePluralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use PHPUnit\Framework\TestCase;
use Zend\I18n\View\Helper\TranslatePlural as TranslatePluralHelper;
use Zend\I18n\Translator\Translator;

/**
* @group Zend_View
Expand Down Expand Up @@ -56,19 +57,12 @@ public function testDefaultInvokeArguments()
$numberInput = 1;
$expected = 'translated';

$translatorMock = $this->createMock('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)
)
->willReturn($expected);
$translatorMock = $this->prophesize(Translator::class);
$translatorMock->translatePlural($singularInput, $pluralInput, $numberInput, 'default', null)
->willReturn($expected)
->shouldBeCalledTimes(1);

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

$this->assertEquals($expected, $this->helper->__invoke($singularInput, $pluralInput, $numberInput));
}
Expand All @@ -82,19 +76,12 @@ public function testCustomInvokeArguments()
$textDomain = 'textDomain';
$locale = 'en_US';

$translatorMock = $this->createMock('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)
)
->willReturn($expected);
$translatorMock = $this->prophesize(Translator::class);
$translatorMock->translatePlural($singularInput, $pluralInput, $numberInput, $textDomain, $locale)
->willReturn($expected)
->shouldBeCalledTimes(1);

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

$this->assertEquals($expected, $this->helper->__invoke(
$singularInput,
Expand Down
23 changes: 11 additions & 12 deletions test/View/Helper/TranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use PHPUnit\Framework\TestCase;
use Zend\I18n\View\Helper\Translate as TranslateHelper;
use Zend\I18n\Translator\Translator;

/**
* @group Zend_View
Expand Down Expand Up @@ -54,13 +55,12 @@ public function testDefaultInvokeArguments()
$input = 'input';
$expected = 'translated';

$translatorMock = $this->createMock('Zend\I18n\Translator\Translator');
$translatorMock->expects($this->once())
->method('translate')
->with($this->equalTo($input), $this->equalTo('default'), $this->equalTo(null))
->willReturn($expected);
$translatorMock = $this->prophesize(Translator::class);
$translatorMock->translate($input, 'default', null)
->willReturn($expected)
->shouldBeCalledTimes(1);

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

$this->assertEquals($expected, $this->helper->__invoke($input));
}
Expand All @@ -72,13 +72,12 @@ public function testCustomInvokeArguments()
$textDomain = 'textDomain';
$locale = 'en_US';

$translatorMock = $this->createMock('Zend\I18n\Translator\Translator');
$translatorMock->expects($this->once())
->method('translate')
->with($this->equalTo($input), $this->equalTo($textDomain), $this->equalTo($locale))
->willReturn($expected);
$translatorMock = $this->prophesize(Translator::class);
$translatorMock->translate($input, $textDomain, $locale)
->willReturn($expected)
->shouldBeCalledTimes(1);

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

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

0 comments on commit f433f75

Please sign in to comment.