MassDeleteTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\Adminhtml\Index;
  7. use Magento\Framework\App\Action\Context;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. /**
  10. * Class MassDeleteTest
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class MassDeleteTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Customer\Controller\Adminhtml\Index\MassDelete
  17. */
  18. protected $massAction;
  19. /**
  20. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $contextMock;
  23. /**
  24. * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $resultRedirectMock;
  27. /**
  28. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $requestMock;
  31. /**
  32. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $responseMock;
  35. /**
  36. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $messageManagerMock;
  39. /**
  40. * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $objectManagerMock;
  43. /**
  44. * @var \Magento\Customer\Model\ResourceModel\Customer\Collection|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $customerCollectionMock;
  47. /**
  48. * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. protected $customerCollectionFactoryMock;
  51. /**
  52. * @var \Magento\Ui\Component\MassAction\Filter|\PHPUnit_Framework_MockObject_MockObject
  53. */
  54. protected $filterMock;
  55. /**
  56. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $customerRepositoryMock;
  59. protected function setUp()
  60. {
  61. $objectManagerHelper = new ObjectManagerHelper($this);
  62. $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
  63. $resultRedirectFactory = $this->createMock(\Magento\Backend\Model\View\Result\RedirectFactory::class);
  64. $this->responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  65. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  66. ->disableOriginalConstructor()->getMock();
  67. $this->objectManagerMock = $this->createPartialMock(
  68. \Magento\Framework\ObjectManager\ObjectManager::class,
  69. ['create']
  70. );
  71. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
  72. $this->customerCollectionMock =
  73. $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->customerCollectionFactoryMock =
  77. $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class)
  78. ->disableOriginalConstructor()
  79. ->setMethods(['create'])
  80. ->getMock();
  81. $redirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $resultFactoryMock->expects($this->any())
  88. ->method('create')
  89. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
  90. ->willReturn($redirectMock);
  91. $this->resultRedirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirectMock);
  95. $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock);
  96. $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
  97. $this->contextMock->expects($this->once())->method('getResponse')->willReturn($this->responseMock);
  98. $this->contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
  99. $this->contextMock->expects($this->any())
  100. ->method('getResultRedirectFactory')
  101. ->willReturn($resultRedirectFactory);
  102. $this->contextMock->expects($this->any())
  103. ->method('getResultFactory')
  104. ->willReturn($resultFactoryMock);
  105. $this->filterMock = $this->createMock(\Magento\Ui\Component\MassAction\Filter::class);
  106. $this->filterMock->expects($this->once())
  107. ->method('getCollection')
  108. ->with($this->customerCollectionMock)
  109. ->willReturnArgument(0);
  110. $this->customerCollectionFactoryMock->expects($this->once())
  111. ->method('create')
  112. ->willReturn($this->customerCollectionMock);
  113. $this->customerRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
  114. ->getMockForAbstractClass();
  115. $this->massAction = $objectManagerHelper->getObject(
  116. \Magento\Customer\Controller\Adminhtml\Index\MassDelete::class,
  117. [
  118. 'context' => $this->contextMock,
  119. 'filter' => $this->filterMock,
  120. 'collectionFactory' => $this->customerCollectionFactoryMock,
  121. 'customerRepository' => $this->customerRepositoryMock,
  122. ]
  123. );
  124. }
  125. public function testExecute()
  126. {
  127. $customersIds = [10, 11, 12];
  128. $this->customerCollectionMock->expects($this->any())
  129. ->method('getAllIds')
  130. ->willReturn($customersIds);
  131. $this->customerRepositoryMock->expects($this->any())
  132. ->method('deleteById')
  133. ->willReturnMap([[10, true], [11, true], [12, true]]);
  134. $this->messageManagerMock->expects($this->once())
  135. ->method('addSuccess')
  136. ->with(__('A total of %1 record(s) were deleted.', count($customersIds)));
  137. $this->resultRedirectMock->expects($this->any())
  138. ->method('setPath')
  139. ->with('customer/*/index')
  140. ->willReturnSelf();
  141. $this->massAction->execute();
  142. }
  143. public function testExecuteWithException()
  144. {
  145. $customersIds = [10, 11, 12];
  146. $this->customerCollectionMock->expects($this->any())
  147. ->method('getAllIds')
  148. ->willReturn($customersIds);
  149. $this->customerRepositoryMock->expects($this->any())
  150. ->method('deleteById')
  151. ->willThrowException(new \Exception('Some message.'));
  152. $this->messageManagerMock->expects($this->once())
  153. ->method('addError')
  154. ->with('Some message.');
  155. $this->massAction->execute();
  156. }
  157. }