60 $this->_storageMock = $this->createPartialMock(
62 [
'getIsCustomerEmulated',
'getData',
'unsIsCustomerEmulated',
'__sleep',
'__wakeup']
64 $this->_eventManagerMock = $this->createMock(\
Magento\Framework\Event\ManagerInterface::class);
65 $this->_httpContextMock = $this->createMock(\
Magento\Framework\
App\Http\Context::class);
66 $this->urlFactoryMock = $this->createMock(\
Magento\Framework\UrlFactory::class);
67 $this->customerFactoryMock = $this->getMockBuilder(\
Magento\
Customer\Model\CustomerFactory::class)
68 ->disableOriginalConstructor()
69 ->setMethods([
'create'])
71 $this->customerRepositoryMock = $this->createMock(\
Magento\
Customer\Api\CustomerRepositoryInterface::class);
72 $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
74 $this->_model =
$helper->getObject(
77 'customerFactory' => $this->customerFactoryMock,
78 'storage' => $this->_storageMock,
79 'eventManager' => $this->_eventManagerMock,
80 'httpContext' => $this->_httpContextMock,
81 'urlFactory' => $this->urlFactoryMock,
82 'customerRepository' => $this->customerRepositoryMock,
83 'response' => $this->responseMock,
94 $customerDto = $this->createMock(\
Magento\
Customer\Api\Data\CustomerInterface::class);
96 ->method(
'getDataModel')
97 ->will($this->returnValue($customerDto));
99 $this->_eventManagerMock->expects($this->at(0))
101 ->with(
'customer_login', [
'customer' =>
$customer]);
102 $this->_eventManagerMock->expects($this->at(1))
104 ->with(
'customer_data_object_login', [
'customer' => $customerDto]);
107 $this->_model->setCustomerAsLoggedIn(
$customer);
108 $this->assertSame(
$customer, $this->_model->getCustomer());
117 $customerDto = $this->createMock(\
Magento\
Customer\Api\Data\CustomerInterface::class);
119 $this->customerFactoryMock->expects($this->once())
123 ->method(
'updateData')
125 ->will($this->returnSelf());
127 $this->_eventManagerMock->expects($this->at(0))
129 ->with(
'customer_login', [
'customer' =>
$customer]);
130 $this->_eventManagerMock->expects($this->at(1))
132 ->with(
'customer_data_object_login', [
'customer' => $customerDto]);
134 $this->_model->setCustomerDataAsLoggedIn($customerDto);
135 $this->assertSame(
$customer, $this->_model->getCustomer());
143 $urlMock = $this->createMock(\
Magento\Framework\Url::class);
144 $urlMock->expects($this->exactly(2))
147 $urlMock->expects($this->once())
148 ->method(
'getRebuiltUrl')
150 $this->urlFactoryMock->expects($this->exactly(4))
152 ->willReturn($urlMock);
153 $urlMock->expects($this->once())
154 ->method(
'getUseSession')
157 $this->responseMock->expects($this->once())
158 ->method(
'setRedirect')
162 $this->assertFalse($this->_model->authenticate());
174 $this->customerRepositoryMock->expects($this->once())
177 ->will($this->returnValue($customerDataMock));
179 $this->assertTrue($this->_model->loginById(
$customerId));
188 $customerDataMock = $this->createMock(\
Magento\
Customer\Api\Data\CustomerInterface::class);
189 $customerDataMock->expects($this->once())
193 $customerMock = $this->createPartialMock(
195 [
'getId',
'isConfirmationRequired',
'getConfirmation',
'updateData',
'getGroupId']
197 $customerMock->expects($this->once())
200 $customerMock->expects($this->once())
201 ->method(
'isConfirmationRequired')
202 ->will($this->returnValue(
true));
203 $customerMock->expects($this->never())
204 ->method(
'getConfirmation')
207 $this->customerFactoryMock->expects($this->once())
209 ->will($this->returnValue($customerMock));
210 $customerMock->expects($this->once())
211 ->method(
'updateData')
212 ->with($customerDataMock)
213 ->will($this->returnSelf());
215 $this->_httpContextMock->expects($this->exactly(3))
216 ->method(
'setValue');
217 return $customerDataMock;
226 public function testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated)
229 $this->_storageMock->expects($this->any())->method(
'getData')->with(
'customer_id')
232 if ($isCustomerIdValid) {
233 $this->customerRepositoryMock->expects($this->once())
237 $this->customerRepositoryMock->expects($this->once())
240 ->will($this->throwException(
new \Exception(
'Customer ID is invalid.')));
242 $this->_storageMock->expects($this->any())->method(
'getIsCustomerEmulated')
243 ->will($this->returnValue($isCustomerEmulated));
244 $this->assertEquals($expectedResult, $this->_model->isLoggedIn());
253 [
'expectedResult' =>
true,
'isCustomerIdValid' =>
true,
'isCustomerEmulated' =>
false],
254 [
'expectedResult' =>
false,
'isCustomerIdValid' =>
true,
'isCustomerEmulated' =>
true],
255 [
'expectedResult' =>
false,
'isCustomerIdValid' =>
false,
'isCustomerEmulated' =>
false],
256 [
'expectedResult' =>
false,
'isCustomerIdValid' =>
false,
'isCustomerEmulated' =>
true],
265 $customerMock = $this->createMock(\
Magento\
Customer\Model\Customer::class);
266 $this->_storageMock->expects($this->once())->method(
'unsIsCustomerEmulated');
267 $this->_model->setCustomer($customerMock);
getIsLoggedInDataProvider()
testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated()
testSetCustomerDataAsLoggedIn()
testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated)
prepareLoginDataMock($customerId)
testSetCustomerAsLoggedIn()