13
13
use Magento \Framework \Exception \NotFoundException ;
14
14
use Magento \Framework \Message \ManagerInterface ;
15
15
use Magento \Framework \ObjectManagerInterface ;
16
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
16
17
use Magento \Wishlist \Controller \Index \Update ;
17
18
use Magento \Wishlist \Controller \WishlistProviderInterface ;
18
19
use Magento \Wishlist \Helper \Data ;
19
20
use Magento \Wishlist \Model \Item ;
20
21
use Magento \Wishlist \Model \LocaleQuantityProcessor ;
22
+ use Magento \Wishlist \Model \Wishlist ;
21
23
use PHPUnit \Framework \TestCase ;
24
+ use PHPUnit_Framework_MockObject_MockObject as MockObject ;
22
25
23
26
/**
24
27
* Test for upate controller wishlist
27
30
class UpdateTest extends TestCase
28
31
{
29
32
/**
30
- * @var Validator $formKeyValidator
33
+ * Wishlist item id
34
+ *
35
+ * @var int
36
+ */
37
+ private const ITEM_ID = 1 ;
38
+
39
+ /**
40
+ * Product qty for wishlist
41
+ *
42
+ * @var int
31
43
*/
32
- private $ formKeyValidator ;
44
+ private const WISHLIST_PRODUCT_QTY = 21 ;
33
45
34
46
/**
35
- * @var WishlistProviderInterface $wishlistProvider
47
+ * @var MockObject|Validator $formKeyValidatorMock
36
48
*/
37
- private $ wishlistProvider ;
49
+ private $ formKeyValidatorMock ;
38
50
39
51
/**
40
- * @var LocaleQuantityProcessor $quantityProcessor
52
+ * @var MockObject|WishlistProviderInterface $wishlistProviderMock
41
53
*/
42
- private $ quantityProcessor ;
54
+ private $ wishlistProviderMock ;
55
+
56
+ /**
57
+ * @var MockObject|LocaleQuantityProcessor $quantityProcessorMock
58
+ */
59
+ private $ quantityProcessorMock ;
43
60
44
61
/**
45
62
* @var Update $updateController
46
63
*/
47
64
private $ updateController ;
48
65
49
66
/**
50
- * @var $context
67
+ * @var MockObject|Context$contextMock
51
68
*/
52
- private $ context ;
69
+ private $ contextMock ;
53
70
54
71
/**
55
- * @var Redirect $resultRedirect
72
+ * @var MockObject| Redirect $resultRedirectMock
56
73
*/
57
- private $ resultRedirect ;
74
+ private $ resultRedirectMock ;
58
75
59
76
/**
60
- * @var ResultFactory $resultFatory
77
+ * @var MockObject| ResultFactory $resultFatoryMock
61
78
*/
62
- private $ resultFactory ;
79
+ private $ resultFactoryMock ;
63
80
64
81
/**
65
- * @var RequestInterface $requestMock
82
+ * @var MockObject| RequestInterface $requestMock
66
83
*/
67
84
private $ requestMock ;
68
85
69
86
/**
70
- * @var ObjectManagerInterface $objectManagerMock
87
+ * @var MockObject| ObjectManagerInterface $objectManagerMock
71
88
*/
72
89
private $ objectManagerMock ;
73
90
74
91
/**
75
- * @var ManagerInterface $messageManager
92
+ * @var MockObject| ManagerInterface $messageManagerMock
76
93
*/
77
- private $ messageManager ;
94
+ private $ messageManagerMock ;
78
95
79
96
/**
80
97
* @inheritdoc
81
98
*/
82
99
protected function setUp ()
83
100
{
84
- $ this ->formKeyValidator = $ this ->createMock (Validator::class);
85
- $ this ->wishlistProvider = $ this ->createMock (WishlistProviderInterface::class);
86
- $ this ->quantityProcessor = $ this ->createMock (LocaleQuantityProcessor::class);
87
- $ this ->context = $ this ->createMock (Context::class);
88
- $ this ->resultRedirect = $ this ->createMock (Redirect::class);
89
- $ this ->resultFactory = $ this ->createPartialMock (ResultFactory::class, ['create ' ]);
101
+ $ this ->formKeyValidatorMock = $ this ->createMock (Validator::class);
102
+ $ this ->wishlistProviderMock = $ this ->createMock (WishlistProviderInterface::class);
103
+ $ this ->quantityProcessorMock = $ this ->createMock (LocaleQuantityProcessor::class);
104
+ $ this ->contextMock = $ this ->createMock (Context::class);
105
+ $ this ->resultRedirectMock = $ this ->createMock (Redirect::class);
106
+ $ this ->resultFactoryMock = $ this ->createPartialMock (ResultFactory::class, ['create ' ]);
107
+ $ this ->messageManagerMock = $ this ->createMock (ManagerInterface::class);
108
+ $ this ->objectManagerMock = $ this ->createMock (ObjectManagerInterface::class);
90
109
$ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
91
110
->setMethods (['getPostValue ' ])
92
111
->getMockForAbstractClass ();
93
- $ this ->objectManagerMock = $ this ->createMock (ObjectManagerInterface::class);
94
112
95
- $ this ->context ->expects ($ this ->once ())
113
+ $ this ->resultFactoryMock ->expects ($ this ->any ())
114
+ ->method ('create ' )
115
+ ->willReturn ($ this ->resultRedirectMock );
116
+ $ this ->contextMock ->expects ($ this ->once ())
96
117
->method ('getResultFactory ' )
97
- ->willReturn ($ this ->resultFactory );
98
- $ this ->context ->expects ($ this ->once ())
118
+ ->willReturn ($ this ->resultFactoryMock );
119
+ $ this ->contextMock ->expects ($ this ->once ())
99
120
->method ('getObjectManager ' )
100
121
->willReturn ($ this ->objectManagerMock );
101
-
102
- $ this ->resultFactory ->expects ($ this ->any ())
103
- ->method ('create ' )
104
- ->willReturn ($ this ->resultRedirect );
105
- $ this ->context ->expects ($ this ->any ())
122
+ $ this ->contextMock ->expects ($ this ->any ())
106
123
->method ('getRequest ' )
107
124
->willReturn ($ this ->requestMock );
108
-
109
- $ this ->messageManager = $ this ->createMock (ManagerInterface::class);
110
- $ this ->context ->expects ($ this ->any ())
125
+ $ this ->contextMock ->expects ($ this ->any ())
111
126
->method ('getMessageManager ' )
112
- ->willReturn ($ this ->messageManager );
127
+ ->willReturn ($ this ->messageManagerMock );
113
128
114
- $ this ->updateController = new Update (
115
- $ this ->context ,
116
- $ this ->formKeyValidator ,
117
- $ this ->wishlistProvider ,
118
- $ this ->quantityProcessor
129
+ $ this ->updateController = (new ObjectManagerHelper ($ this ))->getObject (
130
+ Update::class,
131
+ [
132
+ 'context ' => $ this ->contextMock ,
133
+ '_formKeyValidator ' => $ this ->formKeyValidatorMock ,
134
+ 'wishlistProvider ' => $ this ->wishlistProviderMock ,
135
+ 'quantityProcessor ' => $ this ->quantityProcessorMock
136
+ ]
119
137
);
120
138
}
121
139
122
140
/**
123
141
* Test for update method Wishlist controller.
124
142
*
125
143
* @dataProvider getWishlistDataProvider
144
+ * @param array $wishlistDataProvider
145
+ * @param array $postData
126
146
* @return void
127
147
*/
128
- public function testUpdate (array $ wishlistDataProvider ): void
148
+ public function testUpdate (array $ wishlistDataProvider, array $ postData ): void
129
149
{
130
- $ this ->formKeyValidator ->expects ($ this ->once ())
131
- ->method ('validate ' )
132
- ->willReturn (true );
133
-
134
- $ wishlist = $ this ->createMock (\Magento \Wishlist \Model \Wishlist::class);
135
-
136
- $ this ->wishlistProvider ->expects ($ this ->once ())
137
- ->method ('getWishlist ' )
138
- ->willReturn ($ wishlist );
139
- $ wishlist ->expects ($ this ->exactly (2 ))
140
- ->method ('getId ' )
141
- ->willReturn ($ wishlistDataProvider ['wishlist_data ' ]['id ' ]);
142
- $ this ->requestMock ->expects ($ this ->once ())
143
- ->method ('getPostValue ' )
144
- ->willReturn ($ wishlistDataProvider ['post_data ' ]);
145
- $ this ->resultRedirect ->expects ($ this ->once ())
146
- ->method ('setPath ' )
147
- ->with ('* ' , ['wishlist_id ' => $ wishlistDataProvider ['wishlist_data ' ]['id ' ]]);
150
+ $ wishlist = $ this ->createMock (Wishlist::class);
148
151
$ itemMock = $ this ->getMockBuilder (Item::class)
149
152
->disableOriginalConstructor ()
150
153
->setMethods (
@@ -160,7 +163,27 @@ public function testUpdate(array $wishlistDataProvider): void
160
163
'getName '
161
164
]
162
165
)->getMock ();
166
+ $ dataMock = $ this ->createMock (Data::class);
167
+ $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
168
+ ->disableOriginalConstructor ()
169
+ ->getMock ();
163
170
171
+ $ this ->formKeyValidatorMock ->expects ($ this ->once ())
172
+ ->method ('validate ' )
173
+ ->with ($ this ->requestMock )
174
+ ->willReturn (true );
175
+ $ this ->wishlistProviderMock ->expects ($ this ->once ())
176
+ ->method ('getWishlist ' )
177
+ ->willReturn ($ wishlist );
178
+ $ wishlist ->expects ($ this ->exactly (2 ))
179
+ ->method ('getId ' )
180
+ ->willReturn ($ wishlistDataProvider ['id ' ]);
181
+ $ this ->requestMock ->expects ($ this ->once ())
182
+ ->method ('getPostValue ' )
183
+ ->willReturn ($ postData );
184
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
185
+ ->method ('setPath ' )
186
+ ->with ('* ' , ['wishlist_id ' => $ wishlistDataProvider ['id ' ]]);
164
187
$ this ->objectManagerMock ->expects ($ this ->once ())
165
188
->method ('create ' )
166
189
->with (Item::class)
@@ -171,7 +194,7 @@ public function testUpdate(array $wishlistDataProvider): void
171
194
->willReturnSelf ();
172
195
$ itemMock ->expects ($ this ->once ())
173
196
->method ('getWishLIstId ' )
174
- ->willReturn ($ wishlistDataProvider ['wishlist_data ' ][ ' id ' ]);
197
+ ->willReturn ($ wishlistDataProvider ['id ' ]);
175
198
$ itemMock ->expects ($ this ->once ())
176
199
->method ('getDescription ' )
177
200
->willReturn ('' );
@@ -181,8 +204,6 @@ public function testUpdate(array $wishlistDataProvider): void
181
204
$ itemMock ->expects ($ this ->once ())
182
205
->method ('setQty ' )
183
206
->willReturnSelf ();
184
- $ dataMock = $ this ->createMock (Data::class);
185
-
186
207
$ this ->objectManagerMock ->expects ($ this ->exactly (2 ))
187
208
->method ('get ' )
188
209
->with (Data::class)
@@ -192,33 +213,62 @@ public function testUpdate(array $wishlistDataProvider): void
192
213
->willReturn ('' );
193
214
$ dataMock ->expects ($ this ->once ())
194
215
->method ('calculate ' );
195
- $ this ->quantityProcessor ->expects ($ this ->once ())
216
+ $ this ->quantityProcessorMock ->expects ($ this ->once ())
196
217
->method ('process ' )
197
- ->willReturn ($ wishlistDataProvider ['post_data ' ]['qty ' ]);
198
-
199
- $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
200
- ->disableOriginalConstructor ()
201
- ->getMock ();
218
+ ->willReturn ($ postData ['qty ' ]);
202
219
$ itemMock ->expects ($ this ->once ())
203
220
->method ('getProduct ' )
204
221
->willReturn ($ productMock );
205
222
$ productMock ->expects ($ this ->once ())
206
223
->method ('getName ' )
207
224
->willReturn ('product ' );
208
- $ this ->messageManager ->expects ($ this ->once ())
225
+ $ this ->messageManagerMock ->expects ($ this ->once ())
209
226
->method ('addSuccessMessage ' );
210
- $ this ->assertEquals ($ this ->resultRedirect , $ this ->updateController ->execute ());
227
+
228
+ $ this ->assertEquals ($ this ->resultRedirectMock , $ this ->updateController ->execute ());
229
+ }
230
+
231
+ /**
232
+ * Verify update method if post data not available
233
+ *
234
+ * @dataProvider getWishlistDataProvider
235
+ * @param array $wishlistDataProvider
236
+ * @return void
237
+ */
238
+ public function testUpdateRedirectWhenNoPostData (array $ wishlistDataProvider ): void
239
+ {
240
+ $ wishlist = $ this ->createMock (Wishlist::class);
241
+
242
+ $ this ->formKeyValidatorMock ->expects ($ this ->once ())
243
+ ->method ('validate ' )
244
+ ->willReturn (true );
245
+ $ this ->wishlistProviderMock ->expects ($ this ->once ())
246
+ ->method ('getWishlist ' )
247
+ ->willReturn ($ wishlist );
248
+ $ wishlist ->expects ($ this ->exactly (1 ))
249
+ ->method ('getId ' )
250
+ ->willReturn ($ wishlistDataProvider ['id ' ]);
251
+ $ this ->resultRedirectMock ->expects ($ this ->once ())
252
+ ->method ('setPath ' )
253
+ ->with ('* ' , ['wishlist_id ' => $ wishlistDataProvider ['id ' ]]);
254
+ $ this ->requestMock ->expects ($ this ->once ())
255
+ ->method ('getPostValue ' )
256
+ ->willReturn (null );
257
+
258
+ $ this ->assertEquals ($ this ->resultRedirectMock , $ this ->updateController ->execute ());
211
259
}
212
260
213
261
/**
214
262
* Check if wishlist not availbale, and exception is shown
263
+ *
264
+ * @return void
215
265
*/
216
- public function testUpdateWithNotFoundException ()
266
+ public function testUpdateThrowsNotFoundExceptionWhenWishlistDoNotExist (): void
217
267
{
218
- $ this ->formKeyValidator ->expects ($ this ->once ())
268
+ $ this ->formKeyValidatorMock ->expects ($ this ->once ())
219
269
->method ('validate ' )
220
270
->willReturn (true );
221
- $ this ->wishlistProvider ->expects ($ this ->once ())
271
+ $ this ->wishlistProviderMock ->expects ($ this ->once ())
222
272
->method ('getWishlist ' )
223
273
->willReturn (null );
224
274
$ this ->expectException (NotFoundException::class);
@@ -232,21 +282,17 @@ public function testUpdateWithNotFoundException()
232
282
*/
233
283
public function getWishlistDataProvider (): array
234
284
{
235
- return [
285
+ return
236
286
[
237
287
[
238
- 'wishlist_data ' => [
239
- 'id ' => 1 ,
240
-
288
+ [
289
+ 'id ' => self ::ITEM_ID
241
290
],
242
- 'post_data ' => [
243
- 'qty ' => [1 => 12 ],
244
- 'description ' => [
245
- 1 => 'Description for item_id 1 '
246
- ]
291
+ [
292
+ 'qty ' => [self ::ITEM_ID => self ::WISHLIST_PRODUCT_QTY ],
293
+ 'description ' => [self ::ITEM_ID => 'Description for item_id 1 ' ]
247
294
]
248
295
]
249
- ]
250
- ];
296
+ ];
251
297
}
252
298
}
0 commit comments