10
10
11
11
use Magento \Customer \Helper \Address ;
12
12
use Magento \Customer \Model \Url ;
13
+ use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
14
+ use Magento \Framework \Stdlib \Cookie \PhpCookieManager ;
15
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
13
16
use Magento \Store \Model \ScopeInterface ;
17
+ use Magento \Customer \Controller \Account \Confirm ;
18
+ use Magento \Customer \Model \Session ;
19
+ use Magento \Framework \App \RequestInterface ;
20
+ use Magento \Framework \App \Response \Http ;
21
+ use Magento \Framework \App \ViewInterface ;
22
+ use Magento \Framework \App \Response \RedirectInterface ;
23
+ use Magento \Framework \UrlFactory ;
24
+ use Magento \Customer \Api \AccountManagementInterface ;
25
+ use Magento \Customer \Api \Data \CustomerInterface ;
26
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
27
+ use Magento \Framework \Message \Manager ;
28
+ use Magento \Store \Model \StoreManager ;
29
+ use Magento \Store \Model \Store ;
30
+ use Magento \Framework \Controller \Result \Redirect ;
31
+ use Magento \Framework \Controller \ResultFactory ;
32
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
33
+ use Magento \Framework \App \Action \Context ;
14
34
15
35
/**
16
36
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -98,51 +118,53 @@ class ConfirmTest extends \PHPUnit_Framework_TestCase
98
118
*/
99
119
protected $ redirectResultMock ;
100
120
121
+ /** @var PhpCookieManager | \PHPUnit_Framework_MockObject_MockObject */
122
+ private $ cookieMetadataManager ;
123
+
124
+ /** @var CookieMetadataFactory | \PHPUnit_Framework_MockObject_MockObject */
125
+ private $ cookieMetadataFactory ;
126
+
101
127
protected function setUp ()
102
128
{
103
- $ this ->customerSessionMock = $ this ->getMock ('\Magento\Customer\Model\Session ' , [], [], '' , false );
104
- $ this ->requestMock = $ this ->getMock ('Magento\Framework\App\RequestInterface ' , [], [], '' , false );
105
- $ this ->responseMock = $ this ->getMock (
106
- 'Magento\Framework\App\Response\Http ' , ['setRedirect ' , '__wakeup ' ], [], '' , false
107
- );
108
- $ viewMock = $ this ->getMock ('Magento\Framework\App\ViewInterface ' );
109
- $ this ->redirectMock = $ this ->getMock ('Magento\Framework\App\Response\RedirectInterface ' );
110
-
111
- $ this ->urlMock = $ this ->getMock ('Magento\Framework\Url ' , [], [], '' , false );
112
- $ urlFactoryMock = $ this ->getMock ('Magento\Framework\UrlFactory ' , [], [], '' , false );
129
+ $ this ->customerSessionMock = $ this ->createDefaultMock (Session::class);
130
+ $ this ->requestMock = $ this ->createDefaultMock (RequestInterface::class);
131
+ $ this ->responseMock = $ this ->getMockBuilder (Http::class)
132
+ ->disableOriginalConstructor ()
133
+ ->setMethods (['setRedirect ' , '__wakeup ' ])
134
+ ->getMock ();
135
+ $ viewMock = $ this ->createDefaultMock (ViewInterface::class);
136
+ $ this ->redirectMock = $ this ->createDefaultMock (RedirectInterface::class);
137
+
138
+ $ this ->urlMock = $ this ->createDefaultMock (\Magento \Framework \Url::class);
139
+ $ urlFactoryMock = $ this ->createDefaultMock (UrlFactory::class);
113
140
$ urlFactoryMock ->expects ($ this ->any ())
114
141
->method ('create ' )
115
142
->will ($ this ->returnValue ($ this ->urlMock ));
116
143
117
- $ this ->customerAccountManagementMock =
118
- $ this ->getMockForAbstractClass ('Magento\Customer\Api\AccountManagementInterface ' );
119
- $ this ->customerDataMock = $ this ->getMock (
120
- 'Magento\Customer\Api\Data\CustomerInterface ' , [], [], '' , false
121
- );
144
+ $ this ->customerAccountManagementMock = $ this ->getMockBuilder (AccountManagementInterface::class)
145
+ ->getMockForAbstractClass ();
146
+ $ this ->customerDataMock = $ this ->createDefaultMock (CustomerInterface::class);
122
147
123
- $ this ->customerRepositoryMock =
124
- $ this ->getMockForAbstractClass ('Magento\Customer\Api\CustomerRepositoryInterface ' );
125
-
126
- $ this ->messageManagerMock = $ this ->getMock ('Magento\Framework\Message\Manager ' , [], [], '' , false );
127
- $ this ->addressHelperMock = $ this ->getMock ('Magento\Customer\Helper\Address ' , [], [], '' , false );
128
- $ this ->storeManagerMock = $ this ->getMock ('Magento\Store\Model\StoreManager ' , [], [], '' , false );
129
- $ this ->storeMock = $ this ->getMock ('Magento\Store\Model\Store ' , [], [], '' , false );
130
- $ this ->redirectResultMock = $ this ->getMock ('Magento\Framework\Controller\Result\Redirect ' , [], [], '' , false );
131
-
132
- $ resultFactoryMock = $ this ->getMock (
133
- 'Magento\Framework\Controller\ResultFactory ' ,
134
- ['create ' ],
135
- [],
136
- '' ,
137
- false
138
- );
148
+ $ this ->customerRepositoryMock = $ this ->getMockBuilder (CustomerRepositoryInterface::class)
149
+ ->getMockForAbstractClass ();
150
+
151
+ $ this ->messageManagerMock = $ this ->createDefaultMock (Manager::class);
152
+ $ this ->addressHelperMock = $ this ->createDefaultMock (Address::class);
153
+ $ this ->storeManagerMock = $ this ->createDefaultMock (StoreManager::class);
154
+ $ this ->storeMock = $ this ->createDefaultMock (Store::class);
155
+ $ this ->redirectResultMock = $ this ->createDefaultMock (Redirect::class);
156
+
157
+ $ resultFactoryMock = $ this ->getMockBuilder (ResultFactory::class)
158
+ ->disableOriginalConstructor ()
159
+ ->setMethods (['create ' ])
160
+ ->getMock ();
139
161
$ resultFactoryMock ->expects ($ this ->once ())
140
162
->method ('create ' )
141
- ->with (\ Magento \ Framework \ Controller \ ResultFactory::TYPE_REDIRECT )
163
+ ->with (ResultFactory::TYPE_REDIRECT )
142
164
->willReturn ($ this ->redirectResultMock );
143
165
144
- $ this ->scopeConfigMock = $ this ->getMock ( ' Magento\Framework\App\Config\ ScopeConfigInterface' );
145
- $ this ->contextMock = $ this ->getMock ( ' Magento\Framework\App\Action\ Context' , [], [], '' , false );
166
+ $ this ->scopeConfigMock = $ this ->createDefaultMock ( ScopeConfigInterface::class );
167
+ $ this ->contextMock = $ this ->createDefaultMock ( Context::class );
146
168
$ this ->contextMock ->expects ($ this ->any ())
147
169
->method ('getRequest ' )
148
170
->willReturn ($ this ->requestMock );
@@ -162,10 +184,31 @@ protected function setUp()
162
184
->method ('getResultFactory ' )
163
185
->willReturn ($ resultFactoryMock );
164
186
165
- $ objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
187
+ $ this ->cookieMetadataFactory = $ this ->getMockBuilder (CookieMetadataFactory::class)
188
+ ->disableOriginalConstructor ()
189
+ ->setMethods (['setPath ' , 'createCookieMetadata ' ])
190
+ ->getMock ();
166
191
192
+ $ cookieMetaData = $ this ->getMockBuilder (\Magento \Framework \Stdlib \Cookie \CookieMetadata::class)
193
+ ->disableOriginalConstructor ()
194
+ ->getMock ();
195
+
196
+ $ cookieMetaData ->expects ($ this ->any ())
197
+ ->method ('setPath ' )
198
+ ->will ($ this ->returnSelf ());
199
+
200
+ $ this ->cookieMetadataFactory ->expects ($ this ->any ())
201
+ ->method ('createCookieMetadata ' )
202
+ ->will ($ this ->returnValue ($ cookieMetaData ));
203
+
204
+ /** @var PhpCookieManager | \PHPUnit_Framework_MockObject_MockObject $cookieMetadataManager */
205
+ $ this ->cookieMetadataManager = $ this ->getMockBuilder (PhpCookieManager::class)
206
+ ->disableOriginalConstructor ()
207
+ ->getMock ();
208
+
209
+ $ objectManagerHelper = new ObjectManager ($ this );
167
210
$ this ->model = $ objectManagerHelper ->getObject (
168
- ' Magento\Customer\Controller\Account\ Confirm' ,
211
+ Confirm::class ,
169
212
[
170
213
'context ' => $ this ->contextMock ,
171
214
'customerSession ' => $ this ->customerSessionMock ,
@@ -175,6 +218,8 @@ protected function setUp()
175
218
'customerRepository ' => $ this ->customerRepositoryMock ,
176
219
'addressHelper ' => $ this ->addressHelperMock ,
177
220
'urlFactory ' => $ urlFactoryMock ,
221
+ 'cookieMetadataFactory ' => $ this ->cookieMetadataFactory ,
222
+ 'cookieMetadataManager ' => $ this ->cookieMetadataManager ,
178
223
]
179
224
);
180
225
}
@@ -190,7 +235,7 @@ public function testIsLoggedIn()
190
235
->with ('*/*/ ' )
191
236
->willReturnSelf ();
192
237
193
- $ this ->assertInstanceOf (' Magento\Framework\Controller\Result\ Redirect' , $ this ->model ->execute ());
238
+ $ this ->assertInstanceOf (Redirect::class , $ this ->model ->execute ());
194
239
}
195
240
196
241
/**
@@ -232,7 +277,7 @@ public function testNoCustomerIdInRequest($customerId, $key)
232
277
->with ($ this ->equalTo ($ testUrl ))
233
278
->willReturnSelf ();
234
279
235
- $ this ->assertInstanceOf (' Magento\Framework\Controller\Result\ Redirect' , $ this ->model ->execute ());
280
+ $ this ->assertInstanceOf (Redirect::class , $ this ->model ->execute ());
236
281
}
237
282
238
283
/**
@@ -442,4 +487,84 @@ public function getSuccessRedirectDataProvider()
442
487
],
443
488
];
444
489
}
490
+
491
+ /**
492
+ * @param string $className
493
+ * @return \PHPUnit_Framework_MockObject_MockObject
494
+ */
495
+ protected function createDefaultMock ($ className )
496
+ {
497
+ return $ this ->getMockBuilder ($ className )
498
+ ->disableOriginalConstructor ()
499
+ ->getMock ();
500
+ }
501
+
502
+ /**
503
+ * Tests cookie cleaning
504
+ *
505
+ * @dataProvider dataProviderClean
506
+ * @param mixed $cookieValue
507
+ * @param \PHPUnit_Framework_MockObject_Matcher_Invocation $deleteMatcher
508
+ */
509
+ public function testClean ($ cookieValue , \PHPUnit_Framework_MockObject_Matcher_Invocation $ deleteMatcher )
510
+ {
511
+ $ this ->customerSessionMock ->expects ($ this ->once ())
512
+ ->method ('isLoggedIn ' )
513
+ ->will ($ this ->returnValue (false ));
514
+
515
+ $ this ->requestMock ->expects ($ this ->any ())
516
+ ->method ('getParam ' )
517
+ ->willReturnMap ([
518
+ ['id ' , false , 'id ' ],
519
+ ['key ' , false , 'key ' ],
520
+ ]);
521
+
522
+ $ this ->customerDataMock ->expects ($ this ->once ())
523
+ ->method ('getEmail ' )
524
+ ->will ($ this ->returnValue ('email@exmple.com ' ));
525
+
526
+ $ this ->customerRepositoryMock ->expects ($ this ->any ())
527
+ ->method ('getById ' )
528
+ ->will ($ this ->returnValue ($ this ->customerDataMock ));
529
+
530
+ $ this ->customerAccountManagementMock ->expects ($ this ->once ())
531
+ ->method ('activate ' );
532
+
533
+ $ this ->storeMock ->expects ($ this ->any ())
534
+ ->method ('getFrontendName ' )
535
+ ->will ($ this ->returnValue ('frontend ' ));
536
+ $ this ->storeManagerMock ->expects ($ this ->any ())
537
+ ->method ('getStore ' )
538
+ ->will ($ this ->returnValue ($ this ->storeMock ));
539
+
540
+ $ this ->cookieMetadataManager ->expects ($ this ->once ())
541
+ ->method ('getCookie ' )
542
+ ->will ($ this ->returnValue ($ cookieValue ));
543
+
544
+ $ this ->cookieMetadataManager ->expects ($ deleteMatcher )
545
+ ->method ('deleteCookie ' )
546
+ ->will ($ this ->returnValue (null ));
547
+
548
+ /** @noinspection PhpUnhandledExceptionInspection */
549
+ $ this ->model ->execute ();
550
+ }
551
+
552
+ /**
553
+ * Provides data for testing clean method
554
+ *
555
+ * @return array
556
+ */
557
+ public function dataProviderClean ()
558
+ {
559
+ return [
560
+ 'clean-cookie ' => [
561
+ 'testValue ' ,
562
+ $ this ->once ()
563
+ ],
564
+ 'no-clean ' => [
565
+ null ,
566
+ $ this ->never ()
567
+ ],
568
+ ];
569
+ }
445
570
}
0 commit comments