7
7
8
8
namespace Magento \Checkout \Test \Unit \Controller \Cart ;
9
9
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Model \Product ;
10
12
use Magento \Checkout \Controller \Cart \Add ;
13
+ use Magento \Checkout \Model \AddProductToCart ;
14
+ use Magento \Checkout \Model \Cart ;
15
+ use Magento \Checkout \Model \Cart \RequestQuantityProcessor ;
16
+ use Magento \Framework \App \Request \Http ;
11
17
use Magento \Framework \App \RequestInterface ;
12
18
use Magento \Framework \Controller \Result \Redirect ;
13
19
use Magento \Framework \Controller \Result \RedirectFactory ;
14
20
use Magento \Framework \Data \Form \FormKey \Validator ;
21
+ use Magento \Framework \Json \Helper \Data as JsonSerializer ;
22
+ use Magento \Framework \Locale \ResolverInterface ;
15
23
use Magento \Framework \Message \ManagerInterface ;
24
+ use Magento \Framework \ObjectManagerInterface ;
16
25
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
26
+ use Magento \Quote \Model \Quote ;
27
+ use Magento \Store \Model \Store ;
28
+ use Magento \Store \Model \StoreManagerInterface ;
17
29
use PHPUnit \Framework \MockObject \MockObject ;
18
30
use PHPUnit \Framework \TestCase ;
19
31
32
+ /**
33
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
34
+ */
20
35
class AddTest extends TestCase
21
36
{
22
37
/**
@@ -44,6 +59,36 @@ class AddTest extends TestCase
44
59
*/
45
60
private $ messageManager ;
46
61
62
+ /**
63
+ * @var ProductRepositoryInterface&MockObject
64
+ */
65
+ private $ productRepository ;
66
+
67
+ /**
68
+ * @var ObjectManagerInterface&MockObject
69
+ */
70
+ private $ objectManagerMock ;
71
+
72
+ /**
73
+ * @var RequestQuantityProcessor&MockObject
74
+ */
75
+ private $ quantityProcessor ;
76
+
77
+ /**
78
+ * @var AddProductToCart&MockObject
79
+ */
80
+ private $ addProductToCart ;
81
+
82
+ /**
83
+ * @var Cart&MockObject
84
+ */
85
+ private $ cart ;
86
+
87
+ /**
88
+ * @var \Magento\Framework\App\Response\Http&MockObject
89
+ */
90
+ private $ response ;
91
+
47
92
/**
48
93
* @var Add|MockObject
49
94
*/
@@ -63,21 +108,34 @@ protected function setUp(): void
63
108
$ this ->getMockBuilder (RedirectFactory::class)
64
109
->disableOriginalConstructor ()
65
110
->getMock ();
66
- $ this ->request = $ this ->getMockBuilder (RequestInterface ::class)
111
+ $ this ->request = $ this ->getMockBuilder (Http ::class)
67
112
->disableOriginalConstructor ()
68
113
->getmock ();
69
114
$ this ->messageManager = $ this ->getMockBuilder (ManagerInterface::class)
70
115
->disableOriginalConstructor ()
71
116
->getMockForAbstractClass ();
72
117
118
+ $ this ->productRepository = $ this ->createMock (ProductRepositoryInterface::class);
119
+ $ this ->objectManagerMock = $ this ->createMock (ObjectManagerInterface::class);
120
+ $ this ->quantityProcessor = $ this ->createMock (RequestQuantityProcessor::class);
121
+ $ this ->addProductToCart = $ this ->createMock (AddProductToCart::class);
122
+ $ this ->cart = $ this ->createMock (Cart::class);
123
+ $ this ->response = $ this ->createMock (\Magento \Framework \App \Response \Http::class);
124
+
73
125
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
74
126
$ this ->cartAdd = $ this ->objectManagerHelper ->getObject (
75
127
Add::class,
76
128
[
77
129
'_formKeyValidator ' => $ this ->formKeyValidator ,
78
130
'resultRedirectFactory ' => $ this ->resultRedirectFactory ,
79
131
'_request ' => $ this ->request ,
80
- 'messageManager ' => $ this ->messageManager
132
+ 'messageManager ' => $ this ->messageManager ,
133
+ 'productRepository ' => $ this ->productRepository ,
134
+ '_objectManager ' => $ this ->objectManagerMock ,
135
+ 'quantityProcessor ' => $ this ->quantityProcessor ,
136
+ 'addProductToCart ' => $ this ->addProductToCart ,
137
+ 'cart ' => $ this ->cart ,
138
+ '_response ' => $ this ->response
81
139
]
82
140
);
83
141
}
@@ -87,7 +145,7 @@ protected function setUp(): void
87
145
*
88
146
* @return void
89
147
*/
90
- public function testExecute ()
148
+ public function testExecuteWhenFormKeyValidatorFails (): void
91
149
{
92
150
$ redirect = $ this ->getMockBuilder (Redirect::class)
93
151
->disableOriginalConstructor ()
@@ -100,4 +158,62 @@ public function testExecute()
100
158
$ redirect ->expects ($ this ->once ())->method ('setPath ' )->with ($ path )->willReturnSelf ();
101
159
$ this ->assertEquals ($ redirect , $ this ->cartAdd ->execute ());
102
160
}
161
+
162
+ public function testExecuteWithValidData (): void
163
+ {
164
+ $ productId = 1 ;
165
+ $ storeId = 1 ;
166
+ $ params = ['qty ' => 1 ];
167
+ $ product = $ this ->createMock (Product::class);
168
+ $ quote = $ this ->createMock (Quote::class);
169
+ $ storeManager = $ this ->createMock (StoreManagerInterface::class);
170
+ $ store = $ this ->createMock (Store::class);
171
+ $ localeResolver = $ this ->createMock (ResolverInterface::class);
172
+ $ storeManager ->expects ($ this ->once ())
173
+ ->method ('getStore ' )
174
+ ->willReturn ($ store );
175
+ $ store ->expects ($ this ->once ())
176
+ ->method ('getId ' )
177
+ ->willReturn ($ storeId );
178
+ $ this ->request ->method ('getParam ' )
179
+ ->willReturnMap ([
180
+ ['product ' , null , $ productId ],
181
+ ['related_product ' , null , '2,3 ' ],
182
+ ['return_url ' , null , '/sku.html ' ]
183
+ ]);
184
+ $ this ->request ->expects ($ this ->once ())
185
+ ->method ('getParams ' )
186
+ ->willReturn ($ params );
187
+ $ this ->request ->expects ($ this ->once ())
188
+ ->method ('isAjax ' )
189
+ ->willReturn (true );
190
+ $ this ->productRepository ->expects ($ this ->once ())
191
+ ->method ('getById ' )
192
+ ->with ($ productId , false , $ storeId )
193
+ ->willReturn ($ product );
194
+ $ this ->objectManagerMock ->method ('get ' )
195
+ ->with ()
196
+ ->willReturnMap ([
197
+ [StoreManagerInterface::class, $ storeManager ],
198
+ [ResolverInterface::class, $ localeResolver ],
199
+ [JsonSerializer::class, $ this ->createMock (JsonSerializer::class)],
200
+ ]);
201
+ $ this ->addProductToCart ->expects ($ this ->once ())
202
+ ->method ('execute ' )
203
+ ->with ($ this ->cart , $ product , $ params , [2 , 3 ])
204
+ ->willReturn (true );
205
+ $ this ->quantityProcessor ->expects ($ this ->once ())
206
+ ->method ('prepareQuantity ' )
207
+ ->with ($ params ['qty ' ])
208
+ ->willReturn ($ params ['qty ' ]);
209
+ $ this ->cart ->expects ($ this ->once ())
210
+ ->method ('getQuote ' )
211
+ ->willReturn ($ quote );
212
+ $ this ->formKeyValidator ->expects ($ this ->once ())
213
+ ->method ('validate ' )
214
+ ->with ($ this ->request )
215
+ ->willReturn (true );
216
+
217
+ $ this ->assertEquals ($ this ->response , $ this ->cartAdd ->execute ());
218
+ }
103
219
}
0 commit comments