7
7
*/
8
8
namespace Magento \Customer \Test \Unit \Model ;
9
9
10
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
11
+ use Magento \Customer \Api \Data \CustomerInterface ;
12
+ use Magento \Customer \Model \Customer ;
13
+ use Magento \Customer \Model \CustomerFactory ;
14
+ use Magento \Customer \Model \ResourceModel \Customer as ResourceCustomer ;
15
+ use Magento \Customer \Model \Session ;
16
+ use Magento \Customer \Model \Session \Storage ;
17
+ use Magento \Framework \App \Http \Context ;
18
+ use Magento \Framework \App \Response \Http ;
19
+ use Magento \Framework \Event \ManagerInterface ;
20
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
21
+ use Magento \Framework \Url ;
22
+ use Magento \Framework \UrlFactory ;
23
+ use PHPUnit \Framework \MockObject \MockObject ;
24
+ use PHPUnit \Framework \TestCase ;
25
+
10
26
/**
11
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
28
*/
13
- class SessionTest extends \ PHPUnit \ Framework \ TestCase
29
+ class SessionTest extends TestCase
14
30
{
15
31
/**
16
- * @var \PHPUnit_Framework_MockObject_MockObject
32
+ * @var ResourceCustomer|MockObject
33
+ */
34
+ protected $ _customerResourceMock ;
35
+
36
+ /**
37
+ * @var Storage|MockObject
17
38
*/
18
39
protected $ _storageMock ;
19
40
20
41
/**
21
- * @var \PHPUnit_Framework_MockObject_MockObject
42
+ * @var ManagerInterface|MockObject
22
43
*/
23
44
protected $ _eventManagerMock ;
24
45
25
46
/**
26
- * @var \PHPUnit_Framework_MockObject_MockObject
47
+ * @var Context|MockObject
27
48
*/
28
49
protected $ _httpContextMock ;
29
50
30
51
/**
31
- * @var \Magento\Framework\ UrlFactory|\PHPUnit_Framework_MockObject_MockObject
52
+ * @var UrlFactory|MockObject
32
53
*/
33
54
protected $ urlFactoryMock ;
34
55
35
56
/**
36
- * @var \Magento\Customer\Model\ CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
57
+ * @var CustomerFactory|MockObject
37
58
*/
38
59
protected $ customerFactoryMock ;
39
60
40
61
/**
41
- * @var \Magento\Customer\Api\ CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
62
+ * @var CustomerRepositoryInterface|MockObject
42
63
*/
43
64
protected $ customerRepositoryMock ;
44
65
45
66
/**
46
- * @var \Magento\Framework\App\Response\ Http|\PHPUnit_Framework_MockObject_MockObject
67
+ * @var Http|MockObject
47
68
*/
48
69
protected $ responseMock ;
49
70
50
71
/**
51
- * @var \Magento\Customer\Model\ Session
72
+ * @var Session
52
73
*/
53
74
protected $ _model ;
54
75
@@ -58,21 +79,25 @@ class SessionTest extends \PHPUnit\Framework\TestCase
58
79
protected function setUp ()
59
80
{
60
81
$ this ->_storageMock = $ this ->createPartialMock (
61
- \ Magento \ Customer \ Model \ Session \ Storage::class,
82
+ Storage::class,
62
83
['getIsCustomerEmulated ' , 'getData ' , 'unsIsCustomerEmulated ' , '__sleep ' , '__wakeup ' ]
63
84
);
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)
85
+ $ this ->_eventManagerMock = $ this ->createMock (ManagerInterface::class);
86
+ $ this ->_httpContextMock = $ this ->createMock (Context::class);
87
+ $ this ->urlFactoryMock = $ this ->createMock (UrlFactory::class);
88
+ $ this ->customerFactoryMock = $ this ->getMockBuilder (CustomerFactory::class)
68
89
->disableOriginalConstructor ()
69
90
->setMethods (['create ' , 'save ' ])
70
91
->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);
92
+ $ this ->_customerResourceMock = $ this ->getMockBuilder (ResourceCustomer::class)
93
+ ->disableOriginalConstructor ()
94
+ ->setMethods (['load ' , 'save ' ])
95
+ ->getMock ();
96
+ $ this ->customerRepositoryMock = $ this ->createMock (CustomerRepositoryInterface::class);
97
+ $ helper = new ObjectManagerHelper ($ this );
98
+ $ this ->responseMock = $ this ->createMock (Http::class);
74
99
$ this ->_model = $ helper ->getObject (
75
- \ Magento \ Customer \ Model \ Session::class,
100
+ Session::class,
76
101
[
77
102
'customerFactory ' => $ this ->customerFactoryMock ,
78
103
'storage ' => $ this ->_storageMock ,
@@ -81,6 +106,7 @@ protected function setUp()
81
106
'urlFactory ' => $ this ->urlFactoryMock ,
82
107
'customerRepository ' => $ this ->customerRepositoryMock ,
83
108
'response ' => $ this ->responseMock ,
109
+ '_customerResource ' => $ this ->_customerResourceMock ,
84
110
]
85
111
);
86
112
}
@@ -90,8 +116,8 @@ protected function setUp()
90
116
*/
91
117
public function testSetCustomerAsLoggedIn ()
92
118
{
93
- $ customer = $ this ->createMock (\ Magento \ Customer \ Model \ Customer::class);
94
- $ customerDto = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
119
+ $ customer = $ this ->createMock (Customer::class);
120
+ $ customerDto = $ this ->createMock (CustomerInterface::class);
95
121
$ customer ->expects ($ this ->any ())
96
122
->method ('getDataModel ' )
97
123
->will ($ this ->returnValue ($ customerDto ));
@@ -113,8 +139,8 @@ public function testSetCustomerAsLoggedIn()
113
139
*/
114
140
public function testSetCustomerDataAsLoggedIn ()
115
141
{
116
- $ customer = $ this ->createMock (\ Magento \ Customer \ Model \ Customer::class);
117
- $ customerDto = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
142
+ $ customer = $ this ->createMock (Customer::class);
143
+ $ customerDto = $ this ->createMock (CustomerInterface::class);
118
144
119
145
$ this ->customerFactoryMock ->expects ($ this ->once ())
120
146
->method ('create ' )
@@ -140,7 +166,7 @@ public function testSetCustomerDataAsLoggedIn()
140
166
*/
141
167
public function testAuthenticate ()
142
168
{
143
- $ urlMock = $ this ->createMock (\ Magento \ Framework \ Url::class);
169
+ $ urlMock = $ this ->createMock (Url::class);
144
170
$ urlMock ->expects ($ this ->exactly (2 ))
145
171
->method ('getUrl ' )
146
172
->willReturn ('' );
@@ -185,13 +211,13 @@ public function testLoginById()
185
211
*/
186
212
protected function prepareLoginDataMock ($ customerId )
187
213
{
188
- $ customerDataMock = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
214
+ $ customerDataMock = $ this ->createMock (CustomerInterface::class);
189
215
$ customerDataMock ->expects ($ this ->once ())
190
216
->method ('getId ' )
191
217
->will ($ this ->returnValue ($ customerId ));
192
218
193
219
$ customerMock = $ this ->createPartialMock (
194
- \ Magento \ Customer \ Model \ Customer::class,
220
+ Customer::class,
195
221
['getId ' , 'getConfirmation ' , 'updateData ' , 'getGroupId ' ]
196
222
);
197
223
$ customerMock ->expects ($ this ->exactly (3 ))
@@ -259,8 +285,60 @@ public function getIsLoggedInDataProvider()
259
285
*/
260
286
public function testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated ()
261
287
{
262
- $ customerMock = $ this ->createMock (\ Magento \ Customer \ Model \ Customer::class);
288
+ $ customerMock = $ this ->createMock (Customer::class);
263
289
$ this ->_storageMock ->expects ($ this ->once ())->method ('unsIsCustomerEmulated ' );
264
290
$ this ->_model ->setCustomer ($ customerMock );
265
291
}
292
+
293
+ /**
294
+ * Test "getCustomer()" for guest user
295
+ *
296
+ * @return void
297
+ */
298
+ public function testGetCustomerForGuestUser ()
299
+ {
300
+ $ customerMock = $ this ->getMockBuilder (Customer::class)
301
+ ->disableOriginalConstructor ()
302
+ ->getMock ();
303
+
304
+ $ this ->customerFactoryMock
305
+ ->expects ($ this ->once ())
306
+ ->method ('create ' )
307
+ ->will ($ this ->returnValue ($ customerMock ));
308
+
309
+ $ this ->assertSame ($ customerMock , $ this ->_model ->getCustomer ());
310
+ }
311
+
312
+ /**
313
+ * Test "getCustomer()" for registered user
314
+ *
315
+ * @return void
316
+ */
317
+ public function testGetCustomerForRegisteredUser ()
318
+ {
319
+ $ customerId = 1 ;
320
+
321
+ $ customerMock = $ this ->getMockBuilder (Customer::class)
322
+ ->disableOriginalConstructor ()
323
+ ->getMock ();
324
+
325
+ $ this ->customerFactoryMock
326
+ ->expects ($ this ->once ())
327
+ ->method ('create ' )
328
+ ->will ($ this ->returnValue ($ customerMock ));
329
+
330
+ $ this ->_storageMock
331
+ ->expects ($ this ->exactly (4 ))
332
+ ->method ('getData ' )
333
+ ->with ('customer_id ' )
334
+ ->willReturn ($ customerId );
335
+
336
+ $ this ->_customerResourceMock
337
+ ->expects ($ this ->once ())
338
+ ->method ('load ' )
339
+ ->with ($ customerMock , $ customerId )
340
+ ->will ($ this ->returnValue ($ customerMock ));
341
+
342
+ $ this ->assertSame ($ customerMock , $ this ->_model ->getCustomer ());
343
+ }
266
344
}
0 commit comments