1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2015 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
+
6
7
declare (strict_types=1 );
7
8
8
9
namespace Magento \SalesRule \Test \Unit \Model \ResourceModel ;
@@ -86,6 +87,11 @@ class RuleTest extends TestCase
86
87
*/
87
88
private $ metadataPoolMock ;
88
89
90
+ /**
91
+ * @var \Magento\SalesRule\Model\ResourceModel\Coupon|MockObject
92
+ */
93
+ private $ resourceCoupon ;
94
+
89
95
protected function setUp (): void
90
96
{
91
97
$ this ->objectManager = new ObjectManager ($ this );
@@ -169,6 +175,8 @@ protected function setUp(): void
169
175
->disableOriginalConstructor ()
170
176
->getMock ();
171
177
178
+ $ this ->resourceCoupon = $ this ->createMock (\Magento \SalesRule \Model \ResourceModel \Coupon::class);
179
+
172
180
$ this ->model = $ this ->objectManager ->getObject (
173
181
Rule::class,
174
182
[
@@ -177,7 +185,8 @@ protected function setUp(): void
177
185
'entityManager ' => $ this ->entityManager ,
178
186
'associatedEntityMapInstance ' => $ associatedEntitiesMap ,
179
187
'serializer ' => $ serializerMock ,
180
- 'metadataPool ' => $ this ->metadataPoolMock
188
+ 'metadataPool ' => $ this ->metadataPoolMock ,
189
+ 'resourceCoupon ' => $ this ->resourceCoupon
181
190
]
182
191
);
183
192
}
@@ -245,6 +254,56 @@ public function testSaveStoreLabels()
245
254
$ this ->model ->saveStoreLabels (1 , ['test ' ]);
246
255
}
247
256
257
+ /**
258
+ * @dataProvider afterSaveShouldUpdateExistingCouponsDataProvider
259
+ * @param array $data
260
+ * @param bool $update
261
+ * @return void
262
+ * @throws \PHPUnit\Framework\MockObject\Exception
263
+ */
264
+ public function testAfterSaveShouldUpdateExistingCoupons (array $ data , bool $ update = true ): void
265
+ {
266
+ /** @var AbstractModel|MockObject $abstractModel */
267
+ $ ruleMock = $ this ->getMockBuilder (\Magento \SalesRule \Model \Rule::class)
268
+ ->disableOriginalConstructor ()
269
+ ->onlyMethods (['getConditions ' , 'getActions ' ])
270
+ ->getMock ();
271
+ $ conditions = $ this ->createMock (\Magento \Rule \Model \Condition \Combine::class);
272
+ $ actions = $ this ->createMock (\Magento \Rule \Model \Action \Collection::class);
273
+ $ ruleMock ->method ('getConditions ' )->willReturn ($ conditions );
274
+ $ ruleMock ->method ('getActions ' )->willReturn ($ actions );
275
+ $ ruleMock ->addData ($ data );
276
+ $ this ->resourceCoupon ->expects ($ update ? $ this ->once () : $ this ->never ())
277
+ ->method ('updateSpecificCoupons ' )
278
+ ->with ($ ruleMock );
279
+ $ this ->model ->afterSave ($ ruleMock );
280
+ }
281
+
282
+ /**
283
+ * @return array
284
+ */
285
+ public static function afterSaveShouldUpdateExistingCouponsDataProvider (): array
286
+ {
287
+ return [
288
+ [
289
+ ['use_auto_generation ' => 0 , 'coupon_type ' => \Magento \SalesRule \Model \Rule::COUPON_TYPE_NO_COUPON ],
290
+ false
291
+ ],
292
+ [
293
+ ['use_auto_generation ' => 0 , 'coupon_type ' => \Magento \SalesRule \Model \Rule::COUPON_TYPE_SPECIFIC ],
294
+ false
295
+ ],
296
+ [
297
+ ['use_auto_generation ' => 1 , 'coupon_type ' => \Magento \SalesRule \Model \Rule::COUPON_TYPE_SPECIFIC ],
298
+ true
299
+ ],
300
+ [
301
+ ['use_auto_generation ' => 0 , 'coupon_type ' => \Magento \SalesRule \Model \Rule::COUPON_TYPE_AUTO ],
302
+ true
303
+ ]
304
+ ];
305
+ }
306
+
248
307
/**
249
308
* @return array
250
309
*/
0 commit comments