SaveTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Test\Unit\Controller\Address;
  8. use Magento\Customer\Api\Data\AddressInterface;
  9. use Magento\Framework\Controller\Result\Json;
  10. use Magento\Framework\Controller\Result\JsonFactory;
  11. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class SaveTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var \Magento\Customer\Controller\Adminhtml\Address\Save
  19. */
  20. private $model;
  21. /**
  22. * @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $addressRepositoryMock;
  25. /**
  26. * @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $formFactoryMock;
  29. /**
  30. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $customerRepositoryMock;
  33. /**
  34. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $dataObjectHelperMock;
  37. /**
  38. * @var \Magento\Customer\Api\Data\AddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $addressDataFactoryMock;
  41. /**
  42. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $loggerMock;
  45. /**
  46. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $requestMock;
  49. /**
  50. * @var AddressInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $address;
  53. /**
  54. * @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $resultJsonFactory;
  57. /**
  58. * @var Json|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $json;
  61. /**
  62. * @inheritdoc
  63. */
  64. protected function setUp()
  65. {
  66. $this->addressRepositoryMock = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  67. $this->formFactoryMock = $this->createMock(\Magento\Customer\Model\Metadata\FormFactory::class);
  68. $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  69. $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper ::class);
  70. $this->addressDataFactoryMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterfaceFactory::class);
  71. $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
  72. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  73. $this->address = $this->getMockBuilder(AddressInterface::class)
  74. ->disableOriginalConstructor()
  75. ->getMockForAbstractClass();
  76. $this->resultJsonFactory = $this->getMockBuilder(JsonFactory::class)
  77. ->disableOriginalConstructor()
  78. ->setMethods(['create'])
  79. ->getMock();
  80. $this->json = $this->getMockBuilder(Json::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $objectManager = new ObjectManagerHelper($this);
  84. $this->model = $objectManager->getObject(
  85. \Magento\Customer\Controller\Adminhtml\Address\Save::class,
  86. [
  87. 'addressRepository' => $this->addressRepositoryMock,
  88. 'formFactory' => $this->formFactoryMock,
  89. 'customerRepository' => $this->customerRepositoryMock,
  90. 'dataObjectHelper' => $this->dataObjectHelperMock,
  91. 'addressDataFactory' => $this->addressDataFactoryMock,
  92. 'logger' => $this->loggerMock,
  93. 'request' => $this->requestMock,
  94. 'resultJsonFactory' => $this->resultJsonFactory
  95. ]
  96. );
  97. }
  98. public function testExecute(): void
  99. {
  100. $addressId = 11;
  101. $customerId = 22;
  102. $addressExtractedData = [
  103. 'entity_id' => $addressId,
  104. 'code' => 'value',
  105. 'coolness' => false,
  106. 'region' => 'region',
  107. 'region_id' => 'region_id',
  108. ];
  109. $addressCompactedData = [
  110. 'entity_id' => $addressId,
  111. 'default_billing' => 'true',
  112. 'default_shipping' => 'true',
  113. 'code' => 'value',
  114. 'coolness' => false,
  115. 'region' => 'region',
  116. 'region_id' => 'region_id',
  117. ];
  118. $mergedAddressData = [
  119. 'entity_id' => $addressId,
  120. 'default_billing' => true,
  121. 'default_shipping' => true,
  122. 'code' => 'value',
  123. 'region' => [
  124. 'region' => 'region',
  125. 'region_id' => 'region_id',
  126. ],
  127. 'region_id' => 'region_id',
  128. 'id' => $addressId,
  129. ];
  130. $this->requestMock->method('getParam')
  131. ->withConsecutive(['parent_id'], ['entity_id'])
  132. ->willReturnOnConsecutiveCalls(22, 1);
  133. $customerMock = $this->getMockBuilder(
  134. \Magento\Customer\Api\Data\CustomerInterface::class
  135. )->disableOriginalConstructor()->getMock();
  136. $this->customerRepositoryMock->expects($this->atLeastOnce())
  137. ->method('getById')
  138. ->with($customerId)
  139. ->willReturn($customerMock);
  140. $customerAddressFormMock = $this->createMock(\Magento\Customer\Model\Metadata\Form::class);
  141. $customerAddressFormMock->expects($this->atLeastOnce())
  142. ->method('extractData')
  143. ->with($this->requestMock)
  144. ->willReturn($addressExtractedData);
  145. $customerAddressFormMock->expects($this->once())
  146. ->method('compactData')
  147. ->with($addressExtractedData)
  148. ->willReturn($addressCompactedData);
  149. $this->formFactoryMock->expects($this->exactly(1))
  150. ->method('create')
  151. ->willReturn($customerAddressFormMock);
  152. $addressMock = $this->getMockBuilder(AddressInterface::class)
  153. ->disableOriginalConstructor()
  154. ->getMock();
  155. $this->addressDataFactoryMock->expects($this->once())->method('create')->willReturn($addressMock);
  156. $this->dataObjectHelperMock->expects($this->atLeastOnce())
  157. ->method('populateWithArray')
  158. ->willReturn(
  159. [
  160. $addressMock,
  161. $mergedAddressData, AddressInterface::class,
  162. $this->dataObjectHelperMock,
  163. ]
  164. );
  165. $this->addressRepositoryMock->expects($this->once())->method('save')->willReturn($this->address);
  166. $this->address->expects($this->once())->method('getId')->willReturn($addressId);
  167. $this->resultJsonFactory->expects($this->once())
  168. ->method('create')
  169. ->willReturn($this->json);
  170. $this->json->expects($this->once())
  171. ->method('setData')
  172. ->with(
  173. [
  174. 'message' => __('Customer address has been updated.'),
  175. 'error' => false,
  176. 'data' => [
  177. 'entity_id' => $addressId
  178. ]
  179. ]
  180. )->willReturnSelf();
  181. $this->assertEquals($this->json, $this->model->execute());
  182. }
  183. }