77
88namespace Magento \Checkout \Test \Unit \Controller \Cart ;
99
10+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11+ use Magento \Catalog \Model \Product ;
1012use 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 ;
1117use Magento \Framework \App \RequestInterface ;
1218use Magento \Framework \Controller \Result \Redirect ;
1319use Magento \Framework \Controller \Result \RedirectFactory ;
1420use Magento \Framework \Data \Form \FormKey \Validator ;
21+ use Magento \Framework \Json \Helper \Data as JsonSerializer ;
22+ use Magento \Framework \Locale \ResolverInterface ;
1523use Magento \Framework \Message \ManagerInterface ;
24+ use Magento \Framework \ObjectManagerInterface ;
1625use 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 ;
1729use PHPUnit \Framework \MockObject \MockObject ;
1830use PHPUnit \Framework \TestCase ;
1931
32+ /**
33+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
34+ */
2035class AddTest extends TestCase
2136{
2237 /**
@@ -44,6 +59,36 @@ class AddTest extends TestCase
4459 */
4560 private $ messageManager ;
4661
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+
4792 /**
4893 * @var Add|MockObject
4994 */
@@ -63,21 +108,34 @@ protected function setUp(): void
63108 $ this ->getMockBuilder (RedirectFactory::class)
64109 ->disableOriginalConstructor ()
65110 ->getMock ();
66- $ this ->request = $ this ->getMockBuilder (RequestInterface ::class)
111+ $ this ->request = $ this ->getMockBuilder (Http ::class)
67112 ->disableOriginalConstructor ()
68113 ->getmock ();
69114 $ this ->messageManager = $ this ->getMockBuilder (ManagerInterface::class)
70115 ->disableOriginalConstructor ()
71116 ->getMockForAbstractClass ();
72117
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+
73125 $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
74126 $ this ->cartAdd = $ this ->objectManagerHelper ->getObject (
75127 Add::class,
76128 [
77129 '_formKeyValidator ' => $ this ->formKeyValidator ,
78130 'resultRedirectFactory ' => $ this ->resultRedirectFactory ,
79131 '_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
81139 ]
82140 );
83141 }
@@ -87,7 +145,7 @@ protected function setUp(): void
87145 *
88146 * @return void
89147 */
90- public function testExecute ()
148+ public function testExecuteWhenFormKeyValidatorFails (): void
91149 {
92150 $ redirect = $ this ->getMockBuilder (Redirect::class)
93151 ->disableOriginalConstructor ()
@@ -100,4 +158,62 @@ public function testExecute()
100158 $ redirect ->expects ($ this ->once ())->method ('setPath ' )->with ($ path )->willReturnSelf ();
101159 $ this ->assertEquals ($ redirect , $ this ->cartAdd ->execute ());
102160 }
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+ }
103219}
0 commit comments