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 ;
12
10
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 ;
16
11
use Magento \Framework \App \RequestInterface ;
17
- use Magento \Framework \App \ResponseInterface ;
18
12
use Magento \Framework \Controller \Result \Redirect ;
19
13
use Magento \Framework \Controller \Result \RedirectFactory ;
20
14
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 ;
24
15
use Magento \Framework \Message \ManagerInterface ;
25
- use Magento \Framework \Message \MessageInterface ;
26
- use Magento \Framework \ObjectManagerInterface ;
27
- use Magento \Framework \Phrase ;
28
16
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
29
- use Magento \Store \Api \Data \StoreInterface ;
30
- use Magento \Store \Model \StoreManagerInterface ;
31
17
use PHPUnit \Framework \MockObject \MockObject ;
32
18
use PHPUnit \Framework \TestCase ;
33
19
34
- /**
35
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
- */
37
20
class AddTest extends TestCase
38
21
{
39
22
/**
@@ -66,31 +49,6 @@ class AddTest extends TestCase
66
49
*/
67
50
private $ cartAdd ;
68
51
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
-
94
52
/**
95
53
* Init mocks for tests.
96
54
*
@@ -107,19 +65,10 @@ protected function setUp(): void
107
65
->getMock ();
108
66
$ this ->request = $ this ->getMockBuilder (RequestInterface::class)
109
67
->disableOriginalConstructor ()
110
- ->addMethods (['isAjax ' ])
111
- ->getMockForAbstractClass ();
68
+ ->getmock ();
112
69
$ this ->messageManager = $ this ->getMockBuilder (ManagerInterface::class)
113
70
->disableOriginalConstructor ()
114
71
->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 ();
123
72
124
73
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
125
74
$ this ->cartAdd = $ this ->objectManagerHelper ->getObject (
@@ -128,12 +77,7 @@ protected function setUp(): void
128
77
'_formKeyValidator ' => $ this ->formKeyValidator ,
129
78
'resultRedirectFactory ' => $ this ->resultRedirectFactory ,
130
79
'_request ' => $ this ->request ,
131
- 'messageManager ' => $ this ->messageManager ,
132
- 'cart ' => $ this ->cart ,
133
- 'storeManager ' => $ this ->storeManager ,
134
- 'productRepository ' => $ this ->productRepository ,
135
- 'context ' => $ this ->context ,
136
- 'checkoutSession ' => $ this ->checkoutSession
80
+ 'messageManager ' => $ this ->messageManager
137
81
]
138
82
);
139
83
}
@@ -156,63 +100,4 @@ public function testExecute()
156
100
$ redirect ->expects ($ this ->once ())->method ('setPath ' )->with ($ path )->willReturnSelf ();
157
101
$ this ->assertEquals ($ redirect , $ this ->cartAdd ->execute ());
158
102
}
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
- }
218
103
}
0 commit comments