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