EnvironmentConfigSourceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\App\Config\Source;
  7. use Magento\Config\App\Config\Source\EnvironmentConfigSource;
  8. use Magento\Config\Model\Placeholder\PlaceholderFactory;
  9. use Magento\Config\Model\Placeholder\PlaceholderInterface;
  10. use Magento\Framework\Stdlib\ArrayManager;
  11. class EnvironmentConfigSourceTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var ArrayManager|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $arrayManagerMock;
  17. /**
  18. * @var PlaceholderInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $placeholderMock;
  21. /**
  22. * @var EnvironmentConfigSource
  23. */
  24. private $source;
  25. protected function setUp()
  26. {
  27. $this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->placeholderMock = $this->getMockBuilder(PlaceholderInterface::class)
  31. ->getMockForAbstractClass();
  32. /** @var PlaceholderFactory|\PHPUnit_Framework_MockObject_MockObject $placeholderFactoryMock */
  33. $placeholderFactoryMock = $this->getMockBuilder(PlaceholderFactory::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $placeholderFactoryMock->expects($this->once())
  37. ->method('create')
  38. ->with(PlaceholderFactory::TYPE_ENVIRONMENT)
  39. ->willReturn($this->placeholderMock);
  40. $this->source = new EnvironmentConfigSource($this->arrayManagerMock, $placeholderFactoryMock);
  41. }
  42. /**
  43. * @param string $path
  44. * @param array|string $expectedResult
  45. * @dataProvider getDataProvider
  46. */
  47. public function testGet($path, $expectedResult)
  48. {
  49. $placeholder = 'CONFIG__UNIT__TEST__VALUE';
  50. $configValue = 'test_value';
  51. $configPath = 'unit/test/value';
  52. $expectedArray = ['unit' => ['test' => ['value' => $configValue]]];
  53. $_ENV[$placeholder] = $configValue;
  54. $this->placeholderMock->expects($this->any())
  55. ->method('isApplicable')
  56. ->willReturnMap([
  57. [$placeholder, true]
  58. ]);
  59. $this->placeholderMock->expects($this->once())
  60. ->method('restore')
  61. ->with($placeholder)
  62. ->willReturn($configPath);
  63. $this->arrayManagerMock->expects($this->once())
  64. ->method('set')
  65. ->with($configPath, [], $configValue)
  66. ->willReturn($expectedArray);
  67. $this->assertEquals($expectedResult, $this->source->get($path));
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function getDataProvider()
  73. {
  74. return [
  75. ['', ['unit' => ['test' => ['value' => 'test_value']]]],
  76. ['unit', ['test' => ['value' => 'test_value']]],
  77. ['unit/test', ['value' => 'test_value']],
  78. ['unit/test/value', 'test_value'],
  79. ['wrong/path', []],
  80. ];
  81. }
  82. public function testGetWithoutEnvConfigurationVariables()
  83. {
  84. $expectedArray = [];
  85. $this->placeholderMock->expects($this->any())
  86. ->method('isApplicable')
  87. ->willReturn(false);
  88. $this->placeholderMock->expects($this->never())
  89. ->method('restore');
  90. $this->arrayManagerMock->expects($this->never())
  91. ->method('set');
  92. $this->assertSame($expectedArray, $this->source->get());
  93. }
  94. public function tearDown()
  95. {
  96. unset($_ENV['CONFIG__UNIT__TEST__VALUE']);
  97. }
  98. }