Passed
Push — master ( 13c40c...678844 )
by Christian
22:29 queued 10:00
created

createIndividualCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Test\Cart\Promotion\Helpers\Traits;
4
5
use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountEntity;
6
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
7
use Shopware\Core\Defaults;
8
use Shopware\Core\Framework\Context;
9
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
10
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
11
use Shopware\Core\Framework\Test\TestCaseBase\TaxAddToSalesChannelTestBehaviour;
12
use Shopware\Core\Framework\Uuid\Uuid;
13
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
14
use Shopware\Core\System\SalesChannel\SalesChannelContext;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
trait PromotionTestFixtureBehaviour
18
{
19
    use TaxAddToSalesChannelTestBehaviour;
20
21
    /**
22
     * @param $value
23
     */
24
    public function createSetGroupFixture(string $packagerKey, $value, string $sorterKey, string $promotionId, ContainerInterface $container): string
25
    {
26
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
27
28
        $repository = $container->get('promotion_setgroup.repository');
29
30
        $groupId = Uuid::randomHex();
31
32
        $data = [
33
            'id' => $groupId,
34
            'promotionId' => $promotionId,
35
            'packagerKey' => $packagerKey,
36
            'sorterKey' => $sorterKey,
37
            'value' => $value,
38
        ];
39
40
        $repository->create([$data], $context->getContext());
41
42
        return $groupId;
43
    }
44
45
    /**
46
     * Creates a new product in the database.
47
     */
48
    private function createTestFixtureProduct(string $productId, float $grossPrice, float $taxRate, ContainerInterface $container, SalesChannelContext $context): void
49
    {
50
        /** @var EntityRepositoryInterface $productRepository */
51
        $productRepository = $container->get('product.repository');
52
53
        $tax = ['id' => Uuid::randomHex(), 'taxRate' => $taxRate, 'name' => 'with id'];
54
55
        $productRepository->create(
56
            [
57
                [
58
                    'id' => $productId,
59
                    'productNumber' => $productId,
60
                    'stock' => 1,
61
                    'name' => 'Test',
62
                    'active' => true,
63
                    'price' => [
64
                        [
65
                            'currencyId' => Defaults::CURRENCY,
66
                            'gross' => $grossPrice,
67
                            'net' => 9, 'linked' => false,
68
                        ],
69
                    ],
70
                    'manufacturer' => ['name' => 'test'],
71
                    'tax' => $tax,
72
                    'visibilities' => [
73
                        ['salesChannelId' => $context->getSalesChannel()->getId(), 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL],
74
                    ],
75
                    'categories' => [
76
                        ['id' => Uuid::randomHex(), 'name' => 'Clothing'],
77
                    ],
78
                ],
79
            ],
80
            $context->getContext()
81
        );
82
83
        $this->addTaxDataToSalesChannel($context, $tax);
84
    }
85
86
    /**
87
     * Creates a new absolute promotion in the database.
88
     *
89
     * @return string
90
     */
91
    private function createTestFixtureAbsolutePromotion(string $promotionId, string $code, float $value, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART)
92
    {
93
        /** @var EntityRepositoryInterface $promotionRepository */
94
        $promotionRepository = $container->get('promotion.repository');
95
96
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
97
98
        $this->createPromotion(
99
            $promotionId,
100
            $code,
101
            $promotionRepository,
102
            $context
103
        );
104
105
        return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, $scope, $value, null, $container, $context);
106
    }
107
108
    /**
109
     * Creates a new percentage promotion in the database.
110
     *
111
     * @return string
112
     */
113
    private function createTestFixturePercentagePromotion(string $promotionId, ?string $code, float $percentage, ?float $maxValue, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART)
114
    {
115
        /** @var EntityRepositoryInterface $promotionRepository */
116
        $promotionRepository = $container->get('promotion.repository');
117
118
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
119
120
        $this->createPromotion(
121
            $promotionId,
122
            $code,
123
            $promotionRepository,
124
            $context
125
        );
126
127
        return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_PERCENTAGE, $scope, $percentage, $maxValue, $container, $context);
128
    }
129
130
    /**
131
     * Creates a new percentage promotion in the database.
132
     *
133
     * @return string
134
     */
135
    private function createTestFixtureSetGroupPromotion(string $promotionId, ?string $code, ContainerInterface $container)
136
    {
137
        /** @var EntityRepositoryInterface $promotionRepository */
138
        $promotionRepository = $container->get('promotion.repository');
139
140
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
141
142
        $this->createSetGroupPromotion(
143
            $promotionId,
144
            $code,
145
            $promotionRepository,
146
            $context
147
        );
148
    }
149
150
    private function createSetGroupDiscount(
151
        string $promotionId,
152
        int $groupIndex,
153
        ContainerInterface $container,
154
        float $value,
155
        ?float $maxValue,
156
        string $discountType = PromotionDiscountEntity::TYPE_PERCENTAGE,
157
        string $sortKey = 'PRICE_ASC',
158
        string $applierKey = 'ALL',
159
        string $usageKey = 'ALL',
160
        string $pickerKey = 'VERTICAL'
161
    ) {
162
        $scope = PromotionDiscountEntity::SCOPE_SETGROUP . '-' . $groupIndex;
163
164
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
165
166
        /** @var EntityRepositoryInterface $discountRepository */
167
        $discountRepository = $container->get('promotion_discount.repository');
168
169
        $discountId = Uuid::randomHex();
170
171
        $data = [
172
            'id' => $discountId,
173
            'promotionId' => $promotionId,
174
            'scope' => $scope,
175
            'type' => $discountType,
176
            'value' => $value,
177
            'considerAdvancedRules' => true,
178
            'sorterKey' => $sortKey,
179
            'applierKey' => $applierKey,
180
            'usageKey' => $usageKey,
181
            'pickerKey' => $pickerKey,
182
        ];
183
184
        if ($maxValue !== null) {
185
            $data['maxValue'] = $maxValue;
186
        }
187
188
        $discountRepository->create([$data], $context->getContext());
189
190
        return $discountId;
191
    }
192
193
    /**
194
     * Creates a new advanced currency price for the provided discount
195
     */
196
    private function createTestFixtureAdvancedPrice(string $discountId, string $currency, float $price, ContainerInterface $container): void
197
    {
198
        /** @var EntityRepositoryInterface $pricesRepository */
199
        $pricesRepository = $container->get('promotion_discount_prices.repository');
200
201
        $context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL);
202
203
        $pricesRepository->create(
204
            [
205
                [
206
                    'discountId' => $discountId,
207
                    'currencyId' => $currency,
208
                    'price' => $price,
209
                ],
210
            ],
211
            $context->getContext()
212
        );
213
    }
214
215
    /**
216
     * function creates a discount for a promotion
217
     */
218
    private function createTestFixtureDiscount(
219
        string $promotionId,
220
        string $discountType,
221
        string $scope,
222
        float $value,
223
        ?float $maxValue,
224
        ContainerInterface $container,
225
        SalesChannelContext $context,
226
        bool $considerAdvancedRules = false
227
    ): string {
228
        $discountRepository = $container->get('promotion_discount.repository');
229
230
        $discountId = Uuid::randomHex();
231
232
        $data = [
233
            'id' => $discountId,
234
            'promotionId' => $promotionId,
235
            'scope' => $scope,
236
            'type' => $discountType,
237
            'value' => $value,
238
            'considerAdvancedRules' => $considerAdvancedRules,
239
        ];
240
241
        if ($maxValue !== null) {
242
            $data['maxValue'] = $maxValue;
243
        }
244
245
        $discountRepository->create([$data], $context->getContext());
246
247
        return $discountId;
248
    }
249
250
    /**
251
     * function creates a promotion
252
     */
253
    private function createPromotion(string $promotionId, ?string $code, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): EntityWrittenContainerEvent
254
    {
255
        $data = [
256
            'id' => $promotionId,
257
            'name' => 'Black Friday',
258
            'active' => true,
259
            'useCodes' => false,
260
            'useSetGroups' => false,
261
            'salesChannels' => [
262
                ['salesChannelId' => $context->getSalesChannel()->getId(), 'priority' => 1],
263
            ],
264
        ];
265
266
        if ($code !== null) {
267
            $data['code'] = $code;
268
            $data['useCodes'] = true;
269
        }
270
271
        return $promotionRepository->create([$data], $context->getContext());
272
    }
273
274
    /**
275
     * function creates an individual promotion code
276
     */
277
    private function createIndividualCode(string $promotionId, string $code, EntityRepositoryInterface $promotionIndividualRepository, Context $context): EntityWrittenContainerEvent
278
    {
279
        $data = [
280
            'id' => Uuid::randomHex(),
281
            'promotionId' => $promotionId,
282
            'code' => $code,
283
        ];
284
285
        return $promotionIndividualRepository->create([$data], $context);
286
    }
287
288
    private function createSetGroupPromotion(string $promotionId, ?string $code, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): void
289
    {
290
        $data = [
291
            'id' => $promotionId,
292
            'name' => 'Black Friday',
293
            'active' => true,
294
            'useCodes' => false,
295
            'useSetGroups' => true,
296
            'salesChannels' => [
297
                ['salesChannelId' => $context->getSalesChannel()->getId(), 'priority' => 1],
298
            ],
299
        ];
300
301
        if ($code !== null) {
302
            $data['code'] = $code;
303
            $data['useCodes'] = true;
304
        }
305
306
        $promotionRepository->create([$data], $context->getContext());
307
    }
308
309
    /**
310
     * Creates a new promotion with a discount that has a scope DELIVERY
311
     */
312
    private function createTestFixtureDeliveryPromotion(
313
        string $promotionId,
314
        string $discountType,
315
        float $value,
316
        ContainerInterface $container,
317
        SalesChannelContext $context,
318
        ?string $code
319
    ): string {
320
        /** @var EntityRepositoryInterface $promotionRepository */
321
        $promotionRepository = $container->get('promotion.repository');
322
323
        $this->createPromotion(
324
            $promotionId,
325
            $code,
326
            $promotionRepository,
327
            $context
328
        );
329
330
        $deliveryId = $this->createTestFixtureDiscount(
331
            $promotionId,
332
            $discountType,
333
            PromotionDiscountEntity::SCOPE_DELIVERY,
334
            $value,
335
            null,
336
            $container,
337
            $context
338
        );
339
340
        return $deliveryId;
341
    }
342
343
    /**
344
     * function creates a promotion and a discount for it.
345
     * function returns the id of the new discount
346
     */
347
    private function createTestFixtureFixedUnitDiscountPromotion(
348
        string $promotionId,
349
        float $fixedPrice,
350
        string $scope,
351
        ?string $code,
352
        ContainerInterface $container,
353
        SalesChannelContext $context,
354
        bool $considerAdvancedRules = false
355
    ): string {
356
        /** @var EntityRepositoryInterface $promotionRepository */
357
        $promotionRepository = $container->get('promotion.repository');
358
359
        $this->createPromotion(
360
            $promotionId,
361
            $code,
362
            $promotionRepository,
363
            $context
364
        );
365
366
        $discountId = $this->createTestFixtureDiscount(
367
            $promotionId,
368
            PromotionDiscountEntity::TYPE_FIXED_UNIT,
369
            $scope,
370
            $fixedPrice,
371
            null,
372
            $this->getContainer(),
0 ignored issues
show
Bug introduced by
It seems like getContainer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

372
            $this->/** @scrutinizer ignore-call */ 
373
                   getContainer(),
Loading history...
373
            $context,
374
            $considerAdvancedRules
375
        );
376
377
        return $discountId;
378
    }
379
380
    /**
381
     * function creates a promotion and a discount for it.
382
     * function returns the id of the new discount
383
     */
384
    private function createTestFixtureFixedDiscountPromotion(string $promotionId, float $fixedPrice, string $scope, ?string $code, ContainerInterface $container, SalesChannelContext $context): string
385
    {
386
        /** @var EntityRepositoryInterface $promotionRepository */
387
        $promotionRepository = $container->get('promotion.repository');
388
389
        $this->createPromotion(
390
            $promotionId,
391
            $code,
392
            $promotionRepository,
393
            $context
394
        );
395
396
        $discountId = $this->createTestFixtureDiscount(
397
            $promotionId,
398
            PromotionDiscountEntity::TYPE_FIXED,
399
            $scope,
400
            $fixedPrice,
401
            null,
402
            $this->getContainer(),
403
            $context,
404
            false
405
        );
406
407
        return $discountId;
408
    }
409
}
410