WriterTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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;
  8. use Magento\Framework\App\DeploymentConfig\Reader;
  9. use Magento\Framework\App\DeploymentConfig\Writer;
  10. use Magento\Framework\App\DeploymentConfig\CommentParser;
  11. use Magento\Framework\App\DeploymentConfig\Writer\FormatterInterface;
  12. use Magento\Framework\App\Filesystem\DirectoryList;
  13. use Magento\Framework\Config\File\ConfigFilePool;
  14. use Magento\Framework\Exception\FileSystemException;
  15. use Magento\Framework\Filesystem;
  16. use Magento\Framework\Filesystem\Directory\ReadInterface;
  17. use Magento\Framework\Filesystem\Directory\WriteInterface;
  18. use Magento\Framework\Phrase;
  19. use \PHPUnit_Framework_MockObject_MockObject as Mock;
  20. /**
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class WriterTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * @var Writer
  27. */
  28. private $object;
  29. /**
  30. * @var DeploymentConfig\Reader|Mock
  31. */
  32. private $reader;
  33. /**
  34. * @var WriteInterface|Mock
  35. */
  36. private $dirWrite;
  37. /**
  38. * @var ReadInterface|Mock
  39. */
  40. private $dirRead;
  41. /**
  42. * @var FormatterInterface|Mock
  43. */
  44. protected $formatter;
  45. /**
  46. * @var ConfigFilePool|Mock
  47. */
  48. private $configFilePool;
  49. /**
  50. * @var DeploymentConfig|Mock
  51. */
  52. private $deploymentConfig;
  53. /**
  54. * @var Filesystem|Mock
  55. */
  56. private $filesystem;
  57. /**
  58. * @var CommentParser|Mock
  59. */
  60. private $commentParserMock;
  61. protected function setUp()
  62. {
  63. $this->commentParserMock = $this->getMockBuilder(CommentParser::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->reader = $this->getMockBuilder(Reader::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->filesystem = $this->getMockBuilder(Filesystem::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->formatter = $this->getMockForAbstractClass(FormatterInterface::class);
  73. $this->configFilePool = $this->getMockBuilder(ConfigFilePool::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->dirWrite = $this->getMockForAbstractClass(WriteInterface::class);
  80. $this->dirRead = $this->getMockForAbstractClass(ReadInterface::class);
  81. $this->object = new Writer(
  82. $this->reader,
  83. $this->filesystem,
  84. $this->configFilePool,
  85. $this->deploymentConfig,
  86. $this->formatter,
  87. $this->commentParserMock
  88. );
  89. }
  90. public function testSaveConfig()
  91. {
  92. $configFiles = [
  93. ConfigFilePool::APP_CONFIG => 'config.php'
  94. ];
  95. $testSetExisting = [
  96. ConfigFilePool::APP_CONFIG => [
  97. 'foo' => 'bar',
  98. 'key' => 'value',
  99. 'baz' => [
  100. 'test' => 'value',
  101. 'test1' => 'value1'
  102. ]
  103. ],
  104. ];
  105. $testSetUpdate = [
  106. ConfigFilePool::APP_CONFIG => [
  107. 'baz' => [
  108. 'test' => 'value2'
  109. ]
  110. ],
  111. ];
  112. $testSetExpected = [
  113. ConfigFilePool::APP_CONFIG => [
  114. 'foo' => 'bar',
  115. 'key' => 'value',
  116. 'baz' => [
  117. 'test' => 'value2',
  118. 'test1' => 'value1'
  119. ]
  120. ],
  121. ];
  122. $testComments = [
  123. 'baz' => 'Baz comment2',
  124. 'bar' => 'Bar comment'
  125. ];
  126. $existedComments = [
  127. 'foo' => 'Foo comment',
  128. 'baz' => 'Baz comment',
  129. ];
  130. $expectedComments = [
  131. 'foo' => 'Foo comment',
  132. 'baz' => 'Baz comment2',
  133. 'bar' => 'Bar comment'
  134. ];
  135. $this->deploymentConfig->expects($this->once())
  136. ->method('resetData');
  137. $this->configFilePool->expects($this->once())
  138. ->method('getPaths')
  139. ->willReturn($configFiles);
  140. $this->dirWrite->expects($this->any())
  141. ->method('isExist')
  142. ->willReturn(true);
  143. $this->reader->expects($this->once())
  144. ->method('load')
  145. ->willReturn($testSetExisting[ConfigFilePool::APP_CONFIG]);
  146. $this->commentParserMock->expects($this->once())
  147. ->method('execute')
  148. ->with('config.php')
  149. ->willReturn($existedComments);
  150. $this->formatter->expects($this->once())
  151. ->method('format')
  152. ->with($testSetExpected[ConfigFilePool::APP_CONFIG], $expectedComments)
  153. ->willReturn([]);
  154. $this->dirWrite->expects($this->once())
  155. ->method('writeFile')
  156. ->with('config.php', []);
  157. $this->reader->expects($this->any())
  158. ->method('getFiles')
  159. ->willReturn('test.php');
  160. $this->dirRead->expects($this->any())
  161. ->method('getAbsolutePath');
  162. $this->filesystem->expects($this->any())
  163. ->method('getDirectoryWrite')
  164. ->with(DirectoryList::CONFIG)
  165. ->willReturn($this->dirWrite);
  166. $this->filesystem->expects($this->any())
  167. ->method('getDirectoryRead')
  168. ->with(DirectoryList::CONFIG)
  169. ->willReturn($this->dirRead);
  170. $this->object->saveConfig($testSetUpdate, false, null, $testComments);
  171. }
  172. public function testSaveConfigOverride()
  173. {
  174. $configFiles = [
  175. ConfigFilePool::APP_CONFIG => 'config.php'
  176. ];
  177. $testSetUpdate = [
  178. ConfigFilePool::APP_CONFIG => [
  179. 'baz' => [
  180. 'test' => 'value2'
  181. ]
  182. ],
  183. ];
  184. $testSetExpected = [
  185. ConfigFilePool::APP_CONFIG => [
  186. 'baz' => [
  187. 'test' => 'value2',
  188. ]
  189. ],
  190. ];
  191. $this->deploymentConfig->expects($this->once())
  192. ->method('resetData');
  193. $this->configFilePool->expects($this->once())
  194. ->method('getPaths')
  195. ->willReturn($configFiles);
  196. $this->dirWrite->expects($this->any())
  197. ->method('isExist')
  198. ->willReturn(true);
  199. $this->commentParserMock->expects($this->once())
  200. ->method('execute')
  201. ->with('config.php')
  202. ->willReturn([]);
  203. $this->formatter->expects($this->once())
  204. ->method('format')
  205. ->with($testSetExpected[ConfigFilePool::APP_CONFIG])
  206. ->willReturn([]);
  207. $this->dirWrite->expects($this->once())
  208. ->method('writeFile')
  209. ->with('config.php', []);
  210. $this->reader->expects($this->any())
  211. ->method('getFiles')
  212. ->willReturn('test.php');
  213. $this->dirRead->expects($this->any())
  214. ->method('getAbsolutePath');
  215. $this->filesystem->expects($this->any())
  216. ->method('getDirectoryWrite')
  217. ->with(DirectoryList::CONFIG)
  218. ->willReturn($this->dirWrite);
  219. $this->filesystem->expects($this->any())
  220. ->method('getDirectoryRead')
  221. ->with(DirectoryList::CONFIG)
  222. ->willReturn($this->dirRead);
  223. $this->object->saveConfig($testSetUpdate, true);
  224. }
  225. /**
  226. * @expectedException \Magento\Framework\Exception\FileSystemException
  227. * @expectedExceptionMessage The "env.php" deployment config file isn't writable.
  228. */
  229. public function testSaveConfigException()
  230. {
  231. $exception = new FileSystemException(new Phrase('error when writing file config file'));
  232. $this->configFilePool->method('getPaths')
  233. ->willReturn([ConfigFilePool::APP_ENV => 'env.php']);
  234. $this->commentParserMock->expects($this->once())
  235. ->method('execute')
  236. ->with('env.php')
  237. ->willReturn([]);
  238. $this->dirWrite->method('writeFile')
  239. ->willThrowException($exception);
  240. $this->reader->expects($this->any())
  241. ->method('getFiles')
  242. ->willReturn('test.php');
  243. $this->dirRead->expects($this->any())
  244. ->method('getAbsolutePath');
  245. $this->filesystem->expects($this->any())
  246. ->method('getDirectoryWrite')
  247. ->with(DirectoryList::CONFIG)
  248. ->willReturn($this->dirWrite);
  249. $this->filesystem->expects($this->any())
  250. ->method('getDirectoryRead')
  251. ->with(DirectoryList::CONFIG)
  252. ->willReturn($this->dirRead);
  253. $this->dirWrite->expects($this->any())
  254. ->method('isExist')
  255. ->willReturn(true);
  256. $this->object->saveConfig([ConfigFilePool::APP_ENV => ['key' => 'value']]);
  257. }
  258. }