1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TddWizard\Fixtures\Sales; |
5
|
|
|
|
6
|
|
|
use Magento\Sales\Api\Data\OrderInterface; |
7
|
|
|
use TddWizard\Fixtures\Catalog\ProductBuilder; |
8
|
|
|
use TddWizard\Fixtures\Checkout\CartBuilder; |
9
|
|
|
use TddWizard\Fixtures\Checkout\CustomerCheckout; |
10
|
|
|
use TddWizard\Fixtures\Customer\AddressBuilder; |
11
|
|
|
use TddWizard\Fixtures\Customer\CustomerBuilder; |
12
|
|
|
use TddWizard\Fixtures\Customer\CustomerFixture; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Builder to be used by fixtures |
16
|
|
|
*/ |
17
|
|
|
class OrderBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var CartBuilder |
21
|
|
|
*/ |
22
|
|
|
private $cartBuilder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ProductBuilder[] |
26
|
|
|
*/ |
27
|
|
|
private $productBuilders; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CustomerBuilder |
31
|
|
|
*/ |
32
|
|
|
private $customerBuilder; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $shippingMethod; |
38
|
|
|
|
39
|
14 |
|
final public function __construct() |
40
|
|
|
{ |
41
|
14 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $paymentMethod; |
47
|
|
|
|
48
|
14 |
|
public static function anOrder(): OrderBuilder |
49
|
|
|
{ |
50
|
14 |
|
return new static(); |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
public function withProducts(ProductBuilder ...$productBuilders): OrderBuilder |
54
|
|
|
{ |
55
|
7 |
|
$builder = clone $this; |
56
|
7 |
|
$builder->productBuilders = $productBuilders; |
57
|
|
|
|
58
|
7 |
|
return $builder; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
public function withCustomer(CustomerBuilder $customerBuilder): OrderBuilder |
62
|
|
|
{ |
63
|
4 |
|
$builder = clone $this; |
64
|
4 |
|
$builder->customerBuilder = $customerBuilder; |
65
|
|
|
|
66
|
4 |
|
return $builder; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
public function withCart(CartBuilder $cartBuilder): OrderBuilder |
70
|
|
|
{ |
71
|
5 |
|
$builder = clone $this; |
72
|
5 |
|
$builder->cartBuilder = $cartBuilder; |
73
|
|
|
|
74
|
5 |
|
return $builder; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
public function withShippingMethod(string $shippingMethod): OrderBuilder |
78
|
|
|
{ |
79
|
2 |
|
$builder = clone $this; |
80
|
2 |
|
$builder->shippingMethod = $shippingMethod; |
81
|
|
|
|
82
|
2 |
|
return $builder; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
public function withPaymentMethod(string $paymentMethod): OrderBuilder |
86
|
|
|
{ |
87
|
3 |
|
$builder = clone $this; |
88
|
3 |
|
$builder->paymentMethod = $paymentMethod; |
89
|
|
|
|
90
|
3 |
|
return $builder; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return OrderInterface |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
14 |
|
public function build(): OrderInterface |
98
|
|
|
{ |
99
|
14 |
|
$builder = clone $this; |
100
|
|
|
|
101
|
14 |
|
if (empty($builder->productBuilders)) { |
102
|
|
|
// init simple products |
103
|
8 |
|
for ($i = 0; $i < 3; $i++) { |
104
|
8 |
|
$builder->productBuilders[] = ProductBuilder::aSimpleProduct(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// create products |
109
|
14 |
|
$products = array_map( |
110
|
|
|
static function (ProductBuilder $productBuilder) { |
111
|
14 |
|
return $productBuilder->build(); |
112
|
14 |
|
}, |
113
|
14 |
|
$builder->productBuilders |
114
|
|
|
); |
115
|
|
|
|
116
|
14 |
|
if (empty($builder->customerBuilder)) { |
117
|
|
|
// init customer |
118
|
11 |
|
$builder->customerBuilder = CustomerBuilder::aCustomer() |
119
|
11 |
|
->withAddresses(AddressBuilder::anAddress()->asDefaultBilling()->asDefaultShipping()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// log customer in |
123
|
14 |
|
$customer = $builder->customerBuilder->build(); |
124
|
14 |
|
$customerFixture = new CustomerFixture($customer); |
125
|
14 |
|
$customerFixture->login(); |
126
|
|
|
|
127
|
14 |
|
if (empty($builder->cartBuilder)) { |
128
|
|
|
// init cart, add products |
129
|
10 |
|
$builder->cartBuilder = CartBuilder::forCurrentSession(); |
130
|
10 |
|
foreach ($products as $product) { |
131
|
10 |
|
$qty = 1; |
132
|
10 |
|
$builder->cartBuilder = $builder->cartBuilder->withSimpleProduct($product->getSku(), $qty); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// check out, place order |
137
|
14 |
|
$checkout = CustomerCheckout::fromCart($builder->cartBuilder->build()); |
138
|
14 |
|
if ($builder->shippingMethod) { |
139
|
2 |
|
$checkout = $checkout->withShippingMethodCode($builder->shippingMethod); |
140
|
|
|
} |
141
|
|
|
|
142
|
14 |
|
if ($builder->paymentMethod) { |
143
|
3 |
|
$checkout = $checkout->withPaymentMethodCode($builder->paymentMethod); |
144
|
|
|
} |
145
|
|
|
|
146
|
14 |
|
$order = $checkout->placeOrder(); |
147
|
|
|
|
148
|
14 |
|
$customerFixture->logout(); |
149
|
|
|
|
150
|
14 |
|
return $order; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|