10
10
use Magento \Framework \App \RequestInterface ;
11
11
use Magento \Framework \Controller \ResultFactory ;
12
12
use Magento \Framework \Data \Form \FormKey \Validator ;
13
+ use Magento \Framework \Exception \NotFoundException ;
14
+ use Magento \Framework \Message \ManagerInterface ;
15
+ use Magento \Framework \ObjectManagerInterface ;
13
16
use Magento \Wishlist \Controller \Index \Update ;
14
17
use Magento \Wishlist \Controller \WishlistProviderInterface ;
18
+ use Magento \Wishlist \Helper \Data ;
19
+ use Magento \Wishlist \Model \Item ;
15
20
use Magento \Wishlist \Model \LocaleQuantityProcessor ;
16
21
use PHPUnit \Framework \TestCase ;
17
22
18
23
/**
19
24
* Test for upate controller wishlist
25
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
26
*/
21
27
class UpdateTest extends TestCase
22
28
{
@@ -60,6 +66,16 @@ class UpdateTest extends TestCase
60
66
*/
61
67
private $ requestMock ;
62
68
69
+ /**
70
+ * @var ObjectManagerInterface $objectManagerMock
71
+ */
72
+ private $ objectManagerMock ;
73
+
74
+ /**
75
+ * @var ManagerInterface $messageManager
76
+ */
77
+ private $ messageManager ;
78
+
63
79
/**
64
80
* @inheritdoc
65
81
*/
@@ -74,10 +90,14 @@ protected function setUp()
74
90
$ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
75
91
->setMethods (['getPostValue ' ])
76
92
->getMockForAbstractClass ();
93
+ $ this ->objectManagerMock = $ this ->createMock (ObjectManagerInterface::class);
77
94
78
95
$ this ->context ->expects ($ this ->once ())
79
- ->method ('getResultFactory ' )
80
- ->willReturn ($ this ->resultFactory );
96
+ ->method ('getResultFactory ' )
97
+ ->willReturn ($ this ->resultFactory );
98
+ $ this ->context ->expects ($ this ->once ())
99
+ ->method ('getObjectManager ' )
100
+ ->willReturn ($ this ->objectManagerMock );
81
101
82
102
$ this ->resultFactory ->expects ($ this ->any ())
83
103
->method ('create ' )
@@ -86,6 +106,11 @@ protected function setUp()
86
106
->method ('getRequest ' )
87
107
->willReturn ($ this ->requestMock );
88
108
109
+ $ this ->messageManager = $ this ->createMock (ManagerInterface::class);
110
+ $ this ->context ->expects ($ this ->any ())
111
+ ->method ('getMessageManager ' )
112
+ ->willReturn ($ this ->messageManager );
113
+
89
114
$ this ->updateController = new Update (
90
115
$ this ->context ,
91
116
$ this ->formKeyValidator ,
@@ -97,23 +122,131 @@ protected function setUp()
97
122
/**
98
123
* Test for update method Wishlist controller.
99
124
*
100
- * Check if there is not post value result redirect returned.
101
- *
125
+ * @dataProvider getWishlistDataProvider
102
126
* @return void
103
127
*/
104
- public function testUpdate (): void
128
+ public function testUpdate (array $ wishlistDataProvider ): void
105
129
{
106
130
$ this ->formKeyValidator ->expects ($ this ->once ())
107
- ->method ('validate ' )
108
- ->willReturn (true );
131
+ ->method ('validate ' )
132
+ ->willReturn (true );
109
133
110
134
$ wishlist = $ this ->createMock (\Magento \Wishlist \Model \Wishlist::class);
135
+
111
136
$ this ->wishlistProvider ->expects ($ this ->once ())
112
137
->method ('getWishlist ' )
113
138
->willReturn ($ wishlist );
139
+ $ wishlist ->expects ($ this ->exactly (2 ))
140
+ ->method ('getId ' )
141
+ ->willReturn ($ wishlistDataProvider ['wishlist_data ' ]['id ' ]);
114
142
$ this ->requestMock ->expects ($ this ->once ())
115
143
->method ('getPostValue ' )
116
- ->willReturn (null );
144
+ ->willReturn ($ wishlistDataProvider ['post_data ' ]);
145
+ $ this ->resultRedirect ->expects ($ this ->once ())
146
+ ->method ('setPath ' )
147
+ ->with ('* ' , ['wishlist_id ' => $ wishlistDataProvider ['wishlist_data ' ]['id ' ]]);
148
+ $ itemMock = $ this ->getMockBuilder (Item::class)
149
+ ->disableOriginalConstructor ()
150
+ ->setMethods (
151
+ [
152
+ 'load ' ,
153
+ 'getId ' ,
154
+ 'getWishlistId ' ,
155
+ 'setQty ' ,
156
+ 'save ' ,
157
+ 'getDescription ' ,
158
+ 'setDescription ' ,
159
+ 'getProduct ' ,
160
+ 'getName '
161
+ ]
162
+ )->getMock ();
163
+
164
+ $ this ->objectManagerMock ->expects ($ this ->once ())
165
+ ->method ('create ' )
166
+ ->with (Item::class)
167
+ ->willReturn ($ itemMock );
168
+ $ itemMock ->expects ($ this ->once ())
169
+ ->method ('load ' )
170
+ ->with (1 )
171
+ ->willReturnSelf ();
172
+ $ itemMock ->expects ($ this ->once ())
173
+ ->method ('getWishLIstId ' )
174
+ ->willReturn ($ wishlistDataProvider ['wishlist_data ' ]['id ' ]);
175
+ $ itemMock ->expects ($ this ->once ())
176
+ ->method ('getDescription ' )
177
+ ->willReturn ('' );
178
+ $ itemMock ->expects ($ this ->once ())
179
+ ->method ('setDescription ' )
180
+ ->willReturnSelf ();
181
+ $ itemMock ->expects ($ this ->once ())
182
+ ->method ('setQty ' )
183
+ ->willReturnSelf ();
184
+ $ dataMock = $ this ->createMock (Data::class);
185
+
186
+ $ this ->objectManagerMock ->expects ($ this ->exactly (2 ))
187
+ ->method ('get ' )
188
+ ->with (Data::class)
189
+ ->willReturn ($ dataMock );
190
+ $ dataMock ->expects ($ this ->once ())
191
+ ->method ('defaultCommentString ' )
192
+ ->willReturn ('' );
193
+ $ dataMock ->expects ($ this ->once ())
194
+ ->method ('calculate ' );
195
+ $ this ->quantityProcessor ->expects ($ this ->once ())
196
+ ->method ('process ' )
197
+ ->willReturn ($ wishlistDataProvider ['post_data ' ]['qty ' ]);
198
+
199
+ $ productMock = $ this ->getMockBuilder (\Magento \Catalog \Model \Product::class)
200
+ ->disableOriginalConstructor ()
201
+ ->getMock ();
202
+ $ itemMock ->expects ($ this ->once ())
203
+ ->method ('getProduct ' )
204
+ ->willReturn ($ productMock );
205
+ $ productMock ->expects ($ this ->once ())
206
+ ->method ('getName ' )
207
+ ->willReturn ('product ' );
208
+ $ this ->messageManager ->expects ($ this ->once ())
209
+ ->method ('addSuccessMessage ' );
117
210
$ this ->assertEquals ($ this ->resultRedirect , $ this ->updateController ->execute ());
118
211
}
212
+
213
+ /**
214
+ * Check if wishlist not availbale, and exception is shown
215
+ */
216
+ public function testUpdateWithNotFoundException ()
217
+ {
218
+ $ this ->formKeyValidator ->expects ($ this ->once ())
219
+ ->method ('validate ' )
220
+ ->willReturn (true );
221
+ $ this ->wishlistProvider ->expects ($ this ->once ())
222
+ ->method ('getWishlist ' )
223
+ ->willReturn (null );
224
+ $ this ->expectException (NotFoundException::class);
225
+ $ this ->updateController ->execute ();
226
+ }
227
+
228
+ /**
229
+ * Dataprovider for Update test
230
+ *
231
+ * @return array
232
+ */
233
+ public function getWishlistDataProvider (): array
234
+ {
235
+ return [
236
+ [
237
+ [
238
+ 'wishlist_data ' => [
239
+ 'id ' => 1 ,
240
+
241
+ ],
242
+ 'post_data ' => [
243
+ 'qty ' => [1 => 12 ],
244
+ 'description ' => [
245
+ 1 => 'Description for item_id 1 '
246
+ ]
247
+ ]
248
+ ]
249
+ ]
250
+ ];
251
+ }
119
252
}
0 commit comments