ReaderTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Tests the Config file reader that's shipped with ko7
  4. *
  5. * @group ko7
  6. * @group ko7.config
  7. *
  8. * @package Unittest
  9. *
  10. * @author Jeremy Bush <contractfrombelow@gmail.com>
  11. * @author Matt Button <matthew@sigswitch.com>
  12. * @copyright (c) 2007-2012 Kohana Team
  13. * @copyright (c) 2016-2018 Koseven Team
  14. * @license https://koseven.dev/LICENSE
  15. */
  16. class KO7_Config_File_ReaderTest extends KO7_Unittest_TestCase {
  17. /**
  18. * If the config dir does not exist then the function should just
  19. * return an empty array
  20. *
  21. * @covers KO7_Config_File_Reader::load
  22. */
  23. public function test_load_returns_empty_array_if_conf_dir_dnx()
  24. {
  25. $config = new KO7_Config_File('gafloogle');
  26. self::assertSame([], $config->load('values'));
  27. }
  28. /**
  29. * If the requested config group does not exist then the reader
  30. * should return an empty array
  31. *
  32. * @covers KO7_Config_File_Reader::load
  33. */
  34. public function test_load_returns_empty_array_if_conf_dnx()
  35. {
  36. $config = new KO7_Config_File;
  37. self::assertSame([], $config->load('gafloogle'));
  38. }
  39. /**
  40. * Test that the load() function is actually loading the
  41. * configuration from the files.
  42. *
  43. * @dataProvider provider_configs
  44. *
  45. * @param array $configuration Configuration Details
  46. *
  47. * @covers KO7_Config_File_Reader::load
  48. * @covers KO7_Config_File_Reader::read_from_ob
  49. * @throws KO7_Exception
  50. */
  51. public function test_load_config_from_files($configuration)
  52. {
  53. if ( ! extension_loaded('yaml'))
  54. {
  55. self::markTestSkipped('PHP YAML required to execute this test.');
  56. }
  57. $config = new KO7_Config_File;
  58. // Generate Paths for Configuration Storage
  59. $path = APPPATH . 'config' . DIRECTORY_SEPARATOR;
  60. $json_file = $path.'test.json';
  61. $yaml_file = $path.'test2.yaml';
  62. // Generate Json
  63. $json = json_encode($configuration['value']);
  64. // Check if files are writable
  65. if ( ! touch($json_file) || ! touch($yaml_file))
  66. {
  67. self::fail('Could not write configuration files inside APPATH/config');
  68. }
  69. // Write json config, remove on error
  70. if (file_put_contents($json_file, $json) === FALSE)
  71. {
  72. unlink($json_file);
  73. self::fail('Could not write into config file ' . $json_file);
  74. }
  75. // Write yaml config, remove on error
  76. if ( ! yaml_emit_file($yaml_file, $configuration['value'], YAML_UTF8_ENCODING))
  77. {
  78. unlink($yaml_file);
  79. self::fail('Could not write into config file ' . $yaml_file);
  80. }
  81. // Check if values are like expected
  82. self::assertSame($config->load('test')['database']['host'], $configuration['expected']);
  83. self::assertSame($config->load('test2')['database']['host'], $configuration['expected'] . '...');
  84. // Remove both files
  85. unlink($json_file);
  86. unlink($yaml_file);
  87. // Test PHP Configs
  88. $values = $config->load('inflector');
  89. // Due to the way the cascading filesystem works there could be
  90. // any number of modifications to the system config in the
  91. // actual output. Therefore to increase compatability we just
  92. // check that we've got an array and that it's not empty
  93. self::assertNotSame([], $values);
  94. self::assertIsArray($values);
  95. }
  96. /**
  97. * Data Provider for Configurations
  98. * @return array
  99. */
  100. public function provider_configs() : array
  101. {
  102. return [
  103. [
  104. [
  105. 'value' => [
  106. 'database' => [
  107. 'host' => "<?php echo 'test1'; ?>"
  108. ]
  109. ],
  110. 'expected' => 'test1'
  111. ]
  112. ]
  113. ];
  114. }
  115. }