PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tests/Core/Config/FileLoaderTest.php

http://github.com/concrete5/concrete5
PHP | 144 lines | 102 code | 18 blank | 24 comment | 3 complexity | 3e0805672d1dd6f236b8ae1bf7e4aa11 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. use Concrete\Core\Config\FileLoader;
  3. use Concrete\Core\Config\Renderer;
  4. use Illuminate\Filesystem\Filesystem;
  5. /**
  6. * Class FileLoaderTest.
  7. *
  8. * Non-namespaced order:
  9. * /concrete/config/group.php
  10. * /application/config/generated_overrides/group.php
  11. * /application/config/group.php
  12. * /application/config/environment.group.php
  13. *
  14. * Namespaced order:
  15. * /path/to/namespace/group.php
  16. * /path/to/namespace/environment.group.php
  17. * /application/config/generated_overrides/namespace/group.php
  18. * /application/config/namespace/group.php
  19. * /application/config/namespace/environment.group.php
  20. */
  21. class FileLoaderTest extends PHPUnit_Framework_TestCase
  22. {
  23. /** @var FileLoader */
  24. protected $loader;
  25. /** @var Filesystem */
  26. protected $files;
  27. /** @var string */
  28. protected $group;
  29. /** @var string */
  30. protected $namespace;
  31. /** @var string */
  32. protected $environment;
  33. /** @var array */
  34. protected $to_remove = array();
  35. public function setUp()
  36. {
  37. $this->loader = new FileLoader($this->files = new Filesystem());
  38. $this->group = md5(time() . uniqid());
  39. $this->namespace = md5(time() . uniqid());
  40. $this->environment = md5(time() . uniqid());
  41. $path = DIR_APPLICATION . '/config/';
  42. $this->loader->addNamespace($this->namespace, $path . $this->namespace);
  43. $paths = array(
  44. // No Namespace
  45. "generated_overrides/{$this->group}.php" => array(
  46. 'non-namespaced' => true,
  47. 'override' => true,
  48. 'second' => false, // This isn't the second one
  49. ),
  50. "{$this->group}.php" => array(
  51. 'non-namespaced' => true,
  52. 'main_group' => true,
  53. 'second' => true, // This is the second one, nothing after this should override this value
  54. 'last' => false, // This isn't the last one
  55. ),
  56. "{$this->environment}.{$this->group}.php" => array(
  57. 'non-namespaced' => true,
  58. 'environment' => true,
  59. 'last' => true, // This is the last one, nothing should load after this.
  60. ),
  61. // Namespace
  62. "generated_overrides/{$this->namespace}/{$this->group}.php" => array(
  63. 'namespaced' => true,
  64. 'override' => true,
  65. 'second' => false, // This isn't the second one
  66. ),
  67. "{$this->namespace}/{$this->group}.php" => array(
  68. 'namespaced' => true,
  69. 'main_group' => true,
  70. 'second' => true, // This is the second one, nothing after this should override this value
  71. 'last' => false, // This isn't the last one
  72. ),
  73. "{$this->namespace}/{$this->environment}.{$this->group}.php" => array(
  74. 'namespaced' => true,
  75. 'environment' => true,
  76. 'last' => true, // This is the last one, nothing should load after this.
  77. ),
  78. );
  79. foreach ($paths as $relative_path => $array) {
  80. $split = explode('/', $relative_path);
  81. $current_path = $path;
  82. array_pop($split);
  83. foreach ($split as $directory) {
  84. $dir = "{$current_path}/{$directory}";
  85. if (!$this->files->exists($dir)) {
  86. $this->files->makeDirectory($dir);
  87. $this->to_remove[] = $dir;
  88. }
  89. $current_path = $dir;
  90. }
  91. $this->to_remove[] = $path . $relative_path;
  92. $this->files->put($path . $relative_path, id(new Renderer($array))->render());
  93. }
  94. }
  95. public function tearDown()
  96. {
  97. $remove = array_reverse($this->to_remove);
  98. foreach ($remove as $path) {
  99. if ($this->files->isDirectory($path)) {
  100. $this->files->deleteDirectory($path);
  101. } else {
  102. $this->files->delete($path);
  103. }
  104. }
  105. }
  106. public function testHierarchy()
  107. {
  108. $loaded = $this->loader->load($this->environment, $this->group);
  109. $this->assertTrue(array_get($loaded, 'override'), 'Override didn\'t load');
  110. $this->assertTrue(array_get($loaded, 'main_group'), 'Main group didn\'t load');
  111. $this->assertTrue(array_get($loaded, 'environment'), 'Environment didn\'t load');
  112. $this->assertTrue(array_get($loaded, 'second'), 'Second loaded out of order');
  113. $this->assertTrue(array_get($loaded, 'last'), 'Last loaded out of order');
  114. $this->assertNull(array_get($loaded, 'namespaced'), 'Loaded a namespaced file... that\'s wrong...');
  115. }
  116. public function testNamespaceHierarchy()
  117. {
  118. $loaded = $this->loader->load($this->environment, $this->group, $this->namespace);
  119. $this->assertTrue(array_get($loaded, 'override'), 'Override didn\'t load');
  120. $this->assertTrue(array_get($loaded, 'main_group'), 'Main group didn\'t load');
  121. $this->assertTrue(array_get($loaded, 'environment'), 'Environment didn\'t load');
  122. $this->assertTrue(array_get($loaded, 'second'), 'Second loaded out of order');
  123. $this->assertTrue(array_get($loaded, 'last'), 'Last loaded out of order');
  124. $this->assertNull(array_get($loaded, 'non-namespaced'), 'Loaded a namespaced file... that\'s wrong...');
  125. }
  126. }