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 \UrlFactory ;
22
+ use PHPUnit \Framework \MockObject \MockObject ;
23
+
10
24
/**
11
25
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12
26
*/
13
27
class SessionTest extends \PHPUnit \Framework \TestCase
14
28
{
15
29
/**
16
- * @var \PHPUnit_Framework_MockObject_MockObject
30
+ * @var ResourceCustomer|MockObject
31
+ */
32
+ protected $ _customerResourceMock ;
33
+
34
+ /**
35
+ * @var Storage|MockObject
17
36
*/
18
37
protected $ _storageMock ;
19
38
20
39
/**
21
- * @var \PHPUnit_Framework_MockObject_MockObject
40
+ * @var ManagerInterface|MockObject
22
41
*/
23
42
protected $ _eventManagerMock ;
24
43
25
44
/**
26
- * @var \PHPUnit_Framework_MockObject_MockObject
45
+ * @var Context|MockObject
27
46
*/
28
47
protected $ _httpContextMock ;
29
48
30
49
/**
31
- * @var \Magento\Framework\ UrlFactory|\PHPUnit_Framework_MockObject_MockObject
50
+ * @var UrlFactory|MockObject
32
51
*/
33
52
protected $ urlFactoryMock ;
34
53
35
54
/**
36
- * @var \Magento\Customer\Model\ CustomerFactory|\PHPUnit_Framework_MockObject_MockObject
55
+ * @var CustomerFactory|MockObject
37
56
*/
38
57
protected $ customerFactoryMock ;
39
58
40
59
/**
41
- * @var \Magento\Customer\Api\ CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
60
+ * @var CustomerRepositoryInterface|MockObject
42
61
*/
43
62
protected $ customerRepositoryMock ;
44
63
45
64
/**
46
- * @var \Magento\Framework\App\Response\ Http|\PHPUnit_Framework_MockObject_MockObject
65
+ * @var Http|MockObject
47
66
*/
48
67
protected $ responseMock ;
49
68
50
69
/**
51
- * @var \Magento\Customer\Model\ Session
70
+ * @var Session
52
71
*/
53
72
protected $ _model ;
54
73
@@ -58,21 +77,25 @@ class SessionTest extends \PHPUnit\Framework\TestCase
58
77
protected function setUp ()
59
78
{
60
79
$ this ->_storageMock = $ this ->createPartialMock (
61
- \ Magento \ Customer \ Model \ Session \ Storage::class,
80
+ Storage::class,
62
81
['getIsCustomerEmulated ' , 'getData ' , 'unsIsCustomerEmulated ' , '__sleep ' , '__wakeup ' ]
63
82
);
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)
83
+ $ this ->_eventManagerMock = $ this ->createMock (ManagerInterface::class);
84
+ $ this ->_httpContextMock = $ this ->createMock (Context::class);
85
+ $ this ->urlFactoryMock = $ this ->createMock (UrlFactory::class);
86
+ $ this ->customerFactoryMock = $ this ->getMockBuilder (CustomerFactory::class)
68
87
->disableOriginalConstructor ()
69
- ->setMethods (['create ' , ' save ' ])
88
+ ->setMethods (['create ' ])
70
89
->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);
90
+ $ this ->_customerResourceMock = $ this ->getMockBuilder (ResourceCustomer::class)
91
+ ->disableOriginalConstructor ()
92
+ ->setMethods (['load ' ])
93
+ ->getMock ();
94
+ $ this ->customerRepositoryMock = $ this ->createMock (CustomerRepositoryInterface::class);
95
+ $ helper = new ObjectManagerHelper ($ this );
96
+ $ this ->responseMock = $ this ->createMock (Http::class);
74
97
$ this ->_model = $ helper ->getObject (
75
- \ Magento \ Customer \ Model \ Session::class,
98
+ Session::class,
76
99
[
77
100
'customerFactory ' => $ this ->customerFactoryMock ,
78
101
'storage ' => $ this ->_storageMock ,
@@ -81,6 +104,7 @@ protected function setUp()
81
104
'urlFactory ' => $ this ->urlFactoryMock ,
82
105
'customerRepository ' => $ this ->customerRepositoryMock ,
83
106
'response ' => $ this ->responseMock ,
107
+ '_customerResource ' => $ this ->_customerResourceMock ,
84
108
]
85
109
);
86
110
}
@@ -90,8 +114,8 @@ protected function setUp()
90
114
*/
91
115
public function testSetCustomerAsLoggedIn ()
92
116
{
93
- $ customer = $ this ->createMock (\ Magento \ Customer \ Model \ Customer::class);
94
- $ customerDto = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
117
+ $ customer = $ this ->createMock (Customer::class);
118
+ $ customerDto = $ this ->createMock (CustomerInterface::class);
95
119
$ customer ->expects ($ this ->any ())
96
120
->method ('getDataModel ' )
97
121
->will ($ this ->returnValue ($ customerDto ));
@@ -113,8 +137,8 @@ public function testSetCustomerAsLoggedIn()
113
137
*/
114
138
public function testSetCustomerDataAsLoggedIn ()
115
139
{
116
- $ customer = $ this ->createMock (\ Magento \ Customer \ Model \ Customer::class);
117
- $ customerDto = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
140
+ $ customer = $ this ->createMock (Customer::class);
141
+ $ customerDto = $ this ->createMock (CustomerInterface::class);
118
142
119
143
$ this ->customerFactoryMock ->expects ($ this ->once ())
120
144
->method ('create ' )
@@ -185,19 +209,22 @@ public function testLoginById()
185
209
*/
186
210
protected function prepareLoginDataMock ($ customerId )
187
211
{
188
- $ customerDataMock = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
212
+ $ customerDataMock = $ this ->createMock (CustomerInterface::class);
189
213
$ customerDataMock ->expects ($ this ->once ())
190
214
->method ('getId ' )
191
215
->will ($ this ->returnValue ($ customerId ));
192
216
193
217
$ customerMock = $ this ->createPartialMock (
194
- \ Magento \ Customer \ Model \ Customer::class,
195
- ['getId ' , 'getConfirmation ' , 'updateData ' , 'getGroupId ' ]
218
+ Customer::class,
219
+ ['getId ' , 'isConfirmationRequired ' , ' getConfirmation ' , 'updateData ' , 'getGroupId ' ]
196
220
);
197
- $ customerMock ->expects ($ this ->exactly ( 3 ))
221
+ $ customerMock ->expects ($ this ->once ( ))
198
222
->method ('getId ' )
199
223
->will ($ this ->returnValue ($ customerId ));
200
224
$ customerMock ->expects ($ this ->once ())
225
+ ->method ('isConfirmationRequired ' )
226
+ ->will ($ this ->returnValue (true ));
227
+ $ customerMock ->expects ($ this ->never ())
201
228
->method ('getConfirmation ' )
202
229
->will ($ this ->returnValue ($ customerId ));
203
230
@@ -259,8 +286,59 @@ public function getIsLoggedInDataProvider()
259
286
*/
260
287
public function testSetCustomerRemovesFlagThatShowsIfCustomerIsEmulated ()
261
288
{
262
- $ customerMock = $ this ->createMock (\ Magento \ Customer \ Model \ Customer::class);
289
+ $ customerMock = $ this ->createMock (Customer::class);
263
290
$ this ->_storageMock ->expects ($ this ->once ())->method ('unsIsCustomerEmulated ' );
264
291
$ this ->_model ->setCustomer ($ customerMock );
265
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