DeploymentConfigTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit;
  7. use \Magento\Framework\App\DeploymentConfig;
  8. use \Magento\Framework\Config\ConfigOptionsListConstants;
  9. class DeploymentConfigTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var array
  13. */
  14. private static $fixture = [
  15. 'configData1' => 'scalar_value',
  16. 'configData2' => [
  17. 'foo' => 1,
  18. 'bar' => ['baz' => 2],
  19. ],
  20. ];
  21. /**
  22. * @var array
  23. */
  24. private static $flattenedFixture = [
  25. 'configData1' => 'scalar_value',
  26. 'configData2' => [
  27. 'foo' => 1,
  28. 'bar' => ['baz' => 2],
  29. ],
  30. 'configData2/foo' => 1,
  31. 'configData2/bar' => ['baz' => 2],
  32. 'configData2/bar/baz' => 2,
  33. ];
  34. /**
  35. * @var array
  36. */
  37. protected static $fixtureConfig;
  38. /**
  39. * @var array
  40. */
  41. protected static $fixtureConfigMerged;
  42. /**
  43. * @var \Magento\Framework\App\DeploymentConfig
  44. */
  45. protected $_deploymentConfig;
  46. /**
  47. * @var \Magento\Framework\App\DeploymentConfig
  48. */
  49. protected $_deploymentConfigMerged;
  50. /**
  51. * @var \PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $reader;
  54. public static function setUpBeforeClass()
  55. {
  56. self::$fixtureConfig = require __DIR__ . '/_files/config.php';
  57. self::$fixtureConfigMerged = require __DIR__ . '/_files/other/local_developer_merged.php';
  58. }
  59. protected function setUp()
  60. {
  61. $this->reader = $this->createMock(\Magento\Framework\App\DeploymentConfig\Reader::class);
  62. $this->_deploymentConfig = new \Magento\Framework\App\DeploymentConfig($this->reader, []);
  63. $this->_deploymentConfigMerged = new \Magento\Framework\App\DeploymentConfig(
  64. $this->reader,
  65. require __DIR__ . '/_files/other/local_developer.php'
  66. );
  67. }
  68. public function testGetters()
  69. {
  70. $this->reader->expects($this->once())->method('load')->willReturn(self::$fixture);
  71. $this->assertSame(self::$flattenedFixture, $this->_deploymentConfig->get());
  72. // second time to ensure loader will be invoked only once
  73. $this->assertSame(self::$flattenedFixture, $this->_deploymentConfig->get());
  74. $this->assertSame('scalar_value', $this->_deploymentConfig->getConfigData('configData1'));
  75. $this->assertSame(self::$fixture['configData2'], $this->_deploymentConfig->getConfigData('configData2'));
  76. }
  77. public function testIsAvailable()
  78. {
  79. $this->reader->expects($this->once())->method('load')->willReturn([
  80. ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE => 1
  81. ]);
  82. $object = new DeploymentConfig($this->reader);
  83. $this->assertTrue($object->isAvailable());
  84. }
  85. public function testNotAvailable()
  86. {
  87. $this->reader->expects($this->once())->method('load')->willReturn([]);
  88. $object = new DeploymentConfig($this->reader);
  89. $this->assertFalse($object->isAvailable());
  90. }
  91. public function testNotAvailableThenAvailable()
  92. {
  93. $this->reader->expects($this->at(0))->method('load')->willReturn([]);
  94. $this->reader->expects($this->at(1))->method('load')->willReturn([
  95. ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE => 1
  96. ]);
  97. $object = new DeploymentConfig($this->reader);
  98. $this->assertFalse($object->isAvailable());
  99. $this->assertTrue($object->isAvailable());
  100. }
  101. /**
  102. * @param array $data
  103. * @expectedException \Exception
  104. * @expectedExceptionMessage Key collision
  105. * @dataProvider keyCollisionDataProvider
  106. */
  107. public function testKeyCollision(array $data)
  108. {
  109. $this->reader->expects($this->once())->method('load')->willReturn($data);
  110. $object = new DeploymentConfig($this->reader);
  111. $object->get();
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function keyCollisionDataProvider()
  117. {
  118. return [
  119. [
  120. ['foo' => ['bar' => '1'], 'foo/bar' => '2'],
  121. ['foo/bar' => '1', 'foo' => ['bar' => '2']],
  122. ['foo' => ['subfoo' => ['subbar' => '1'], 'subfoo/subbar' => '2'], 'bar' => '3'],
  123. ]
  124. ];
  125. }
  126. }