UnlockTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Locks;
  7. use Magento\Customer\Model\AuthenticationInterface;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Test class for \Magento\Customer\Controller\Adminhtml\Locks\Unlock testing
  11. *
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class UnlockTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Backend\App\Action\Context
  18. */
  19. protected $contextMock;
  20. /**
  21. * Authentication
  22. *
  23. * @var AuthenticationInterface
  24. */
  25. protected $authenticationMock;
  26. /**
  27. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  28. */
  29. protected $objectManager;
  30. /**
  31. * @var \Magento\Framework\App\RequestInterface
  32. */
  33. protected $requestMock;
  34. /**
  35. * @var \Magento\Framework\Message\ManagerInterface
  36. */
  37. protected $messageManagerMock;
  38. /**
  39. * @var \Magento\Framework\Controller\ResultFactory
  40. */
  41. protected $resultFactoryMock;
  42. /**
  43. * @var \Magento\Backend\Model\View\Result\Redirect
  44. */
  45. protected $redirectMock;
  46. /**
  47. * @var \Magento\Customer\Model\Data\Customer
  48. */
  49. protected $customerDataMock;
  50. /**
  51. * @var \Magento\Customer\Controller\Adminhtml\Locks\Unlock
  52. */
  53. protected $controller;
  54. /**
  55. * Init mocks for tests
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. $this->objectManager = new ObjectManager($this);
  61. $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
  68. ->setMethods(['getParam'])
  69. ->getMockForAbstractClass();
  70. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  71. $this->resultFactoryMock = $this->createPartialMock(
  72. \Magento\Framework\Controller\ResultFactory::class,
  73. ['create']
  74. );
  75. $this->redirectMock = $this->createPartialMock(\Magento\Backend\Model\View\Result\Redirect::class, ['setPath']);
  76. $this->customerDataMock = $this->createMock(\Magento\Customer\Model\Data\Customer::class);
  77. $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
  78. ->setMethods(['getObjectManager', 'getResultFactory', 'getMessageManager', 'getRequest'])
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $this->contextMock->expects($this->any())
  82. ->method('getRequest')
  83. ->willReturn($this->requestMock);
  84. $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
  85. $this->contextMock->expects($this->any())->method('getResultFactory')->willReturn($this->resultFactoryMock);
  86. $this->resultFactoryMock->expects($this->once())->method('create')->willReturn($this->redirectMock);
  87. $this->controller = $this->objectManager->getObject(
  88. \Magento\Customer\Controller\Adminhtml\Locks\Unlock::class,
  89. [
  90. 'context' => $this->contextMock,
  91. 'authentication' => $this->authenticationMock,
  92. ]
  93. );
  94. }
  95. /**
  96. * @return void
  97. */
  98. public function testExecute()
  99. {
  100. $customerId = 1;
  101. $this->requestMock->expects($this->once())
  102. ->method('getParam')
  103. ->with($this->equalTo('customer_id'))
  104. ->will($this->returnValue($customerId));
  105. $this->authenticationMock->expects($this->once())->method('unlock')->with($customerId);
  106. $this->messageManagerMock->expects($this->once())->method('addSuccess');
  107. $this->redirectMock->expects($this->once())
  108. ->method('setPath')
  109. ->with($this->equalTo('customer/index/edit'))
  110. ->willReturnSelf();
  111. $this->assertInstanceOf(\Magento\Backend\Model\View\Result\Redirect::class, $this->controller->execute());
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function testExecuteWithException()
  117. {
  118. $customerId = 1;
  119. $phrase = new \Magento\Framework\Phrase('some error');
  120. $this->requestMock->expects($this->once())
  121. ->method('getParam')
  122. ->with($this->equalTo('customer_id'))
  123. ->will($this->returnValue($customerId));
  124. $this->authenticationMock->expects($this->once())
  125. ->method('unlock')
  126. ->with($customerId)
  127. ->willThrowException(new \Exception($phrase));
  128. $this->messageManagerMock->expects($this->once())->method('addError');
  129. $this->controller->execute();
  130. }
  131. }