123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Intl\Tests\Data\Bundle\Reader;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\Intl\Exception\ResourceBundleNotFoundException;
- use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
- /**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class BundleEntryReaderTest extends TestCase
- {
- const RES_DIR = '/res/dir';
- /**
- * @var BundleEntryReader
- */
- private $reader;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $readerImpl;
- private static $data = array(
- 'Entries' => array(
- 'Foo' => 'Bar',
- 'Bar' => 'Baz',
- ),
- 'Foo' => 'Bar',
- 'Version' => '2.0',
- );
- private static $fallbackData = array(
- 'Entries' => array(
- 'Foo' => 'Foo',
- 'Bam' => 'Lah',
- ),
- 'Baz' => 'Foo',
- 'Version' => '1.0',
- );
- private static $mergedData = array(
- // no recursive merging -> too complicated
- 'Entries' => array(
- 'Foo' => 'Bar',
- 'Bar' => 'Baz',
- ),
- 'Baz' => 'Foo',
- 'Version' => '2.0',
- 'Foo' => 'Bar',
- );
- protected function setUp()
- {
- $this->readerImpl = $this->getMockBuilder('Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface')->getMock();
- $this->reader = new BundleEntryReader($this->readerImpl);
- }
- public function testForwardCallToRead()
- {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'root')
- ->will($this->returnValue(self::$data));
- $this->assertSame(self::$data, $this->reader->read(self::RES_DIR, 'root'));
- }
- public function testReadEntireDataFileIfNoIndicesGiven()
- {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(self::$data));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'root')
- ->will($this->returnValue(self::$fallbackData));
- $this->assertSame(self::$mergedData, $this->reader->readEntry(self::RES_DIR, 'en', array()));
- }
- public function testReadExistingEntry()
- {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'root')
- ->will($this->returnValue(self::$data));
- $this->assertSame('Bar', $this->reader->readEntry(self::RES_DIR, 'root', array('Entries', 'Foo')));
- }
- /**
- * @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
- */
- public function testReadNonExistingEntry()
- {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'root')
- ->will($this->returnValue(self::$data));
- $this->reader->readEntry(self::RES_DIR, 'root', array('Entries', 'NonExisting'));
- }
- public function testFallbackIfEntryDoesNotExist()
- {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(self::$data));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(self::$fallbackData));
- $this->assertSame('Lah', $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Entries', 'Bam')));
- }
- /**
- * @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
- */
- public function testDontFallbackIfEntryDoesNotExistAndFallbackDisabled()
- {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(self::$data));
- $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Entries', 'Bam'), false);
- }
- public function testFallbackIfLocaleDoesNotExist()
- {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->throwException(new ResourceBundleNotFoundException()));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(self::$fallbackData));
- $this->assertSame('Lah', $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Entries', 'Bam')));
- }
- /**
- * @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
- */
- public function testDontFallbackIfLocaleDoesNotExistAndFallbackDisabled()
- {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->throwException(new ResourceBundleNotFoundException()));
- $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Entries', 'Bam'), false);
- }
- public function provideMergeableValues()
- {
- return array(
- array('foo', null, 'foo'),
- array(null, 'foo', 'foo'),
- array(array('foo', 'bar'), null, array('foo', 'bar')),
- array(array('foo', 'bar'), array(), array('foo', 'bar')),
- array(null, array('baz'), array('baz')),
- array(array(), array('baz'), array('baz')),
- array(array('foo', 'bar'), array('baz'), array('baz', 'foo', 'bar')),
- );
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testMergeDataWithFallbackData($childData, $parentData, $result)
- {
- if (null === $childData || is_array($childData)) {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue($childData));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'root')
- ->will($this->returnValue($parentData));
- } else {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue($childData));
- }
- $this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'en', array(), true));
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testDontMergeDataIfFallbackDisabled($childData, $parentData, $result)
- {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue($childData));
- $this->assertSame($childData, $this->reader->readEntry(self::RES_DIR, 'en_GB', array(), false));
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testMergeExistingEntryWithExistingFallbackEntry($childData, $parentData, $result)
- {
- if (null === $childData || is_array($childData)) {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'root')
- ->will($this->returnValue(array('Foo' => array('Bar' => $parentData))));
- } else {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- }
- $this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'en', array('Foo', 'Bar'), true));
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testMergeNonExistingEntryWithExistingFallbackEntry($childData, $parentData, $result)
- {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(array('Foo' => 'Baz')));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(array('Foo' => array('Bar' => $parentData))));
- $this->assertSame($parentData, $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Foo', 'Bar'), true));
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testMergeExistingEntryWithNonExistingFallbackEntry($childData, $parentData, $result)
- {
- if (null === $childData || is_array($childData)) {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(array('Foo' => 'Bar')));
- } else {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- }
- $this->assertSame($childData, $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Foo', 'Bar'), true));
- }
- /**
- * @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
- */
- public function testFailIfEntryFoundNeitherInParentNorChild()
- {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(array('Foo' => 'Baz')));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(array('Foo' => 'Bar')));
- $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Foo', 'Bar'), true);
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testMergeTraversables($childData, $parentData, $result)
- {
- $parentData = is_array($parentData) ? new \ArrayObject($parentData) : $parentData;
- $childData = is_array($childData) ? new \ArrayObject($childData) : $childData;
- if (null === $childData || $childData instanceof \ArrayObject) {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'en')
- ->will($this->returnValue(array('Foo' => array('Bar' => $parentData))));
- } else {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'en_GB')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- }
- $this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'en_GB', array('Foo', 'Bar'), true));
- }
- /**
- * @dataProvider provideMergeableValues
- */
- public function testFollowLocaleAliases($childData, $parentData, $result)
- {
- $this->reader->setLocaleAliases(array('mo' => 'ro_MD'));
- if (null === $childData || is_array($childData)) {
- $this->readerImpl->expects($this->at(0))
- ->method('read')
- ->with(self::RES_DIR, 'ro_MD')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- // Read fallback locale of aliased locale ("ro_MD" -> "ro")
- $this->readerImpl->expects($this->at(1))
- ->method('read')
- ->with(self::RES_DIR, 'ro')
- ->will($this->returnValue(array('Foo' => array('Bar' => $parentData))));
- } else {
- $this->readerImpl->expects($this->once())
- ->method('read')
- ->with(self::RES_DIR, 'ro_MD')
- ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
- }
- $this->assertSame($result, $this->reader->readEntry(self::RES_DIR, 'mo', array('Foo', 'Bar'), true));
- }
- }
|