|
20 | 20 | use Magento\Framework\Mail\EmailMessageInterface; |
21 | 21 | use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
22 | 22 | use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 23 | +use Magento\Sales\Api\Data\ShipmentInterface; |
23 | 24 | use Magento\Sales\Api\InvoiceRepositoryInterface; |
24 | 25 | use Magento\Sales\Api\OrderRepositoryInterface; |
25 | 26 | use Magento\Sales\Model\Order; |
@@ -139,21 +140,33 @@ private function createInvoiceForOrder(Order $order): void |
139 | 140 | } |
140 | 141 |
|
141 | 142 | /** |
142 | | - * Creates a shipment for the order from fixtures. |
| 143 | + * Gets order from fixture storage. |
143 | 144 | * |
144 | | - * @return \Magento\Sales\Api\Data\ShipmentInterface |
145 | | - * @throws LocalizedException |
| 145 | + * @return Order |
146 | 146 | */ |
147 | | - private function createShipmentForOrder() |
| 147 | + private function getOrderFromFixture(): Order |
148 | 148 | { |
149 | 149 | $fixtures = DataFixtureStorageManager::getStorage(); |
150 | 150 | /** @var Order $fixtureOrder */ |
151 | 151 | $fixtureOrder = $fixtures->get('order'); |
152 | 152 | $objectManager = Bootstrap::getObjectManager(); |
153 | 153 | $orderRepository = $objectManager->get(OrderRepositoryInterface::class); |
| 154 | + return $orderRepository->get((int)$fixtureOrder->getEntityId()); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates a shipment for the order from fixtures. |
| 159 | + * |
| 160 | + * @return ShipmentInterface |
| 161 | + * @throws LocalizedException |
| 162 | + */ |
| 163 | + private function createShipmentForOrder(): ShipmentInterface |
| 164 | + { |
| 165 | + $order = $this->getOrderFromFixture(); |
| 166 | + $objectManager = Bootstrap::getObjectManager(); |
154 | 167 | $shipmentRepository = $objectManager->get(ShipmentRepositoryInterface::class); |
155 | 168 | $shipmentFactory = $objectManager->get(ShipmentFactory::class); |
156 | | - $order = $orderRepository->get((int)$fixtureOrder->getEntityId()); |
| 169 | + $orderRepository = $objectManager->get(OrderRepositoryInterface::class); |
157 | 170 | $this->createInvoiceForOrder($order); |
158 | 171 | $quantities = $this->calculateShippableQuantities($order); |
159 | 172 | $shipment = $shipmentFactory->create($order, $quantities); |
@@ -200,9 +213,22 @@ private function assertShipmentEmailContent(EmailMessageInterface $email): void |
200 | 213 | $email->getSubject(), |
201 | 214 | 'Email subject should contain shipment confirmation text.' |
202 | 215 | ); |
| 216 | + |
| 217 | + // Assert getTo() returns a non-empty array |
| 218 | + $recipients = $email->getTo(); |
| 219 | + $this->assertNotEmpty( |
| 220 | + $recipients, |
| 221 | + 'Email should have at least one recipient.' |
| 222 | + ); |
| 223 | + $this->assertIsArray( |
| 224 | + $recipients, |
| 225 | + 'Email recipients should be returned as an array.' |
| 226 | + ); |
| 227 | + |
| 228 | + // Now safely access the first recipient |
203 | 229 | $this->assertEquals( |
204 | 230 | 'async-shipment@example.com', |
205 | | - $email->getTo()[0]->getEmail(), |
| 231 | + $recipients[0]->getEmail(), |
206 | 232 | 'Email should be sent to the customer email address.' |
207 | 233 | ); |
208 | 234 | } |
|
0 commit comments