4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\Tests\ResourceBundle\Reader
;
14 use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReader
;
17 * @author Bernhard Schussek <bschussek@gmail.com>
19 class StructuredBundleReaderTest
extends \PHPUnit_Framework_TestCase
21 const RES_DIR
= '/res/dir';
24 * @var StructuredBundleReader
29 * @var \PHPUnit_Framework_MockObject_MockObject
33 protected function setUp()
35 $this->readerImpl
= $this->getMock('Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface');
36 $this->reader
= new StructuredBundleReader($this->readerImpl
);
39 public function testGetLocales()
41 $locales = array('en', 'de', 'fr');
43 $this->readerImpl
->expects($this->once())
44 ->method('getLocales')
46 ->will($this->returnValue($locales));
48 $this->assertSame($locales, $this->reader
->getLocales(self
::RES_DIR
));
51 public function testRead()
53 $data = array('foo', 'bar');
55 $this->readerImpl
->expects($this->once())
57 ->with(self
::RES_DIR
, 'en')
58 ->will($this->returnValue($data));
60 $this->assertSame($data, $this->reader
->read(self
::RES_DIR
, 'en'));
63 public function testReadEntryNoParams()
65 $data = array('foo', 'bar');
67 $this->readerImpl
->expects($this->once())
69 ->with(self
::RES_DIR
, 'en')
70 ->will($this->returnValue($data));
72 $this->assertSame($data, $this->reader
->readEntry(self
::RES_DIR
, 'en', array()));
75 public function testReadEntryWithParam()
77 $data = array('Foo' => array('Bar' => 'Baz'));
79 $this->readerImpl
->expects($this->once())
81 ->with(self
::RES_DIR
, 'en')
82 ->will($this->returnValue($data));
84 $this->assertSame('Baz', $this->reader
->readEntry(self
::RES_DIR
, 'en', array('Foo', 'Bar')));
87 public function testReadEntryWithUnresolvablePath()
89 $data = array('Foo' => 'Baz');
91 $this->readerImpl
->expects($this->once())
93 ->with(self
::RES_DIR
, 'en')
94 ->will($this->returnValue($data));
96 $this->assertNull($this->reader
->readEntry(self
::RES_DIR
, 'en', array('Foo', 'Bar')));
99 public function readMergedEntryProvider()
102 array('foo', null, 'foo'),
103 array(null, 'foo', 'foo'),
104 array(array('foo', 'bar'), null, array('foo', 'bar')),
105 array(array('foo', 'bar'), array(), array('foo', 'bar')),
106 array(null, array('baz'), array('baz')),
107 array(array(), array('baz'), array('baz')),
108 array(array('foo', 'bar'), array('baz'), array('baz', 'foo', 'bar')),
113 * @dataProvider readMergedEntryProvider
115 public function testReadMergedEntryNoParams($childData, $parentData, $result)
117 $this->readerImpl
->expects($this->at(0))
119 ->with(self
::RES_DIR
, 'en_GB')
120 ->will($this->returnValue($childData));
122 if (null === $childData || is_array($childData)) {
123 $this->readerImpl
->expects($this->at(1))
125 ->with(self
::RES_DIR
, 'en')
126 ->will($this->returnValue($parentData));
129 $this->assertSame($result, $this->reader
->readEntry(self
::RES_DIR
, 'en_GB', array(), true));
133 * @dataProvider readMergedEntryProvider
135 public function testReadMergedEntryWithParams($childData, $parentData, $result)
137 $this->readerImpl
->expects($this->at(0))
139 ->with(self
::RES_DIR
, 'en_GB')
140 ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
142 if (null === $childData || is_array($childData)) {
143 $this->readerImpl
->expects($this->at(1))
145 ->with(self
::RES_DIR
, 'en')
146 ->will($this->returnValue(array('Foo' => array('Bar' => $parentData))));
149 $this->assertSame($result, $this->reader
->readEntry(self
::RES_DIR
, 'en_GB', array('Foo', 'Bar'), true));
152 public function testReadMergedEntryWithUnresolvablePath()
154 $this->readerImpl
->expects($this->at(0))
156 ->with(self
::RES_DIR
, 'en_GB')
157 ->will($this->returnValue(array('Foo' => 'Baz')));
159 $this->readerImpl
->expects($this->at(1))
161 ->with(self
::RES_DIR
, 'en')
162 ->will($this->returnValue(array('Foo' => 'Bar')));
164 $this->assertNull($this->reader
->readEntry(self
::RES_DIR
, 'en_GB', array('Foo', 'Bar'), true));
167 public function testReadMergedEntryWithUnresolvablePathInParent()
169 $this->readerImpl
->expects($this->at(0))
171 ->with(self
::RES_DIR
, 'en_GB')
172 ->will($this->returnValue(array('Foo' => array('Bar' => array('three')))));
174 $this->readerImpl
->expects($this->at(1))
176 ->with(self
::RES_DIR
, 'en')
177 ->will($this->returnValue(array('Foo' => 'Bar')));
179 $result = array('three');
181 $this->assertSame($result, $this->reader
->readEntry(self
::RES_DIR
, 'en_GB', array('Foo', 'Bar'), true));
184 public function testReadMergedEntryWithUnresolvablePathInChild()
186 $this->readerImpl
->expects($this->at(0))
188 ->with(self
::RES_DIR
, 'en_GB')
189 ->will($this->returnValue(array('Foo' => 'Baz')));
191 $this->readerImpl
->expects($this->at(1))
193 ->with(self
::RES_DIR
, 'en')
194 ->will($this->returnValue(array('Foo' => array('Bar' => array('one', 'two')))));
196 $result = array('one', 'two');
198 $this->assertSame($result, $this->reader
->readEntry(self
::RES_DIR
, 'en_GB', array('Foo', 'Bar'), true));
202 * @dataProvider readMergedEntryProvider
204 public function testReadMergedEntryWithTraversables($childData, $parentData, $result)
206 $parentData = is_array($parentData) ? new \
ArrayObject($parentData) : $parentData;
207 $childData = is_array($childData) ? new \
ArrayObject($childData) : $childData;
209 $this->readerImpl
->expects($this->at(0))
211 ->with(self
::RES_DIR
, 'en_GB')
212 ->will($this->returnValue(array('Foo' => array('Bar' => $childData))));
214 if (null === $childData || $childData instanceof \ArrayObject
) {
215 $this->readerImpl
->expects($this->at(1))
217 ->with(self
::RES_DIR
, 'en')
218 ->will($this->returnValue(array('Foo' => array('Bar' => $parentData))));
221 $this->assertSame($result, $this->reader
->readEntry(self
::RES_DIR
, 'en_GB', array('Foo', 'Bar'), true));