PhpConfigTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core\Configure\Engine;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure\Engine\PhpConfig;
  18. use Cake\Core\Plugin;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Class PhpConfigTest
  22. *
  23. */
  24. class PhpConfigTest extends TestCase
  25. {
  26. /**
  27. * Test data to serialize and unserialize.
  28. *
  29. * @var array
  30. */
  31. public $testData = [
  32. 'One' => [
  33. 'two' => 'value',
  34. 'three' => [
  35. 'four' => 'value four'
  36. ],
  37. 'is_null' => null,
  38. 'bool_false' => false,
  39. 'bool_true' => true,
  40. ],
  41. 'Asset' => [
  42. 'timestamp' => 'force'
  43. ],
  44. ];
  45. /**
  46. * Setup.
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. parent::setUp();
  53. $this->path = CONFIG;
  54. }
  55. /**
  56. * Test reading files.
  57. *
  58. * @return void
  59. */
  60. public function testRead()
  61. {
  62. $engine = new PhpConfig($this->path);
  63. $values = $engine->read('var_test');
  64. $this->assertEquals('value', $values['Read']);
  65. $this->assertEquals('buried', $values['Deep']['Deeper']['Deepest']);
  66. }
  67. /**
  68. * Test an exception is thrown by reading files that exist without .php extension.
  69. *
  70. * @expectedException \Cake\Core\Exception\Exception
  71. * @return void
  72. */
  73. public function testReadWithExistentFileWithoutExtension()
  74. {
  75. $engine = new PhpConfig($this->path);
  76. $engine->read('no_php_extension');
  77. }
  78. /**
  79. * Test an exception is thrown by reading files that don't exist.
  80. *
  81. * @expectedException \Cake\Core\Exception\Exception
  82. * @return void
  83. */
  84. public function testReadWithNonExistentFile()
  85. {
  86. $engine = new PhpConfig($this->path);
  87. $engine->read('fake_values');
  88. }
  89. /**
  90. * Test reading an empty file.
  91. *
  92. * @expectedException \Cake\Core\Exception\Exception
  93. * @return void
  94. */
  95. public function testReadEmptyFile()
  96. {
  97. $engine = new PhpConfig($this->path);
  98. $engine->read('empty');
  99. }
  100. /**
  101. * Test reading keys with ../ doesn't work.
  102. *
  103. * @expectedException \Cake\Core\Exception\Exception
  104. * @return void
  105. */
  106. public function testReadWithDots()
  107. {
  108. $engine = new PhpConfig($this->path);
  109. $engine->read('../empty');
  110. }
  111. /**
  112. * Test reading from plugins.
  113. *
  114. * @return void
  115. */
  116. public function testReadPluginValue()
  117. {
  118. Plugin::load('TestPlugin');
  119. $engine = new PhpConfig($this->path);
  120. $result = $engine->read('TestPlugin.load');
  121. $this->assertTrue(isset($result['plugin_load']));
  122. Plugin::unload();
  123. }
  124. /**
  125. * Test dumping data to PHP format.
  126. *
  127. * @return void
  128. */
  129. public function testDump()
  130. {
  131. $engine = new PhpConfig(TMP);
  132. $result = $engine->dump('test', $this->testData);
  133. $this->assertTrue($result > 0);
  134. $expected = <<<PHP
  135. <?php
  136. return array (
  137. 'One' =>
  138. array (
  139. 'two' => 'value',
  140. 'three' =>
  141. array (
  142. 'four' => 'value four',
  143. ),
  144. 'is_null' => NULL,
  145. 'bool_false' => false,
  146. 'bool_true' => true,
  147. ),
  148. 'Asset' =>
  149. array (
  150. 'timestamp' => 'force',
  151. ),
  152. );
  153. PHP;
  154. $file = TMP . 'test.php';
  155. $contents = file_get_contents($file);
  156. unlink($file);
  157. $this->assertTextEquals($expected, $contents);
  158. $result = $engine->dump('test', $this->testData);
  159. $this->assertTrue($result > 0);
  160. $contents = file_get_contents($file);
  161. $this->assertTextEquals($expected, $contents);
  162. unlink($file);
  163. }
  164. /**
  165. * Test that dump() makes files read() can read.
  166. *
  167. * @return void
  168. */
  169. public function testDumpRead()
  170. {
  171. $engine = new PhpConfig(TMP);
  172. $engine->dump('test', $this->testData);
  173. $result = $engine->read('test');
  174. unlink(TMP . 'test.php');
  175. $this->assertEquals($this->testData, $result);
  176. }
  177. }