PhpReaderTest.php 4.2 KB

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