Passed
Push — trunk ( 26fc38...0b2f4f )
by Christian
13:44 queued 13s
created

OrderBuilder::addAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Test\Integration\Builder\Order;
4
5
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
6
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
7
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
8
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
9
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
10
use Shopware\Core\Defaults;
11
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\CashRoundingConfig;
12
use Shopware\Core\Framework\Log\Package;
13
use Shopware\Core\Framework\Test\IdsCollection;
14
use Shopware\Core\Framework\Test\TestCaseBase\BasicTestDataBehaviour;
15
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
16
use Shopware\Core\Test\TestBuilderTrait;
17
use Shopware\Core\Test\TestDefaults;
18
19
/**
20
 * @final
21
 */
22
#[Package('customer-order')]
23
class OrderBuilder
24
{
25
    use BasicTestDataBehaviour;
26
    use KernelTestBehaviour;
27
    use TestBuilderTrait;
28
29
    protected string $id;
30
31
    protected string $orderNumber;
32
33
    protected string $currencyId;
34
35
    protected float $currencyFactor;
36
37
    protected string $billingAddressId;
38
39
    protected string $orderDateTime;
40
41
    protected CartPrice $price;
42
43
    protected CalculatedPrice $shippingCosts;
44
45
    /**
46
     * @var array<mixed>
47
     */
48
    protected array $lineItems = [];
49
50
    /**
51
     * @var array<mixed>
52
     */
53
    protected array $transactions = [];
54
55
    /**
56
     * @var array<mixed>
57
     */
58
    protected array $addresses = [];
59
60
    protected string $stateId;
61
62
    public function __construct(
63
        IdsCollection $ids,
64
        string $orderNumber,
65
        protected string $salesChannelId = TestDefaults::SALES_CHANNEL
66
    ) {
67
        $this->ids = $ids;
68
        $this->id = $ids->get($orderNumber);
69
        $this->billingAddressId = $ids->get('billing_address');
70
        $this->currencyId = Defaults::CURRENCY;
71
        $this->stateId = $this->getStateMachineState();
72
        $this->orderNumber = $orderNumber;
73
        $this->orderDateTime = (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT);
74
        $this->currencyFactor = 1.0;
75
76
        $this->price(420.69);
77
        $this->shippingCosts(0);
78
        $this->add('itemRounding', json_decode(json_encode(new CashRoundingConfig(2, 0.01, true), \JSON_THROW_ON_ERROR), true, 512, \JSON_THROW_ON_ERROR));
79
        $this->add('totalRounding', json_decode(json_encode(new CashRoundingConfig(2, 0.01, true), \JSON_THROW_ON_ERROR), true, 512, \JSON_THROW_ON_ERROR));
80
    }
81
82
    public function price(float $amount): self
83
    {
84
        $this->price = new CartPrice(
85
            $amount,
86
            $amount,
87
            $amount,
88
            new CalculatedTaxCollection(),
89
            new TaxRuleCollection(),
90
            CartPrice::TAX_STATE_FREE
91
        );
92
93
        return $this;
94
    }
95
96
    public function shippingCosts(float $amount): self
97
    {
98
        $this->shippingCosts = new CalculatedPrice(
99
            $amount,
100
            $amount,
101
            new CalculatedTaxCollection(),
102
            new TaxRuleCollection()
103
        );
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param array<mixed> $customParams
110
     */
111
    public function addTransaction(string $key, array $customParams = []): self
112
    {
113
        if (\array_key_exists('amount', $customParams)) {
114
            if (\is_float($customParams['amount'])) {
115
                $customParams['amount'] = new CalculatedPrice(
116
                    $customParams['amount'],
117
                    $customParams['amount'],
118
                    new CalculatedTaxCollection(),
119
                    new TaxRuleCollection()
120
                );
121
            }
122
        }
123
124
        $transaction = \array_replace([
125
            'id' => $this->ids->get($key),
126
            'orderId' => $this->id,
127
            'paymentMethodId' => $this->getValidPaymentMethodId(),
128
            'amount' => new CalculatedPrice(
129
                420.69,
130
                420.69,
131
                new CalculatedTaxCollection(),
132
                new TaxRuleCollection()
133
            ),
134
            'stateId' => $this->getStateMachineState(
135
                OrderTransactionStates::STATE_MACHINE,
136
                OrderTransactionStates::STATE_OPEN
137
            ),
138
        ], $customParams);
139
140
        $this->transactions[$this->ids->get($key)] = $transaction;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param array<mixed> $customParams
147
     */
148
    public function addAddress(string $key, array $customParams = []): self
149
    {
150
        $address = \array_replace([
151
            'firstName' => 'Max',
152
            'lastName' => 'Mustermann',
153
            'city' => 'Bielefeld',
154
            'street' => 'Buchenweg 5',
155
            'zipcode' => '33062',
156
            'country' => [
157
                'id' => $this->ids->get($key),
158
                'name' => 'Germany',
159
            ],
160
        ], $customParams);
161
162
        $this->addresses[$key] = $address;
163
164
        return $this;
165
    }
166
}
167