Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UnlockTest.php
Go to the documentation of this file.
1 <?php
7 
10 
16 class UnlockTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $contextMock;
22 
29 
33  protected $objectManager;
34 
38  protected $requestMock;
39 
44 
48  protected $resultFactoryMock;
49 
53  protected $redirectMock;
54 
58  protected $customerDataMock;
59 
63  protected $controller;
64 
69  public function setUp()
70  {
71  $this->objectManager = new ObjectManager($this);
72  $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->authenticationMock = $this->getMockBuilder(AuthenticationInterface::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
80  ->setMethods(['getParam'])
81  ->getMockForAbstractClass();
82  $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
83  $this->resultFactoryMock = $this->createPartialMock(
84  \Magento\Framework\Controller\ResultFactory::class,
85  ['create']
86  );
87  $this->redirectMock = $this->createPartialMock(\Magento\Backend\Model\View\Result\Redirect::class, ['setPath']);
88  $this->customerDataMock = $this->createMock(\Magento\Customer\Model\Data\Customer::class);
89  $this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
90  ->setMethods(['getObjectManager', 'getResultFactory', 'getMessageManager', 'getRequest'])
91  ->disableOriginalConstructor()
92  ->getMock();
93 
94  $this->contextMock->expects($this->any())
95  ->method('getRequest')
96  ->willReturn($this->requestMock);
97  $this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
98  $this->contextMock->expects($this->any())->method('getResultFactory')->willReturn($this->resultFactoryMock);
99  $this->resultFactoryMock->expects($this->once())->method('create')->willReturn($this->redirectMock);
100 
101  $this->controller = $this->objectManager->getObject(
102  \Magento\Customer\Controller\Adminhtml\Locks\Unlock::class,
103  [
104  'context' => $this->contextMock,
105  'authentication' => $this->authenticationMock,
106  ]
107  );
108  }
109 
113  public function testExecute()
114  {
115  $customerId = 1;
116  $this->requestMock->expects($this->once())
117  ->method('getParam')
118  ->with($this->equalTo('customer_id'))
119  ->will($this->returnValue($customerId));
120  $this->authenticationMock->expects($this->once())->method('unlock')->with($customerId);
121  $this->messageManagerMock->expects($this->once())->method('addSuccess');
122  $this->redirectMock->expects($this->once())
123  ->method('setPath')
124  ->with($this->equalTo('customer/index/edit'))
125  ->willReturnSelf();
126  $this->assertInstanceOf(\Magento\Backend\Model\View\Result\Redirect::class, $this->controller->execute());
127  }
128 
132  public function testExecuteWithException()
133  {
134  $customerId = 1;
135  $phrase = new \Magento\Framework\Phrase('some error');
136  $this->requestMock->expects($this->once())
137  ->method('getParam')
138  ->with($this->equalTo('customer_id'))
139  ->will($this->returnValue($customerId));
140  $this->authenticationMock->expects($this->once())
141  ->method('unlock')
142  ->with($customerId)
143  ->willThrowException(new \Exception($phrase));
144  $this->messageManagerMock->expects($this->once())->method('addError');
145  $this->controller->execute();
146  }
147 }