PhpConfigTest.php 4.2 KB

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