123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Controller\Adminhtml\Index;
- use Magento\Framework\App\Action\Context;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- /**
- * Class MassUnsubscribeTest
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class MassUnsubscribeTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Customer\Controller\Adminhtml\Index\MassUnsubscribe
- */
- protected $massAction;
- /**
- * @var Context|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $contextMock;
- /**
- * @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $resultRedirectMock;
- /**
- * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $responseMock;
- /**
- * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $messageManagerMock;
- /**
- * @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $objectManagerMock;
- /**
- * @var \Magento\Customer\Model\ResourceModel\Customer\Collection|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerCollectionMock;
- /**
- * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerCollectionFactoryMock;
- /**
- * @var \Magento\Ui\Component\MassAction\Filter|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $filterMock;
- /**
- * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $customerRepositoryMock;
- /**
- * @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $subscriberMock;
- protected function setUp()
- {
- $objectManagerHelper = new ObjectManagerHelper($this);
- $this->contextMock = $this->createMock(\Magento\Backend\App\Action\Context::class);
- $resultRedirectFactory = $this->createMock(\Magento\Backend\Model\View\Result\RedirectFactory::class);
- $this->responseMock = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
- $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
- ->disableOriginalConstructor()->getMock();
- $this->objectManagerMock = $this->createPartialMock(
- \Magento\Framework\ObjectManager\ObjectManager::class,
- ['create']
- );
- $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
- $this->customerCollectionMock =
- $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\Collection::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerCollectionFactoryMock =
- $this->getMockBuilder(\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $redirectMock = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Redirect::class)
- ->disableOriginalConstructor()
- ->getMock();
- $resultFactoryMock = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $resultFactoryMock->expects($this->any())
- ->method('create')
- ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
- ->willReturn($redirectMock);
- $this->subscriberMock = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
- $subscriberFactoryMock = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $subscriberFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->subscriberMock);
- $this->resultRedirectMock = $this->createMock(\Magento\Backend\Model\View\Result\Redirect::class);
- $resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirectMock);
- $this->contextMock->expects($this->once())->method('getMessageManager')->willReturn($this->messageManagerMock);
- $this->contextMock->expects($this->once())->method('getRequest')->willReturn($this->requestMock);
- $this->contextMock->expects($this->once())->method('getResponse')->willReturn($this->responseMock);
- $this->contextMock->expects($this->once())->method('getObjectManager')->willReturn($this->objectManagerMock);
- $this->contextMock->expects($this->any())
- ->method('getResultRedirectFactory')
- ->willReturn($resultRedirectFactory);
- $this->contextMock->expects($this->any())
- ->method('getResultFactory')
- ->willReturn($resultFactoryMock);
- $this->filterMock = $this->createMock(\Magento\Ui\Component\MassAction\Filter::class);
- $this->filterMock->expects($this->once())
- ->method('getCollection')
- ->with($this->customerCollectionMock)
- ->willReturnArgument(0);
- $this->customerCollectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($this->customerCollectionMock);
- $this->customerRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\CustomerRepositoryInterface::class)
- ->getMockForAbstractClass();
- $this->massAction = $objectManagerHelper->getObject(
- \Magento\Customer\Controller\Adminhtml\Index\MassUnsubscribe::class,
- [
- 'context' => $this->contextMock,
- 'filter' => $this->filterMock,
- 'collectionFactory' => $this->customerCollectionFactoryMock,
- 'customerRepository' => $this->customerRepositoryMock,
- 'subscriberFactory' => $subscriberFactoryMock,
- ]
- );
- }
- public function testExecute()
- {
- $customersIds = [10, 11, 12];
- $this->customerCollectionMock->expects($this->any())
- ->method('getAllIds')
- ->willReturn($customersIds);
- $this->customerRepositoryMock->expects($this->any())
- ->method('getById')
- ->willReturnMap([[10, true], [11, true], [12, true]]);
- $this->subscriberMock->expects($this->any())
- ->method('unsubscribeCustomerById')
- ->willReturnMap([[10, true], [11, true], [12, true]]);
- $this->messageManagerMock->expects($this->once())
- ->method('addSuccess')
- ->with(__('A total of %1 record(s) were updated.', count($customersIds)));
- $this->resultRedirectMock->expects($this->any())
- ->method('setPath')
- ->with('customer/*/index')
- ->willReturnSelf();
- $this->massAction->execute();
- }
- public function testExecuteWithException()
- {
- $customersIds = [10, 11, 12];
- $this->customerCollectionMock->expects($this->any())
- ->method('getAllIds')
- ->willReturn($customersIds);
- $this->customerRepositoryMock->expects($this->any())
- ->method('getById')
- ->willThrowException(new \Exception('Some message.'));
- $this->messageManagerMock->expects($this->once())
- ->method('addError')
- ->with('Some message.');
- $this->massAction->execute();
- }
- }
|