ImporterTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Model\Config;
  7. use Magento\Config\Model\Config\Importer;
  8. use Magento\Config\Model\Config\Importer\SaveProcessor;
  9. use Magento\Config\Model\PreparedValueFactory;
  10. use Magento\Framework\App\Area;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\App\Config\Value;
  13. use Magento\Framework\App\State;
  14. use Magento\Framework\Config\ScopeInterface;
  15. use Magento\Framework\Flag;
  16. use Magento\Framework\FlagManager;
  17. use Magento\Framework\Stdlib\ArrayUtils;
  18. use PHPUnit_Framework_MockObject_MockObject as Mock;
  19. /**
  20. * Test for Importer.
  21. *
  22. * @see Importer
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class ImporterTest extends \PHPUnit\Framework\TestCase
  26. {
  27. /**
  28. * @var Importer
  29. */
  30. private $model;
  31. /**
  32. * @var FlagManager|Mock
  33. */
  34. private $flagManagerMock;
  35. /**
  36. * @var Flag|Mock
  37. */
  38. private $flagMock;
  39. /**
  40. * @var ArrayUtils|Mock
  41. */
  42. private $arrayUtilsMock;
  43. /**
  44. * @var PreparedValueFactory|Mock
  45. */
  46. private $valueFactoryMock;
  47. /**
  48. * @var ScopeConfigInterface|Mock
  49. */
  50. private $scopeConfigMock;
  51. /**
  52. * @var State|Mock
  53. */
  54. private $stateMock;
  55. /**
  56. * @var ScopeInterface|Mock
  57. */
  58. private $scopeMock;
  59. /**
  60. * @var Value|Mock
  61. */
  62. private $valueMock;
  63. /**
  64. * @var SaveProcessor|Mock
  65. */
  66. private $saveProcessorMock;
  67. /**
  68. * @inheritdoc
  69. */
  70. protected function setUp()
  71. {
  72. $this->flagManagerMock = $this->getMockBuilder(FlagManager::class)
  73. ->setMethods(['create', 'getFlagData', 'saveFlag'])
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->flagMock = $this->getMockBuilder(Flag::class)
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->arrayUtilsMock = $this->getMockBuilder(ArrayUtils::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->valueFactoryMock = $this->getMockBuilder(PreparedValueFactory::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->valueMock = $this->getMockBuilder(Value::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
  89. ->getMockForAbstractClass();
  90. $this->stateMock = $this->getMockBuilder(State::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
  94. ->getMockForAbstractClass();
  95. $this->saveProcessorMock = $this->getMockBuilder(SaveProcessor::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->flagManagerMock->expects($this->any())
  99. ->method('create')
  100. ->willReturn($this->flagMock);
  101. $this->model = new Importer(
  102. $this->flagManagerMock,
  103. $this->arrayUtilsMock,
  104. $this->saveProcessorMock,
  105. $this->scopeConfigMock,
  106. $this->stateMock,
  107. $this->scopeMock
  108. );
  109. }
  110. public function testImport()
  111. {
  112. $data = [];
  113. $currentData = ['current' => '2'];
  114. $this->flagManagerMock->expects($this->once())
  115. ->method('getFlagData')
  116. ->with(Importer::FLAG_CODE)
  117. ->willReturn($currentData);
  118. $this->arrayUtilsMock->expects($this->exactly(2))
  119. ->method('recursiveDiff')
  120. ->willReturnMap([
  121. [$data, $currentData, []],
  122. [$currentData, $data, []]
  123. ]);
  124. $this->scopeMock->expects($this->once())
  125. ->method('getCurrentScope')
  126. ->willReturn('oldScope');
  127. $this->stateMock->expects($this->once())
  128. ->method('emulateAreaCode')
  129. ->with(Area::AREA_ADMINHTML, $this->anything())
  130. ->willReturnCallback(function ($area, $function) {
  131. $this->assertEquals(Area::AREA_ADMINHTML, $area);
  132. return $function();
  133. });
  134. $this->saveProcessorMock->expects($this->once())
  135. ->method('process')
  136. ->with([]);
  137. $this->scopeMock->expects($this->at(1))
  138. ->method('setCurrentScope')
  139. ->with(Area::AREA_ADMINHTML);
  140. $this->scopeMock->expects($this->at(2))
  141. ->method('setCurrentScope')
  142. ->with('oldScope');
  143. $this->scopeMock->expects($this->at(3))
  144. ->method('setCurrentScope')
  145. ->with('oldScope');
  146. $this->flagManagerMock->expects($this->once())
  147. ->method('saveFlag')
  148. ->with(Importer::FLAG_CODE, $data);
  149. $this->assertSame(['System config was processed'], $this->model->import($data));
  150. }
  151. /**
  152. * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException
  153. * @expectedExceptionMessage Some error
  154. */
  155. public function testImportWithException()
  156. {
  157. $data = [];
  158. $currentData = ['current' => '2'];
  159. $this->flagManagerMock->expects($this->once())
  160. ->method('getFlagData')
  161. ->willReturn($currentData);
  162. $this->arrayUtilsMock->expects($this->exactly(2))
  163. ->method('recursiveDiff')
  164. ->willReturnMap([
  165. [$data, $currentData, []],
  166. [$currentData, $data, []]
  167. ]);
  168. $this->scopeMock->expects($this->once())
  169. ->method('getCurrentScope')
  170. ->willReturn('oldScope');
  171. $this->stateMock->expects($this->once())
  172. ->method('emulateAreaCode')
  173. ->willThrowException(new \Exception('Some error'));
  174. $this->scopeMock->expects($this->once())
  175. ->method('setCurrentScope')
  176. ->with('oldScope');
  177. $this->model->import($data);
  178. }
  179. }