ConfigWriterTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Test\Unit\Model;
  7. use Magento\Config\Model\PreparedValueFactory;
  8. use Magento\Deploy\Model\ConfigWriter;
  9. use Magento\Framework\App\Config\ValueInterface;
  10. use Magento\Framework\App\Config\Value;
  11. use Magento\Framework\App\DeploymentConfig\Writer;
  12. use Magento\Framework\Config\File\ConfigFilePool;
  13. use Magento\Framework\Stdlib\ArrayManager;
  14. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  15. class ConfigWriterTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Writer|MockObject
  19. */
  20. private $writerMock;
  21. /**
  22. * @var ArrayManager|MockObject
  23. */
  24. private $arrayManagerMock;
  25. /**
  26. * @var PreparedValueFactory|MockObject
  27. */
  28. private $preparedValueFactoryMock;
  29. /**
  30. * @var ValueInterface|MockObject
  31. */
  32. private $valueInterfaceMock;
  33. /**
  34. * @var Value|MockObject
  35. */
  36. private $valueMock;
  37. /**
  38. * @var ConfigWriter
  39. */
  40. private $model;
  41. /**
  42. * @inheritdoc
  43. */
  44. public function setUp()
  45. {
  46. $this->arrayManagerMock = $this->getMockBuilder(ArrayManager::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->writerMock = $this->getMockBuilder(Writer::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->preparedValueFactoryMock = $this->getMockBuilder(PreparedValueFactory::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->valueInterfaceMock = $this->getMockBuilder(ValueInterface::class)
  56. ->getMockForAbstractClass();
  57. $this->valueMock = $this->getMockBuilder(Value::class)
  58. ->disableOriginalConstructor()
  59. ->setMethods(['validateBeforeSave', 'beforeSave', 'getValue', 'afterSave'])
  60. ->getMock();
  61. $this->model = new ConfigWriter(
  62. $this->writerMock,
  63. $this->arrayManagerMock,
  64. $this->preparedValueFactoryMock
  65. );
  66. }
  67. public function testSave()
  68. {
  69. $values = [
  70. 'some1/config1/path1' => 'someValue1',
  71. 'some2/config2/path2' => 'someValue2',
  72. 'some3/config3/path3' => 'someValue3'
  73. ];
  74. $config = ['system' => []];
  75. $this->preparedValueFactoryMock->expects($this->exactly(3))
  76. ->method('create')
  77. ->withConsecutive(
  78. ['some1/config1/path1', 'someValue1', 'scope', 'scope_code'],
  79. ['some2/config2/path2', 'someValue2', 'scope', 'scope_code'],
  80. ['some3/config3/path3', 'someValue3', 'scope', 'scope_code']
  81. )
  82. ->willReturnOnConsecutiveCalls(
  83. $this->valueInterfaceMock,
  84. $this->valueMock,
  85. $this->valueMock
  86. );
  87. $this->valueMock->expects($this->exactly(2))
  88. ->method('validateBeforeSave');
  89. $this->valueMock->expects($this->exactly(2))
  90. ->method('beforeSave');
  91. $this->valueMock->expects($this->exactly(2))
  92. ->method('getValue')
  93. ->willReturnOnConsecutiveCalls('someValue2', 'someValue3');
  94. $this->valueMock->expects($this->exactly(2))
  95. ->method('afterSave');
  96. $this->arrayManagerMock->expects($this->exactly(3))
  97. ->method('set')
  98. ->withConsecutive(
  99. ['system/scope/scope_code/some1/config1/path1', $this->anything(), 'someValue1'],
  100. ['system/scope/scope_code/some2/config2/path2', $this->anything(), 'someValue2'],
  101. ['system/scope/scope_code/some3/config3/path3', $this->anything(), 'someValue3']
  102. )
  103. ->willReturn($config);
  104. $this->writerMock->expects($this->once())
  105. ->method('saveConfig')
  106. ->with([ConfigFilePool::APP_ENV => $config]);
  107. $this->model->save($values, 'scope', 'scope_code');
  108. }
  109. public function testSaveDefaultScope()
  110. {
  111. $values = [
  112. 'some1/config1/path1' => 'someValue1',
  113. 'some2/config2/path2' => 'someValue2',
  114. 'some3/config3/path3' => 'someValue3'
  115. ];
  116. $config = ['system' => []];
  117. $this->preparedValueFactoryMock->expects($this->exactly(3))
  118. ->method('create')
  119. ->withConsecutive(
  120. ['some1/config1/path1', 'someValue1', 'default'],
  121. ['some2/config2/path2', 'someValue2', 'default'],
  122. ['some3/config3/path3', 'someValue3', 'default']
  123. )
  124. ->willReturnOnConsecutiveCalls(
  125. $this->valueInterfaceMock,
  126. $this->valueMock,
  127. $this->valueMock
  128. );
  129. $this->valueMock->expects($this->exactly(2))
  130. ->method('validateBeforeSave');
  131. $this->valueMock->expects($this->exactly(2))
  132. ->method('beforeSave');
  133. $this->valueMock->expects($this->exactly(2))
  134. ->method('getValue')
  135. ->willReturnOnConsecutiveCalls('someValue2', 'someValue3');
  136. $this->valueMock->expects($this->exactly(2))
  137. ->method('afterSave');
  138. $this->arrayManagerMock->expects($this->exactly(3))
  139. ->method('set')
  140. ->withConsecutive(
  141. ['system/default/some1/config1/path1', $this->anything(), 'someValue1'],
  142. ['system/default/some2/config2/path2', $this->anything(), 'someValue2'],
  143. ['system/default/some3/config3/path3', $this->anything(), 'someValue3']
  144. )
  145. ->willReturn($config);
  146. $this->writerMock->expects($this->once())
  147. ->method('saveConfig')
  148. ->with([ConfigFilePool::APP_ENV => $config]);
  149. $this->model->save($values);
  150. }
  151. /**
  152. * Save null (empty input) through CLI and assert it does not create backend model for validation
  153. * @return void
  154. */
  155. public function testSavingNullValues()
  156. {
  157. $values = [
  158. 'some1/config1/path1' => null,
  159. ];
  160. $this->preparedValueFactoryMock->expects($this->never())->method('create');
  161. $this->writerMock->expects($this->once())
  162. ->method('saveConfig')
  163. ->with([ConfigFilePool::APP_ENV => []]);
  164. $this->model->save($values);
  165. }
  166. }