PhpConfigTest.php 4.2 KB

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