PageRenderTime 1153ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/g11n/catalog/adapter/PhpTest.php

https://bitbucket.org/d1rk/lithium
PHP | 204 lines | 169 code | 29 blank | 6 comment | 0 complexity | 88f7b28a36be6751fd63dec9c2e7e322 MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, 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\catalog\adapter;
  9. use Exception;
  10. use lithium\core\Libraries;
  11. use lithium\g11n\catalog\adapter\Php;
  12. class PhpTest extends \lithium\test\Unit {
  13. public $adapter;
  14. protected $_path;
  15. public function skip() {
  16. $this->_path = $path = Libraries::get(true, 'resources') . '/tmp/tests';
  17. $this->skipIf(!is_writable($path), "Path `{$path}` is not writable.");
  18. }
  19. public function setUp() {
  20. $this->adapter = new Php(array('path' => $this->_path));
  21. }
  22. public function tearDown() {
  23. $this->_cleanUp();
  24. }
  25. public function testPathMustExist() {
  26. try {
  27. new Php(array('path' => $this->_path));
  28. $result = true;
  29. } catch (Exception $e) {
  30. $result = false;
  31. }
  32. $this->assert($result);
  33. try {
  34. new Php(array('path' => "{$this->_path}/i_do_not_exist"));
  35. $result = false;
  36. } catch (Exception $e) {
  37. $result = true;
  38. }
  39. $this->assert($result);
  40. }
  41. public function testRead() {
  42. mkdir("{$this->_path}/fr/message", 0755, true);
  43. $data = <<<EOD
  44. <?php
  45. return array(
  46. 'politics' => 'politique',
  47. 'house' => array('maison', 'maisons')
  48. );
  49. ?>
  50. EOD;
  51. file_put_contents("{$this->_path}/fr/message/default.php", $data);
  52. $result = $this->adapter->read('message', 'fr', null);
  53. $expected = array(
  54. 'politics' => array(
  55. 'id' => 'politics',
  56. 'ids' => array(),
  57. 'translated' => 'politique',
  58. 'flags' => array(),
  59. 'comments' => array(),
  60. 'occurrences' => array()
  61. ),
  62. 'house' => array(
  63. 'id' => 'house',
  64. 'ids' => array(),
  65. 'translated' => array('maison', 'maisons'),
  66. 'flags' => array(),
  67. 'comments' => array(),
  68. 'occurrences' => array()
  69. ));
  70. $this->assertEqual($expected, $result);
  71. }
  72. public function testReadTemplate() {
  73. $data = <<<EOD
  74. <?php
  75. return array(
  76. 'politics' => 'politique',
  77. 'house' => array('maison', 'maisons')
  78. );
  79. ?>
  80. EOD;
  81. file_put_contents("{$this->_path}/message_default.php", $data);
  82. $result = $this->adapter->read('messageTemplate', 'root', null);
  83. $expected = array(
  84. 'politics' => array(
  85. 'id' => 'politics',
  86. 'ids' => array(),
  87. 'translated' => 'politique',
  88. 'flags' => array(),
  89. 'comments' => array(),
  90. 'occurrences' => array()
  91. ),
  92. 'house' => array(
  93. 'id' => 'house',
  94. 'ids' => array(),
  95. 'translated' => array('maison', 'maisons'),
  96. 'flags' => array(),
  97. 'comments' => array(),
  98. 'occurrences' => array()
  99. ));
  100. $this->assertEqual($expected, $result);
  101. }
  102. public function testReadWithScope() {
  103. mkdir("{$this->_path}/fr/message", 0755, true);
  104. $data = <<<EOD
  105. <?php
  106. return array(
  107. 'politics' => 'politique'
  108. );
  109. ?>
  110. EOD;
  111. file_put_contents("{$this->_path}/fr/message/li3_docs.php", $data);
  112. $result = $this->adapter->read('message', 'fr', null);
  113. $this->assertFalse($result);
  114. $result = $this->adapter->read('message', 'fr', 'li3_docs');
  115. $expected = array(
  116. 'politics' => array(
  117. 'id' => 'politics',
  118. 'ids' => array(),
  119. 'translated' => 'politique',
  120. 'flags' => array(),
  121. 'comments' => array(),
  122. 'occurrences' => array()
  123. ));
  124. $this->assertEqual($expected, $result);
  125. }
  126. public function testReadValidation() {
  127. mkdir("{$this->_path}/fr/validation", 0755, true);
  128. $data = <<<EOD
  129. <?php
  130. return array(
  131. 'phone' => '/[0-9].*/i'
  132. );
  133. ?>
  134. EOD;
  135. file_put_contents("{$this->_path}/fr/validation/default.php", $data);
  136. $result = $this->adapter->read('validation', 'fr', null);
  137. $expected = array(
  138. 'phone' => array(
  139. 'id' => 'phone',
  140. 'ids' => array(),
  141. 'translated' => '/[0-9].*/i',
  142. 'flags' => array(),
  143. 'comments' => array(),
  144. 'occurrences' => array()
  145. ));
  146. $this->assertEqual($expected, $result);
  147. }
  148. public function testReadWithAnonymousFunction() {
  149. mkdir("{$this->_path}/fr/message", 0755, true);
  150. $data = <<<EOD
  151. <?php
  152. return array(
  153. 'plural' => function() { return 123; },
  154. 'politics' => 'politique',
  155. );
  156. ?>
  157. EOD;
  158. file_put_contents("{$this->_path}/fr/message/default.php", $data);
  159. $result = $this->adapter->read('message', 'fr', null);
  160. $expected = array(
  161. 'id' => 'politics',
  162. 'ids' => array(),
  163. 'translated' => 'politique',
  164. 'flags' => array(),
  165. 'comments' => array(),
  166. 'occurrences' => array()
  167. );
  168. $this->assertEqual($expected, $result['politics']);
  169. $this->assertTrue(is_callable($result['plural']['translated']));
  170. $expected = 123;
  171. $result = $result['plural']['translated']();
  172. $this->assertEqual($expected, $result);
  173. }
  174. }
  175. ?>