ReaderTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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\DeploymentConfig;
  7. use Magento\Framework\App\DeploymentConfig\Reader;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Config\File\ConfigFilePool;
  10. use Magento\Framework\Filesystem\Driver\File;
  11. use Magento\Framework\Filesystem\DriverPool;
  12. class ReaderTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\App\Filesystem\DirectoryList|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $dirList;
  18. /**
  19. * @var DriverPool|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $driverPool;
  22. /**
  23. * @var File|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $fileDriver;
  26. /**
  27. * @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $configFilePool;
  30. protected function setUp()
  31. {
  32. $this->dirList = $this->createMock(\Magento\Framework\App\Filesystem\DirectoryList::class);
  33. $this->dirList->expects($this->any())
  34. ->method('getPath')
  35. ->with(DirectoryList::CONFIG)
  36. ->willReturn(__DIR__ . '/_files');
  37. $this->fileDriver = $this->createMock(File::class);
  38. $this->fileDriver
  39. ->expects($this->any())
  40. ->method('isExists')
  41. ->will($this->returnValueMap([
  42. [__DIR__ . '/_files/config.php', true],
  43. [__DIR__ . '/_files/custom.php', true],
  44. [__DIR__ . '/_files/duplicateConfig.php', true],
  45. [__DIR__ . '/_files/env.php', true],
  46. [__DIR__ . '/_files/mergeOne.php', true],
  47. [__DIR__ . '/_files/mergeTwo.php', true],
  48. [__DIR__ . '/_files/nonexistent.php', false]
  49. ]));
  50. $this->driverPool = $this->createMock(DriverPool::class);
  51. $this->driverPool
  52. ->expects($this->any())
  53. ->method('getDriver')
  54. ->willReturn($this->fileDriver);
  55. $this->configFilePool = $this->createMock(ConfigFilePool::class);
  56. $this->configFilePool
  57. ->expects($this->any())
  58. ->method('getPaths')
  59. ->willReturn(['configKeyOne' => 'config.php', 'configKeyTwo' => 'env.php']);
  60. }
  61. public function testGetFile()
  62. {
  63. $object = new Reader($this->dirList, $this->driverPool, $this->configFilePool);
  64. $files = $object->getFiles();
  65. $this->assertArrayHasKey('configKeyOne', $files);
  66. $this->assertArrayHasKey('configKeyTwo', $files);
  67. $object = new Reader($this->dirList, $this->driverPool, $this->configFilePool, 'customOne.php');
  68. $this->assertEquals(['customOne.php'], $object->getFiles());
  69. }
  70. /**
  71. * @expectedException \InvalidArgumentException
  72. * @expectedExceptionMessage Invalid file name: invalid_name
  73. */
  74. public function testWrongFile()
  75. {
  76. new Reader($this->dirList, $this->driverPool, $this->configFilePool, 'invalid_name');
  77. }
  78. public function testLoad()
  79. {
  80. $files = [['configKeyOne', 'config.php'], ['configKeyTwo','env.php']];
  81. $this->configFilePool
  82. ->expects($this->any())
  83. ->method('getPath')
  84. ->will($this->returnValueMap($files));
  85. $object = new Reader($this->dirList, $this->driverPool, $this->configFilePool);
  86. $this->assertSame(['fooKey' =>'foo', 'barKey' => 'bar', 'envKey' => 'env'], $object->load());
  87. }
  88. /**
  89. * @param string $file
  90. * @param array $expected
  91. * @dataProvider loadCustomDataProvider
  92. */
  93. public function testCustomLoad($file, $expected)
  94. {
  95. $configFilePool = $this->createMock(ConfigFilePool::class);
  96. $configFilePool->expects($this->any())->method('getPaths')->willReturn([$file]);
  97. $configFilePool->expects($this->any())->method('getPath')->willReturn($file);
  98. $object = new Reader($this->dirList, $this->driverPool, $configFilePool, $file);
  99. $this->assertSame($expected, $object->load($file));
  100. }
  101. /**
  102. * Test Reader::load() will throw exception in case of invalid configuration file(single file).
  103. *
  104. * @expectedException \Magento\Framework\Exception\RuntimeException
  105. * @expectedExceptionMessageRegExp /Invalid configuration file: \'.*\/\_files\/emptyConfig\.php\'/
  106. * @return void
  107. */
  108. public function testLoadInvalidConfigurationFileWithFileKey()
  109. {
  110. $fileDriver = $this->getMockBuilder(File::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $fileDriver->expects($this->once())
  114. ->method('isExists')
  115. ->willReturn(true);
  116. /** @var DriverPool|\PHPUnit_Framework_MockObject_MockObject $driverPool */
  117. $driverPool = $this->getMockBuilder(DriverPool::class)
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $driverPool
  121. ->expects($this->once())
  122. ->method('getDriver')
  123. ->willReturn($fileDriver);
  124. /** @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject $configFilePool */
  125. $configFilePool = $this->getMockBuilder(ConfigFilePool::class)
  126. ->disableOriginalConstructor()
  127. ->getMock();
  128. $configFilePool
  129. ->expects($this->once())
  130. ->method('getPath')
  131. ->with($this->identicalTo('testConfig'))
  132. ->willReturn('emptyConfig.php');
  133. $object = new Reader($this->dirList, $driverPool, $configFilePool);
  134. $object->load('testConfig');
  135. }
  136. /**
  137. * Test Reader::load() will throw exception in case of invalid configuration file(multiple files).
  138. *
  139. * @expectedException \Magento\Framework\Exception\RuntimeException
  140. * @expectedExceptionMessageRegExp /Invalid configuration file: \'.*\/\_files\/emptyConfig\.php\'/
  141. * @return void
  142. */
  143. public function testLoadInvalidConfigurationFile()
  144. {
  145. $fileDriver = $this->getMockBuilder(File::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $fileDriver->expects($this->exactly(2))
  149. ->method('isExists')
  150. ->willReturn(true);
  151. /** @var DriverPool|\PHPUnit_Framework_MockObject_MockObject $driverPool */
  152. $driverPool = $this->getMockBuilder(DriverPool::class)
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $driverPool
  156. ->expects($this->once())
  157. ->method('getDriver')
  158. ->willReturn($fileDriver);
  159. /** @var ConfigFilePool|\PHPUnit_Framework_MockObject_MockObject $configFilePool */
  160. $configFilePool = $this->getMockBuilder(ConfigFilePool::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $configFilePool->expects($this->exactly(2))
  164. ->method('getPaths')
  165. ->willReturn(
  166. [
  167. 'configKeyOne' => 'config.php',
  168. 'testConfig' => 'emptyConfig.php'
  169. ]
  170. );
  171. $configFilePool->expects($this->exactly(2))
  172. ->method('getPath')
  173. ->withConsecutive(
  174. [$this->identicalTo('configKeyOne')],
  175. [$this->identicalTo('testConfig')]
  176. )->willReturnOnConsecutiveCalls(
  177. 'config.php',
  178. 'emptyConfig.php'
  179. );
  180. $object = new Reader($this->dirList, $driverPool, $configFilePool);
  181. $object->load();
  182. }
  183. /**
  184. * @return array
  185. */
  186. public function loadCustomDataProvider()
  187. {
  188. return [
  189. ['custom.php', ['bazKey' => 'baz']],
  190. ['nonexistent.php', []],
  191. ];
  192. }
  193. }