Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SessionTest.php
Go to the documentation of this file.
1 <?php
9 
13 class SessionTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $_storageMock;
19 
23  protected $_eventManagerMock;
24 
28  protected $_httpContextMock;
29 
33  protected $urlFactoryMock;
34 
39 
44 
48  protected $responseMock;
49 
53  protected $_model;
54 
58  protected function setUp()
59  {
60  $this->_storageMock = $this->createPartialMock(
61  \Magento\Customer\Model\Session\Storage::class,
62  ['getIsCustomerEmulated', 'getData', 'unsIsCustomerEmulated', '__sleep', '__wakeup']
63  );
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'])
70  ->getMock();
71  $this->customerRepositoryMock = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
72  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
73  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
74  $this->_model = $helper->getObject(
75  \Magento\Customer\Model\Session::class,
76  [
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,
84  ]
85  );
86  }
87 
91  public function testSetCustomerAsLoggedIn()
92  {
93  $customer = $this->createMock(\Magento\Customer\Model\Customer::class);
94  $customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
95  $customer->expects($this->any())
96  ->method('getDataModel')
97  ->will($this->returnValue($customerDto));
98 
99  $this->_eventManagerMock->expects($this->at(0))
100  ->method('dispatch')
101  ->with('customer_login', ['customer' => $customer]);
102  $this->_eventManagerMock->expects($this->at(1))
103  ->method('dispatch')
104  ->with('customer_data_object_login', ['customer' => $customerDto]);
105 
106  $_SESSION = [];
107  $this->_model->setCustomerAsLoggedIn($customer);
108  $this->assertSame($customer, $this->_model->getCustomer());
109  }
110 
115  {
116  $customer = $this->createMock(\Magento\Customer\Model\Customer::class);
117  $customerDto = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
118 
119  $this->customerFactoryMock->expects($this->once())
120  ->method('create')
121  ->will($this->returnValue($customer));
122  $customer->expects($this->once())
123  ->method('updateData')
124  ->with($customerDto)
125  ->will($this->returnSelf());
126 
127  $this->_eventManagerMock->expects($this->at(0))
128  ->method('dispatch')
129  ->with('customer_login', ['customer' => $customer]);
130  $this->_eventManagerMock->expects($this->at(1))
131  ->method('dispatch')
132  ->with('customer_data_object_login', ['customer' => $customerDto]);
133 
134  $this->_model->setCustomerDataAsLoggedIn($customerDto);
135  $this->assertSame($customer, $this->_model->getCustomer());
136  }
137 
141  public function testAuthenticate()
142  {
143  $urlMock = $this->createMock(\Magento\Framework\Url::class);
144  $urlMock->expects($this->exactly(2))
145  ->method('getUrl')
146  ->willReturn('');
147  $urlMock->expects($this->once())
148  ->method('getRebuiltUrl')
149  ->willReturn('');
150  $this->urlFactoryMock->expects($this->exactly(4))
151  ->method('create')
152  ->willReturn($urlMock);
153  $urlMock->expects($this->once())
154  ->method('getUseSession')
155  ->willReturn(false);
156 
157  $this->responseMock->expects($this->once())
158  ->method('setRedirect')
159  ->with('')
160  ->willReturn('');
161 
162  $this->assertFalse($this->_model->authenticate());
163  }
164 
168  public function testLoginById()
169  {
170  $customerId = 1;
171 
172  $customerDataMock = $this->prepareLoginDataMock($customerId);
173 
174  $this->customerRepositoryMock->expects($this->once())
175  ->method('getById')
176  ->with($this->equalTo($customerId))
177  ->will($this->returnValue($customerDataMock));
178 
179  $this->assertTrue($this->_model->loginById($customerId));
180  }
181 
186  protected function prepareLoginDataMock($customerId)
187  {
188  $customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
189  $customerDataMock->expects($this->once())
190  ->method('getId')
191  ->will($this->returnValue($customerId));
192 
193  $customerMock = $this->createPartialMock(
194  \Magento\Customer\Model\Customer::class,
195  ['getId', 'isConfirmationRequired', 'getConfirmation', 'updateData', 'getGroupId']
196  );
197  $customerMock->expects($this->once())
198  ->method('getId')
199  ->will($this->returnValue($customerId));
200  $customerMock->expects($this->once())
201  ->method('isConfirmationRequired')
202  ->will($this->returnValue(true));
203  $customerMock->expects($this->never())
204  ->method('getConfirmation')
205  ->will($this->returnValue($customerId));
206 
207  $this->customerFactoryMock->expects($this->once())
208  ->method('create')
209  ->will($this->returnValue($customerMock));
210  $customerMock->expects($this->once())
211  ->method('updateData')
212  ->with($customerDataMock)
213  ->will($this->returnSelf());
214 
215  $this->_httpContextMock->expects($this->exactly(3))
216  ->method('setValue');
217  return $customerDataMock;
218  }
219 
226  public function testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated)
227  {
228  $customerId = 1;
229  $this->_storageMock->expects($this->any())->method('getData')->with('customer_id')
230  ->will($this->returnValue($customerId));
231 
232  if ($isCustomerIdValid) {
233  $this->customerRepositoryMock->expects($this->once())
234  ->method('getById')
235  ->with($customerId);
236  } else {
237  $this->customerRepositoryMock->expects($this->once())
238  ->method('getById')
239  ->with($customerId)
240  ->will($this->throwException(new \Exception('Customer ID is invalid.')));
241  }
242  $this->_storageMock->expects($this->any())->method('getIsCustomerEmulated')
243  ->will($this->returnValue($isCustomerEmulated));
244  $this->assertEquals($expectedResult, $this->_model->isLoggedIn());
245  }
246 
250  public function getIsLoggedInDataProvider()
251  {
252  return [
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],
257  ];
258  }
259 
264  {
265  $customerMock = $this->createMock(\Magento\Customer\Model\Customer::class);
266  $this->_storageMock->expects($this->once())->method('unsIsCustomerEmulated');
267  $this->_model->setCustomer($customerMock);
268  }
269 }
$helper
Definition: iframe.phtml:13
$customer
Definition: customers.php:11
testIsLoggedIn($expectedResult, $isCustomerIdValid, $isCustomerEmulated)