Skip to content

Commit 3c3018d

Browse files
committed
Cover changes with unit test
1 parent 21bdc67 commit 3c3018d

File tree

1 file changed

+141
-8
lines changed

1 file changed

+141
-8
lines changed

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

Lines changed: 141 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
use Magento\Framework\App\RequestInterface;
1111
use Magento\Framework\Controller\ResultFactory;
1212
use Magento\Framework\Data\Form\FormKey\Validator;
13+
use Magento\Framework\Exception\NotFoundException;
14+
use Magento\Framework\Message\ManagerInterface;
15+
use Magento\Framework\ObjectManagerInterface;
1316
use Magento\Wishlist\Controller\Index\Update;
1417
use Magento\Wishlist\Controller\WishlistProviderInterface;
18+
use Magento\Wishlist\Helper\Data;
19+
use Magento\Wishlist\Model\Item;
1520
use Magento\Wishlist\Model\LocaleQuantityProcessor;
1621
use PHPUnit\Framework\TestCase;
1722

1823
/**
1924
* Test for upate controller wishlist
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2026
*/
2127
class UpdateTest extends TestCase
2228
{
@@ -60,6 +66,16 @@ class UpdateTest extends TestCase
6066
*/
6167
private $requestMock;
6268

69+
/**
70+
* @var ObjectManagerInterface $objectManagerMock
71+
*/
72+
private $objectManagerMock;
73+
74+
/**
75+
* @var ManagerInterface $messageManager
76+
*/
77+
private $messageManager;
78+
6379
/**
6480
* @inheritdoc
6581
*/
@@ -74,10 +90,14 @@ protected function setUp()
7490
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
7591
->setMethods(['getPostValue'])
7692
->getMockForAbstractClass();
93+
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
7794

7895
$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);
81101

82102
$this->resultFactory->expects($this->any())
83103
->method('create')
@@ -86,6 +106,11 @@ protected function setUp()
86106
->method('getRequest')
87107
->willReturn($this->requestMock);
88108

109+
$this->messageManager = $this->createMock(ManagerInterface::class);
110+
$this->context->expects($this->any())
111+
->method('getMessageManager')
112+
->willReturn($this->messageManager);
113+
89114
$this->updateController = new Update(
90115
$this->context,
91116
$this->formKeyValidator,
@@ -97,23 +122,131 @@ protected function setUp()
97122
/**
98123
* Test for update method Wishlist controller.
99124
*
100-
* Check if there is not post value result redirect returned.
101-
*
125+
* @dataProvider getWishlistDataProvider
102126
* @return void
103127
*/
104-
public function testUpdate(): void
128+
public function testUpdate(array $wishlistDataProvider): void
105129
{
106130
$this->formKeyValidator->expects($this->once())
107-
->method('validate')
108-
->willReturn(true);
131+
->method('validate')
132+
->willReturn(true);
109133

110134
$wishlist = $this->createMock(\Magento\Wishlist\Model\Wishlist::class);
135+
111136
$this->wishlistProvider->expects($this->once())
112137
->method('getWishlist')
113138
->willReturn($wishlist);
139+
$wishlist->expects($this->exactly(2))
140+
->method('getId')
141+
->willReturn($wishlistDataProvider['wishlist_data']['id']);
114142
$this->requestMock->expects($this->once())
115143
->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');
117210
$this->assertEquals($this->resultRedirect, $this->updateController->execute());
118211
}
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+
}
119252
}

0 commit comments

Comments
 (0)