CatalogTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\g11n;
  9. use lithium\g11n\Catalog;
  10. use lithium\g11n\catalog\adapter\Memory;
  11. class CatalogTest extends \lithium\test\Unit {
  12. protected $_backup = array();
  13. public function setUp() {
  14. $this->_backup['catalogConfig'] = Catalog::config();
  15. Catalog::reset();
  16. Catalog::config(array(
  17. 'runtime' => array('adapter' => new Memory())
  18. ));
  19. }
  20. public function tearDown() {
  21. Catalog::reset();
  22. Catalog::config($this->_backup['catalogConfig']);
  23. }
  24. /**
  25. * Tests for values returned by `read()`.
  26. *
  27. * @return void
  28. */
  29. public function testRead() {
  30. $result = Catalog::read('runtime', 'validation.ssn', 'de_DE');
  31. $this->assertNull($result);
  32. }
  33. /**
  34. * Tests for values returned by `write()`.
  35. *
  36. * @return void
  37. */
  38. public function testWrite() {
  39. $data = array(
  40. 'DKK' => 'Dänische Krone'
  41. );
  42. $result = Catalog::write('runtime', 'currency', 'de_DE', $data);
  43. $this->assertTrue($result);
  44. }
  45. /**
  46. * Tests writing and reading for single and multiple items.
  47. *
  48. * @return void
  49. */
  50. public function testWriteRead() {
  51. $data = '/postalCode en_US/';
  52. Catalog::write('runtime', 'validation.postalCode', 'en_US', $data);
  53. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US');
  54. $this->assertEqual($data, $result);
  55. $this->tearDown();
  56. $this->setUp();
  57. $data = array(
  58. 'GRD' => 'Griechische Drachme',
  59. 'DKK' => 'Dänische Krone'
  60. );
  61. Catalog::write('runtime', 'currency', 'de', $data);
  62. $result = Catalog::read('runtime', 'currency', 'de');
  63. $this->assertEqual($data, $result);
  64. }
  65. /**
  66. * Tests writing and reading with data merged between cascaded locales.
  67. *
  68. * Only complete items are merged in, (atomic) merging between items
  69. * should not occur. Categories fall back to results for more generic locales.
  70. *
  71. * @return void
  72. */
  73. public function testWriteReadMergeLocales() {
  74. $data = '/postalCode en/';
  75. Catalog::write('runtime', 'validation.postalCode', 'en', $data);
  76. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US');
  77. $expected = '/postalCode en/';
  78. $this->assertEqual($expected, $result);
  79. $this->tearDown();
  80. $this->setUp();
  81. $data = '/postalCode en_US/';
  82. Catalog::write('runtime', 'validation.postalCode', 'en_US', $data);
  83. $data = '/postalCode en/';
  84. Catalog::write('runtime', 'validation.postalCode', 'en', $data);
  85. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US');
  86. $expected = '/postalCode en_US/';
  87. $this->assertEqual($expected, $result);
  88. $this->tearDown();
  89. $this->setUp();
  90. $data = array('a' => true, 'b' => true, 'c' => true);
  91. Catalog::write('runtime', 'language', 'en', $data);
  92. $result = Catalog::read('runtime', 'language', 'en_US');
  93. $expected = array('a' => true, 'b' => true, 'c' => true);
  94. $this->assertEqual($expected, $result);
  95. $this->tearDown();
  96. $this->setUp();
  97. $data = array(
  98. 'DKK' => 'Dänische Krone'
  99. );
  100. Catalog::write('runtime', 'currency', 'de', $data);
  101. $data = array(
  102. 'GRD' => 'Griechische Drachme'
  103. );
  104. Catalog::write('runtime', 'currency', 'de_CH', $data);
  105. $result = Catalog::read('runtime', 'currency', 'de_CH');
  106. $expected = array(
  107. 'GRD' => 'Griechische Drachme',
  108. 'DKK' => 'Dänische Krone'
  109. );
  110. $this->assertEqual($expected, $result);
  111. $this->tearDown();
  112. $this->setUp();
  113. $data = array(
  114. 'GRD' => 'de Griechische Drachme',
  115. 'DKK' => 'de Dänische Krone'
  116. );
  117. Catalog::write('runtime', 'currency', 'de', $data);
  118. $data = array(
  119. 'GRD' => 'de_CH Griechische Drachme'
  120. );
  121. Catalog::write('runtime', 'currency', 'de_CH', $data);
  122. $result = Catalog::read('runtime', 'currency', 'de_CH');
  123. $expected = array(
  124. 'GRD' => 'de_CH Griechische Drachme',
  125. 'DKK' => 'de Dänische Krone'
  126. );
  127. $this->assertEqual($expected, $result);
  128. }
  129. /**
  130. * Tests that a scope is honored if one is used.
  131. *
  132. * @return void
  133. */
  134. public function testWriteReadWithScope() {
  135. $data = '/postalCode en_US scope0/';
  136. Catalog::write('runtime', 'validation.postalCode', 'en_US', $data, array(
  137. 'scope' => 'scope0'
  138. ));
  139. $data = '/postalCode en_US scope1/';
  140. Catalog::write('runtime', 'validation.postalCode', 'en_US', $data, array(
  141. 'scope' => 'scope1'
  142. ));
  143. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US');
  144. $this->assertNull($result);
  145. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US', array(
  146. 'scope' => 'scope0'
  147. ));
  148. $expected = '/postalCode en_US scope0/';
  149. $this->assertEqual($expected, $result);
  150. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US', array(
  151. 'scope' => 'scope1'
  152. ));
  153. $expected = '/postalCode en_US scope1/';
  154. $this->assertEqual($expected, $result);
  155. $data = '/postalCode en_US/';
  156. Catalog::write('runtime', 'validation.postalCode', 'en_US', $data);
  157. $result = Catalog::read('runtime', 'validation.postalCode', 'en_US');
  158. $expected = '/postalCode en_US/';
  159. $this->assertEqual($expected, $result);
  160. }
  161. /**
  162. * Tests reading from all configured stores with fallbacks.
  163. *
  164. * @return void
  165. */
  166. public function testWriteReadMergeAllConfigurations() {
  167. Catalog::reset();
  168. Catalog::config(array(
  169. 'runtime0' => array('adapter' => new Memory()),
  170. 'runtime1' => array('adapter' => new Memory())
  171. ));
  172. $data = '/postalCode en0/';
  173. Catalog::write('runtime0', 'validation.postalCode', 'en', $data);
  174. $data = '/postalCode en_US1/';
  175. Catalog::write('runtime1', 'validation.postalCode', 'en_US', $data);
  176. $data = '/postalCode en1/';
  177. Catalog::write('runtime1', 'validation.postalCode', 'en', $data);
  178. $result = Catalog::read(true, 'validation.postalCode', 'en_US');
  179. $expected = '/postalCode en_US1/';
  180. $this->assertEqual($expected, $result);
  181. Catalog::reset();
  182. Catalog::config(array(
  183. 'runtime0' => array('adapter' => new Memory()),
  184. 'runtime1' => array('adapter' => new Memory())
  185. ));
  186. $data = array(
  187. 'GRD' => 'de0 Griechische Drachme',
  188. 'DKK' => 'de0 Dänische Krone'
  189. );
  190. Catalog::write('runtime0', 'currency', 'de', $data);
  191. $data = array(
  192. 'GRD' => 'de1 Griechische Drachme'
  193. );
  194. Catalog::write('runtime1', 'currency', 'de', $data);
  195. $data = array(
  196. 'GRD' => 'de_CH1 Griechische Drachme'
  197. );
  198. Catalog::write('runtime1', 'currency', 'de_CH', $data);
  199. $result = Catalog::read(true, 'currency', 'de_CH');
  200. $expected = array(
  201. 'GRD' => 'de_CH1 Griechische Drachme',
  202. 'DKK' => 'de0 Dänische Krone'
  203. );
  204. $this->assertEqual($expected, $result);
  205. }
  206. /**
  207. * Tests reading from selected multiple configured stores.
  208. *
  209. * @return void
  210. */
  211. public function testReadMergeSelectedConfigurations() {
  212. Catalog::reset();
  213. Catalog::config(array(
  214. 'runtime0' => array('adapter' => new Memory()),
  215. 'runtime1' => array('adapter' => new Memory()),
  216. 'runtime2' => array('adapter' => new Memory())
  217. ));
  218. $data = '/postalCode en0/';
  219. Catalog::write('runtime0', 'validation.postalCode', 'en', $data);
  220. $data = '/postalCode en1/';
  221. Catalog::write('runtime1', 'validation.postalCode', 'en', $data);
  222. $data = '/postalCode en2/';
  223. Catalog::write('runtime2', 'validation.postalCode', 'en', $data);
  224. $data = '/ssn en2/';
  225. Catalog::write('runtime2', 'validation.ssn', 'en', $data);
  226. $result = Catalog::read('runtime0', 'validation.postalCode', 'en');
  227. $expected = '/postalCode en0/';
  228. $this->assertEqual($expected, $result);
  229. $result = Catalog::read('runtime2', 'validation.postalCode', 'en');
  230. $expected = '/postalCode en2/';
  231. $this->assertEqual($expected, $result);
  232. $result = Catalog::read('runtime2', 'validation.postalCode', 'en');
  233. $expected = '/postalCode en2/';
  234. $this->assertEqual($expected, $result);
  235. $result = Catalog::read(array('runtime0', 'runtime2'), 'validation', 'en');
  236. $expected = array(
  237. 'postalCode' => '/postalCode en0/',
  238. 'ssn' => '/ssn en2/'
  239. );
  240. $this->assertEqual($expected, $result);
  241. $resultA = Catalog::read(array('runtime0', 'runtime2'), 'validation', 'en');
  242. $resultB = Catalog::read(true, 'validation', 'en');
  243. $this->assertEqual($resultA, $resultB);
  244. }
  245. /**
  246. * Tests writing, then reading different types of values.
  247. *
  248. * @return void
  249. */
  250. public function testDataTypeSupport() {
  251. $data = function($n) { return $n === 1 ? 0 : 1; };
  252. Catalog::write('runtime', 'message.pluralRule', 'en', $data);
  253. $result = Catalog::read('runtime', 'message.pluralRule', 'en');
  254. $this->assertEqual($data, $result);
  255. $data = array('fish', 'fishes');
  256. Catalog::write('runtime', 'message.fish', 'en', $data);
  257. $result = Catalog::read('runtime', 'message.fish', 'en');
  258. $this->assertEqual($data, $result);
  259. }
  260. /**
  261. * Tests if the output is normalized and doesn't depend on the input format.
  262. *
  263. * @return void
  264. */
  265. public function testInputFormatNormalization() {
  266. $data = array('house' => 'Haus');
  267. Catalog::write('runtime', 'message', 'de', $data);
  268. $result = Catalog::read('runtime', 'message', 'de', array('lossy' => false));
  269. $expected = array('house' => array(
  270. 'id' => 'house',
  271. 'ids' => array(),
  272. 'translated' => 'Haus',
  273. 'flags' => array(),
  274. 'comments' => array(),
  275. 'occurrences' => array()
  276. ));
  277. $this->assertEqual($expected, $result);
  278. $data = array('house' => array(
  279. 'id' => 'house',
  280. 'ids' => array(),
  281. 'translated' => 'Haus',
  282. 'flags' => array(),
  283. 'comments' => array(),
  284. 'occurrences' => array()
  285. ));
  286. Catalog::write('runtime', 'message', 'de', $data);
  287. $result = Catalog::read('runtime', 'message', 'de', array('lossy' => false));
  288. $expected = array('house' => array(
  289. 'id' => 'house',
  290. 'ids' => array(),
  291. 'translated' => 'Haus',
  292. 'flags' => array(),
  293. 'comments' => array(),
  294. 'occurrences' => array()
  295. ));
  296. $this->assertEqual($expected, $result);
  297. }
  298. public function testOutputLossyFormat() {
  299. $data = array('house' => array(
  300. 'id' => 'house',
  301. 'ids' => array('singular' => 'house'),
  302. 'translated' => 'Haus',
  303. 'flags' => array(),
  304. 'comments' => array(),
  305. 'occurrences' => array()
  306. ));
  307. Catalog::write('runtime', 'message', 'de', $data);
  308. $result = Catalog::read('runtime', 'message', 'de');
  309. $expected = array('house' => 'Haus');
  310. $this->assertEqual($expected, $result);
  311. }
  312. public function testOutputLosslessFormat() {
  313. $data = array('house' => array(
  314. 'id' => 'house',
  315. 'ids' => array('singular' => 'house'),
  316. 'translated' => 'Haus',
  317. 'flags' => array(),
  318. 'comments' => array(),
  319. 'occurrences' => array()
  320. ));
  321. Catalog::write('runtime', 'message', 'de', $data);
  322. $result = Catalog::read('runtime', 'message', 'de', array('lossy' => false));
  323. $expected = array('house' => array(
  324. 'id' => 'house',
  325. 'ids' => array('singular' => 'house'),
  326. 'translated' => 'Haus',
  327. 'flags' => array(),
  328. 'comments' => array(),
  329. 'occurrences' => array()
  330. ));
  331. $this->assertEqual($expected, $result);
  332. }
  333. public function testInvalidWrite() {
  334. Catalog::reset();
  335. $data = array('house' => array('id' => 'house'));
  336. $this->expectException("Configuration `runtime` has not been defined.");
  337. $this->assertFalse(Catalog::write('runtime', 'message', 'de', $data));
  338. }
  339. }
  340. ?>