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 \Cart as CustomerCart ;
14
+ use Magento \Checkout \Model \Session ;
15
+ use Magento \Framework \App \Action \Context ;
11
16
use Magento \Framework \App \RequestInterface ;
17
+ use Magento \Framework \App \ResponseInterface ;
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 \Exception \LocalizedException ;
22
+ use Magento \Framework \Json \Helper \Data ;
23
+ use Magento \Framework \Message \Collection ;
15
24
use Magento \Framework \Message \ManagerInterface ;
25
+ use Magento \Framework \Message \MessageInterface ;
26
+ use Magento \Framework \ObjectManagerInterface ;
27
+ use Magento \Framework \Phrase ;
16
28
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
29
+ use Magento \Store \Api \Data \StoreInterface ;
30
+ use Magento \Store \Model \StoreManagerInterface ;
17
31
use PHPUnit \Framework \MockObject \MockObject ;
18
32
use PHPUnit \Framework \TestCase ;
19
33
34
+ /**
35
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
+ */
20
37
class AddTest extends TestCase
21
38
{
22
39
/**
@@ -49,6 +66,31 @@ class AddTest extends TestCase
49
66
*/
50
67
private $ cartAdd ;
51
68
69
+ /**
70
+ * @var CustomerCart|MockObject
71
+ */
72
+ private CustomerCart $ cart ;
73
+
74
+ /**
75
+ * @var StoreManagerInterface|StoreManagerInterface&MockObject|MockObject
76
+ */
77
+ private StoreManagerInterface $ storeManager ;
78
+
79
+ /**
80
+ * @var ProductRepositoryInterface
81
+ */
82
+ private ProductRepositoryInterface $ productRepository ;
83
+
84
+ /**
85
+ * @var Context|Context&MockObject|MockObject
86
+ */
87
+ private Context $ context ;
88
+
89
+ /**
90
+ * @var Session|Session&MockObject|MockObject
91
+ */
92
+ private Session $ checkoutSession ;
93
+
52
94
/**
53
95
* Init mocks for tests.
54
96
*
@@ -65,10 +107,19 @@ protected function setUp(): void
65
107
->getMock ();
66
108
$ this ->request = $ this ->getMockBuilder (RequestInterface::class)
67
109
->disableOriginalConstructor ()
68
- ->getmock ();
110
+ ->addMethods (['isAjax ' ])
111
+ ->getMockForAbstractClass ();
69
112
$ this ->messageManager = $ this ->getMockBuilder (ManagerInterface::class)
70
113
->disableOriginalConstructor ()
71
114
->getMockForAbstractClass ();
115
+ $ this ->cart = $ this ->createMock (CustomerCart::class);
116
+ $ this ->storeManager = $ this ->createMock (StoreManagerInterface::class);
117
+ $ this ->productRepository = $ this ->createMock (ProductRepositoryInterface::class);
118
+ $ this ->context = $ this ->createMock (Context::class);
119
+ $ this ->checkoutSession = $ this ->getMockBuilder (Session::class)
120
+ ->disableOriginalConstructor ()
121
+ ->addMethods (['getRedirectUrl ' , 'getUseNotice ' ])
122
+ ->getMock ();
72
123
73
124
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
74
125
$ this ->cartAdd = $ this ->objectManagerHelper ->getObject (
@@ -77,7 +128,12 @@ protected function setUp(): void
77
128
'_formKeyValidator ' => $ this ->formKeyValidator ,
78
129
'resultRedirectFactory ' => $ this ->resultRedirectFactory ,
79
130
'_request ' => $ this ->request ,
80
- 'messageManager ' => $ this ->messageManager
131
+ 'messageManager ' => $ this ->messageManager ,
132
+ 'cart ' => $ this ->cart ,
133
+ 'storeManager ' => $ this ->storeManager ,
134
+ 'productRepository ' => $ this ->productRepository ,
135
+ 'context ' => $ this ->context ,
136
+ 'checkoutSession ' => $ this ->checkoutSession
81
137
]
82
138
);
83
139
}
@@ -100,4 +156,63 @@ public function testExecute()
100
156
$ redirect ->expects ($ this ->once ())->method ('setPath ' )->with ($ path )->willReturnSelf ();
101
157
$ this ->assertEquals ($ redirect , $ this ->cartAdd ->execute ());
102
158
}
159
+
160
+ /**
161
+ * @return void
162
+ */
163
+ public function testExecuteWithError (): void
164
+ {
165
+ $ helper = $ this ->createMock (Data::class);
166
+ $ escaper = $ this ->createMock (\Magento \Framework \Escaper::class);
167
+ $ escaper ->expects ($ this ->once ())->method ('escapeHtml ' );
168
+ $ objectManager = $ this ->createMock (ObjectManagerInterface::class);
169
+ $ objectManager ->expects ($ this ->any ())
170
+ ->method ('get ' )
171
+ ->willReturnOnConsecutiveCalls ($ escaper , $ helper );
172
+ $ this ->context ->expects ($ this ->any ())->method ('getObjectManager ' )->willReturn ($ objectManager );
173
+ $ response = $ this ->getMockBuilder (ResponseInterface::class)
174
+ ->disableOriginalConstructor ()
175
+ ->addMethods (['representJson ' ])
176
+ ->getMockForAbstractClass ();
177
+ $ response ->expects ($ this ->once ())->method ('representJson ' );
178
+ $ this ->context ->expects ($ this ->any ())->method ('getResponse ' )->willReturn ($ response );
179
+ $ this ->formKeyValidator ->expects ($ this ->once ())->method ('validate ' )->with ($ this ->request )->willReturn (true );
180
+ $ this ->request ->expects ($ this ->exactly (2 ))
181
+ ->method ('getParam ' )
182
+ ->willReturnOnConsecutiveCalls (1 , [], []);
183
+ $ this ->request ->expects ($ this ->once ())
184
+ ->method ('isAjax ' )
185
+ ->willReturn (true );
186
+ $ store = $ this ->createMock (StoreInterface::class);
187
+ $ store ->expects ($ this ->once ())->method ('getId ' );
188
+ $ this ->storeManager ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ store );
189
+ $ product = $ this ->createMock (Product::class);
190
+ $ this ->productRepository ->expects ($ this ->once ())->method ('getById ' )->willReturn ($ product );
191
+ $ this ->cart ->expects ($ this ->once ())
192
+ ->method ('save ' )
193
+ ->willThrowException (new LocalizedException (new Phrase ('error ' )));
194
+ $ this ->messageManager ->expects ($ this ->once ())->method ('addErrorMessage ' );
195
+ $ message = $ this ->createMock (MessageInterface::class);
196
+ $ error = $ this ->createMock (Collection::class);
197
+ $ error ->expects ($ this ->exactly (2 ))->method ('getErrors ' )->willReturn ([$ message ]);
198
+ $ this ->messageManager ->expects ($ this ->exactly (2 ))->method ('getMessages ' )->willReturn ($ error );
199
+ $ this ->checkoutSession ->expects ($ this ->once ())->method ('getRedirectUrl ' )->willReturn ('redirect ' );
200
+
201
+ $ cartAdd = $ this ->objectManagerHelper ->getObject (
202
+ Add::class,
203
+ [
204
+ '_formKeyValidator ' => $ this ->formKeyValidator ,
205
+ 'resultRedirectFactory ' => $ this ->resultRedirectFactory ,
206
+ '_request ' => $ this ->request ,
207
+ 'messageManager ' => $ this ->messageManager ,
208
+ 'cart ' => $ this ->cart ,
209
+ 'storeManager ' => $ this ->storeManager ,
210
+ 'productRepository ' => $ this ->productRepository ,
211
+ 'context ' => $ this ->context ,
212
+ 'checkoutSession ' => $ this ->checkoutSession
213
+ ]
214
+ );
215
+
216
+ $ cartAdd ->execute ();
217
+ }
103
218
}
0 commit comments