PhpConfigTest.php 4.5 KB

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