1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TddWizard\Fixtures\Sales; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\ObjectManagerInterface; |
6
|
|
|
use Magento\Sales\Api\Data\OrderInterface; |
7
|
|
|
use Magento\Sales\Api\Data\ShipmentInterface; |
8
|
|
|
use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory; |
|
|
|
|
9
|
|
|
use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory; |
|
|
|
|
10
|
|
|
use Magento\Sales\Api\ShipmentRepositoryInterface; |
11
|
|
|
use Magento\Sales\Api\ShipOrderInterface; |
12
|
|
|
use Magento\TestFramework\Helper\Bootstrap; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Builder to be used by fixtures |
16
|
|
|
*/ |
17
|
|
|
class ShipmentBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ShipmentItemCreationInterfaceFactory |
21
|
|
|
*/ |
22
|
|
|
private $itemFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ShipmentTrackCreationInterfaceFactory |
26
|
|
|
*/ |
27
|
|
|
private $trackFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ShipOrderInterface |
31
|
|
|
*/ |
32
|
|
|
private $shipOrder; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ShipmentRepositoryInterface |
36
|
|
|
*/ |
37
|
|
|
private $shipmentRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var OrderInterface |
41
|
|
|
*/ |
42
|
|
|
private $order; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int[] |
46
|
|
|
*/ |
47
|
|
|
private $orderItems; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string[] |
51
|
|
|
*/ |
52
|
|
|
private $trackingNumbers; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
ShipmentItemCreationInterfaceFactory $itemFactory, |
56
|
|
|
ShipmentTrackCreationInterfaceFactory $trackFactory, |
57
|
|
|
ShipOrderInterface $shipOrder, |
58
|
|
|
ShipmentRepositoryInterface $shipmentRepository, |
59
|
|
|
OrderInterface $order |
60
|
|
|
) { |
61
|
|
|
$this->itemFactory = $itemFactory; |
62
|
|
|
$this->trackFactory = $trackFactory; |
63
|
|
|
$this->shipOrder = $shipOrder; |
64
|
|
|
$this->shipmentRepository = $shipmentRepository; |
65
|
|
|
$this->order = $order; |
66
|
|
|
|
67
|
|
|
$this->orderItems = []; |
68
|
|
|
$this->trackingNumbers = []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function forOrder( |
72
|
|
|
OrderInterface $order, |
73
|
|
|
ObjectManagerInterface $objectManager = null |
74
|
|
|
): ShipmentBuilder { |
75
|
|
|
if ($objectManager === null) { |
76
|
|
|
$objectManager = Bootstrap::getObjectManager(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return new static( |
80
|
|
|
$objectManager->create(ShipmentItemCreationInterfaceFactory::class), |
81
|
|
|
$objectManager->create(ShipmentTrackCreationInterfaceFactory::class), |
82
|
|
|
$objectManager->create(ShipOrderInterface::class), |
83
|
|
|
$objectManager->create(ShipmentRepositoryInterface::class), |
84
|
|
|
$order |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function withItem(int $orderItemId, int $qty): ShipmentBuilder |
89
|
|
|
{ |
90
|
|
|
$builder = clone $this; |
91
|
|
|
|
92
|
|
|
$builder->orderItems[$orderItemId] = $qty; |
93
|
|
|
|
94
|
|
|
return $builder; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function withTrackingNumbers(string ...$trackingNumbers): ShipmentBuilder |
98
|
|
|
{ |
99
|
|
|
$builder = clone $this; |
100
|
|
|
|
101
|
|
|
$builder->trackingNumbers = $trackingNumbers; |
102
|
|
|
|
103
|
|
|
return $builder; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function build(): ShipmentInterface |
107
|
|
|
{ |
108
|
|
|
$shipmentItems = []; |
109
|
|
|
|
110
|
|
|
foreach ($this->orderItems as $orderItemId => $qty) { |
111
|
|
|
$shipmentItem = $this->itemFactory->create(); |
112
|
|
|
$shipmentItem->setOrderItemId($orderItemId); |
113
|
|
|
$shipmentItem->setQty($qty); |
114
|
|
|
$shipmentItems[] = $shipmentItem; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$tracks = array_map( |
118
|
|
|
function (string $trackingNumber) { |
119
|
|
|
$carrierCode = strtok($this->order->getShippingMethod(), '_'); |
|
|
|
|
120
|
|
|
$track = $this->trackFactory->create(); |
121
|
|
|
$track->setCarrierCode($carrierCode); |
122
|
|
|
$track->setTitle($carrierCode); |
123
|
|
|
$track->setTrackNumber($trackingNumber); |
124
|
|
|
|
125
|
|
|
return $track; |
126
|
|
|
}, |
127
|
|
|
$this->trackingNumbers |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$shipmentId = $this->shipOrder->execute( |
131
|
|
|
$this->order->getEntityId(), |
132
|
|
|
$shipmentItems, |
133
|
|
|
false, |
134
|
|
|
false, |
135
|
|
|
null, |
136
|
|
|
$tracks |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$shipment = $this->shipmentRepository->get($shipmentId); |
140
|
|
|
if (!empty($this->trackingNumbers)) { |
141
|
|
|
$shipment->setShippingLabel('%PDF-1.4'); |
142
|
|
|
$this->shipmentRepository->save($shipment); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $shipment; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths