DeleteTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Address;
  7. use Magento\Customer\Controller\Address\Delete;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class DeleteTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var Delete */
  15. protected $model;
  16. /** @var \Magento\Framework\App\Action\Context */
  17. protected $context;
  18. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $sessionMock;
  20. /** @var \Magento\Framework\Data\Form\FormKey\Validator|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $validatorMock;
  22. /** @var \Magento\Customer\Api\AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $addressRepositoryMock;
  24. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $request;
  26. /** @var \Magento\Customer\Api\Data\AddressInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $address;
  28. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $messageManager;
  30. /** @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $resultRedirectFactory;
  32. /** @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $resultRedirect;
  34. protected function setUp()
  35. {
  36. $this->sessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Data\Form\FormKey\Validator::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $formFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\Metadata\FormFactory::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->addressRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
  46. ->getMockForAbstractClass();
  47. $addressInterfaceFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterfaceFactory::class)
  48. ->disableOriginalConstructor()
  49. ->setMethods(['create'])
  50. ->getMock();
  51. $regionInterfaceFactoryMock = $this->getMockBuilder(\Magento\Customer\Api\Data\RegionInterfaceFactory::class)
  52. ->disableOriginalConstructor()
  53. ->setMethods(['create'])
  54. ->getMock();
  55. $dataObjectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $forwardFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\Result\ForwardFactory::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods(['create'])
  64. ->getMock();
  65. $pageFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  69. ->getMockForAbstractClass();
  70. $this->address = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
  71. ->getMockForAbstractClass();
  72. $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
  73. ->getMockForAbstractClass();
  74. $this->resultRedirectFactory =
  75. $this->getMockBuilder(\Magento\Framework\Controller\Result\RedirectFactory::class)
  76. ->disableOriginalConstructor()
  77. ->getMock();
  78. $this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $objectManager = new ObjectManagerHelper($this);
  82. $this->context = $objectManager->getObject(
  83. \Magento\Framework\App\Action\Context::class,
  84. [
  85. 'request' => $this->request,
  86. 'messageManager' => $this->messageManager,
  87. 'resultRedirectFactory' => $this->resultRedirectFactory,
  88. ]
  89. );
  90. $this->model = new Delete(
  91. $this->context,
  92. $this->sessionMock,
  93. $this->validatorMock,
  94. $formFactoryMock,
  95. $this->addressRepositoryMock,
  96. $addressInterfaceFactoryMock,
  97. $regionInterfaceFactoryMock,
  98. $dataObjectProcessorMock,
  99. $dataObjectHelperMock,
  100. $forwardFactoryMock,
  101. $pageFactoryMock
  102. );
  103. }
  104. public function testExecute()
  105. {
  106. $addressId = 1;
  107. $customerId = 2;
  108. $this->resultRedirectFactory->expects($this->once())
  109. ->method('create')
  110. ->willReturn($this->resultRedirect);
  111. $this->request->expects($this->once())
  112. ->method('getParam')
  113. ->with('id', false)
  114. ->willReturn($addressId);
  115. $this->validatorMock->expects($this->once())
  116. ->method('validate')
  117. ->with($this->request)
  118. ->willReturn(true);
  119. $this->addressRepositoryMock->expects($this->once())
  120. ->method('getById')
  121. ->with($addressId)
  122. ->willReturn($this->address);
  123. $this->sessionMock->expects($this->once())
  124. ->method('getCustomerId')
  125. ->willReturn($customerId);
  126. $this->address->expects($this->once())
  127. ->method('getCustomerId')
  128. ->willReturn($customerId);
  129. $this->addressRepositoryMock->expects($this->once())
  130. ->method('deleteById')
  131. ->with($addressId);
  132. $this->messageManager->expects($this->once())
  133. ->method('addSuccess')
  134. ->with(__('You deleted the address.'));
  135. $this->resultRedirect->expects($this->once())
  136. ->method('setPath')
  137. ->with('*/*/index')
  138. ->willReturnSelf();
  139. $this->assertSame($this->resultRedirect, $this->model->execute());
  140. }
  141. public function testExecuteWithException()
  142. {
  143. $addressId = 1;
  144. $customerId = 2;
  145. $this->resultRedirectFactory->expects($this->once())
  146. ->method('create')
  147. ->willReturn($this->resultRedirect);
  148. $this->request->expects($this->once())
  149. ->method('getParam')
  150. ->with('id', false)
  151. ->willReturn($addressId);
  152. $this->validatorMock->expects($this->once())
  153. ->method('validate')
  154. ->with($this->request)
  155. ->willReturn(true);
  156. $this->addressRepositoryMock->expects($this->once())
  157. ->method('getById')
  158. ->with($addressId)
  159. ->willReturn($this->address);
  160. $this->sessionMock->expects($this->once())
  161. ->method('getCustomerId')
  162. ->willReturn($customerId);
  163. $this->address->expects($this->once())
  164. ->method('getCustomerId')
  165. ->willReturn(34);
  166. $exception = new \Exception('Exception');
  167. $this->messageManager->expects($this->once())
  168. ->method('addError')
  169. ->with(__('We can\'t delete the address right now.'))
  170. ->willThrowException($exception);
  171. $this->messageManager->expects($this->once())
  172. ->method('addException')
  173. ->with($exception, __('We can\'t delete the address right now.'));
  174. $this->resultRedirect->expects($this->once())
  175. ->method('setPath')
  176. ->with('*/*/index')
  177. ->willReturnSelf();
  178. $this->assertSame($this->resultRedirect, $this->model->execute());
  179. }
  180. }