Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EnvironmentConfigSourceTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class EnvironmentConfigSourceTest extends \PHPUnit\Framework\TestCase
14 {
18  private $arrayManagerMock;
19 
23  private $placeholderMock;
24 
28  private $source;
29 
30  protected function setUp()
31  {
32  $this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
33  ->disableOriginalConstructor()
34  ->getMock();
35  $this->placeholderMock = $this->getMockBuilder(PlaceholderInterface::class)
36  ->getMockForAbstractClass();
37 
39  $placeholderFactoryMock = $this->getMockBuilder(PlaceholderFactory::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42  $placeholderFactoryMock->expects($this->once())
43  ->method('create')
45  ->willReturn($this->placeholderMock);
46 
47  $this->source = new EnvironmentConfigSource($this->arrayManagerMock, $placeholderFactoryMock);
48  }
49 
55  public function testGet($path, $expectedResult)
56  {
57  $placeholder = 'CONFIG__UNIT__TEST__VALUE';
58  $configValue = 'test_value';
59  $configPath = 'unit/test/value';
60  $expectedArray = ['unit' => ['test' => ['value' => $configValue]]];
61  $_ENV[$placeholder] = $configValue;
62 
63  $this->placeholderMock->expects($this->any())
64  ->method('isApplicable')
65  ->willReturnMap([
66  [$placeholder, true]
67  ]);
68  $this->placeholderMock->expects($this->once())
69  ->method('restore')
70  ->with($placeholder)
71  ->willReturn($configPath);
72  $this->arrayManagerMock->expects($this->once())
73  ->method('set')
74  ->with($configPath, [], $configValue)
75  ->willReturn($expectedArray);
76 
77  $this->assertEquals($expectedResult, $this->source->get($path));
78  }
79 
83  public function getDataProvider()
84  {
85  return [
86  ['', ['unit' => ['test' => ['value' => 'test_value']]]],
87  ['unit', ['test' => ['value' => 'test_value']]],
88  ['unit/test', ['value' => 'test_value']],
89  ['unit/test/value', 'test_value'],
90  ['wrong/path', []],
91  ];
92  }
93 
95  {
96  $expectedArray = [];
97 
98  $this->placeholderMock->expects($this->any())
99  ->method('isApplicable')
100  ->willReturn(false);
101  $this->placeholderMock->expects($this->never())
102  ->method('restore');
103  $this->arrayManagerMock->expects($this->never())
104  ->method('set');
105 
106  $this->assertSame($expectedArray, $this->source->get());
107  }
108 
109  public function tearDown()
110  {
111  unset($_ENV['CONFIG__UNIT__TEST__VALUE']);
112  }
113 }