Skip to content

Commit 74a244f

Browse files
committed
Refactor per review comments, add new test case
1 parent 3c3018d commit 74a244f

File tree

1 file changed

+130
-84
lines changed

1 file changed

+130
-84
lines changed

app/code/Magento/Wishlist/Test/Unit/Controller/Index/UpdateTest.php

Lines changed: 130 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
use Magento\Framework\Exception\NotFoundException;
1414
use Magento\Framework\Message\ManagerInterface;
1515
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1617
use Magento\Wishlist\Controller\Index\Update;
1718
use Magento\Wishlist\Controller\WishlistProviderInterface;
1819
use Magento\Wishlist\Helper\Data;
1920
use Magento\Wishlist\Model\Item;
2021
use Magento\Wishlist\Model\LocaleQuantityProcessor;
22+
use Magento\Wishlist\Model\Wishlist;
2123
use PHPUnit\Framework\TestCase;
24+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
2225

2326
/**
2427
* Test for upate controller wishlist
@@ -27,124 +30,124 @@
2730
class UpdateTest extends TestCase
2831
{
2932
/**
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
3143
*/
32-
private $formKeyValidator;
44+
private const WISHLIST_PRODUCT_QTY = 21;
3345

3446
/**
35-
* @var WishlistProviderInterface $wishlistProvider
47+
* @var MockObject|Validator $formKeyValidatorMock
3648
*/
37-
private $wishlistProvider;
49+
private $formKeyValidatorMock;
3850

3951
/**
40-
* @var LocaleQuantityProcessor $quantityProcessor
52+
* @var MockObject|WishlistProviderInterface $wishlistProviderMock
4153
*/
42-
private $quantityProcessor;
54+
private $wishlistProviderMock;
55+
56+
/**
57+
* @var MockObject|LocaleQuantityProcessor $quantityProcessorMock
58+
*/
59+
private $quantityProcessorMock;
4360

4461
/**
4562
* @var Update $updateController
4663
*/
4764
private $updateController;
4865

4966
/**
50-
* @var $context
67+
* @var MockObject|Context$contextMock
5168
*/
52-
private $context;
69+
private $contextMock;
5370

5471
/**
55-
* @var Redirect $resultRedirect
72+
* @var MockObject|Redirect $resultRedirectMock
5673
*/
57-
private $resultRedirect;
74+
private $resultRedirectMock;
5875

5976
/**
60-
* @var ResultFactory $resultFatory
77+
* @var MockObject|ResultFactory $resultFatoryMock
6178
*/
62-
private $resultFactory;
79+
private $resultFactoryMock;
6380

6481
/**
65-
* @var RequestInterface $requestMock
82+
* @var MockObject|RequestInterface $requestMock
6683
*/
6784
private $requestMock;
6885

6986
/**
70-
* @var ObjectManagerInterface $objectManagerMock
87+
* @var MockObject|ObjectManagerInterface $objectManagerMock
7188
*/
7289
private $objectManagerMock;
7390

7491
/**
75-
* @var ManagerInterface $messageManager
92+
* @var MockObject|ManagerInterface $messageManagerMock
7693
*/
77-
private $messageManager;
94+
private $messageManagerMock;
7895

7996
/**
8097
* @inheritdoc
8198
*/
8299
protected function setUp()
83100
{
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);
90109
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
91110
->setMethods(['getPostValue'])
92111
->getMockForAbstractClass();
93-
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
94112

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())
96117
->method('getResultFactory')
97-
->willReturn($this->resultFactory);
98-
$this->context->expects($this->once())
118+
->willReturn($this->resultFactoryMock);
119+
$this->contextMock->expects($this->once())
99120
->method('getObjectManager')
100121
->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())
106123
->method('getRequest')
107124
->willReturn($this->requestMock);
108-
109-
$this->messageManager = $this->createMock(ManagerInterface::class);
110-
$this->context->expects($this->any())
125+
$this->contextMock->expects($this->any())
111126
->method('getMessageManager')
112-
->willReturn($this->messageManager);
127+
->willReturn($this->messageManagerMock);
113128

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+
]
119137
);
120138
}
121139

122140
/**
123141
* Test for update method Wishlist controller.
124142
*
125143
* @dataProvider getWishlistDataProvider
144+
* @param array $wishlistDataProvider
145+
* @param array $postData
126146
* @return void
127147
*/
128-
public function testUpdate(array $wishlistDataProvider): void
148+
public function testUpdate(array $wishlistDataProvider, array $postData): void
129149
{
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);
148151
$itemMock = $this->getMockBuilder(Item::class)
149152
->disableOriginalConstructor()
150153
->setMethods(
@@ -160,7 +163,27 @@ public function testUpdate(array $wishlistDataProvider): void
160163
'getName'
161164
]
162165
)->getMock();
166+
$dataMock = $this->createMock(Data::class);
167+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
168+
->disableOriginalConstructor()
169+
->getMock();
163170

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']]);
164187
$this->objectManagerMock->expects($this->once())
165188
->method('create')
166189
->with(Item::class)
@@ -171,7 +194,7 @@ public function testUpdate(array $wishlistDataProvider): void
171194
->willReturnSelf();
172195
$itemMock->expects($this->once())
173196
->method('getWishLIstId')
174-
->willReturn($wishlistDataProvider['wishlist_data']['id']);
197+
->willReturn($wishlistDataProvider['id']);
175198
$itemMock->expects($this->once())
176199
->method('getDescription')
177200
->willReturn('');
@@ -181,8 +204,6 @@ public function testUpdate(array $wishlistDataProvider): void
181204
$itemMock->expects($this->once())
182205
->method('setQty')
183206
->willReturnSelf();
184-
$dataMock = $this->createMock(Data::class);
185-
186207
$this->objectManagerMock->expects($this->exactly(2))
187208
->method('get')
188209
->with(Data::class)
@@ -192,33 +213,62 @@ public function testUpdate(array $wishlistDataProvider): void
192213
->willReturn('');
193214
$dataMock->expects($this->once())
194215
->method('calculate');
195-
$this->quantityProcessor->expects($this->once())
216+
$this->quantityProcessorMock->expects($this->once())
196217
->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']);
202219
$itemMock->expects($this->once())
203220
->method('getProduct')
204221
->willReturn($productMock);
205222
$productMock->expects($this->once())
206223
->method('getName')
207224
->willReturn('product');
208-
$this->messageManager->expects($this->once())
225+
$this->messageManagerMock->expects($this->once())
209226
->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());
211259
}
212260

213261
/**
214262
* Check if wishlist not availbale, and exception is shown
263+
*
264+
* @return void
215265
*/
216-
public function testUpdateWithNotFoundException()
266+
public function testUpdateThrowsNotFoundExceptionWhenWishlistDoNotExist(): void
217267
{
218-
$this->formKeyValidator->expects($this->once())
268+
$this->formKeyValidatorMock->expects($this->once())
219269
->method('validate')
220270
->willReturn(true);
221-
$this->wishlistProvider->expects($this->once())
271+
$this->wishlistProviderMock->expects($this->once())
222272
->method('getWishlist')
223273
->willReturn(null);
224274
$this->expectException(NotFoundException::class);
@@ -232,21 +282,17 @@ public function testUpdateWithNotFoundException()
232282
*/
233283
public function getWishlistDataProvider(): array
234284
{
235-
return [
285+
return
236286
[
237287
[
238-
'wishlist_data' => [
239-
'id' => 1,
240-
288+
[
289+
'id' => self::ITEM_ID
241290
],
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']
247294
]
248295
]
249-
]
250-
];
296+
];
251297
}
252298
}

0 commit comments

Comments
 (0)