|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | +declare(strict_types=1); |
6 | 7 |
|
7 | 8 | namespace Magento\Sales\Model\Order;
|
8 | 9 |
|
| 10 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 11 | +use Magento\Payment\Helper\Data; |
| 12 | +use Magento\Sales\Api\Data\CommentInterface; |
| 13 | +use Magento\Sales\Api\Data\OrderInterface; |
| 14 | +use Magento\Sales\Api\Data\ShipmentInterface; |
| 15 | +use Magento\Sales\Api\Data\ShipmentItemInterface; |
| 16 | +use Magento\Sales\Api\Data\ShipmentTrackInterface; |
| 17 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 18 | +use Magento\Sales\Api\ShipmentRepositoryInterface; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\TestFramework\ObjectManager; |
| 21 | + |
9 | 22 | /**
|
10 |
| - * Class ShipmentTest |
11 | 23 | * @magentoAppIsolation enabled
|
12 |
| - * @package Magento\Sales\Model\Order |
| 24 | + * @magentoDataFixture Magento/Sales/_files/order.php |
13 | 25 | */
|
14 | 26 | class ShipmentTest extends \PHPUnit\Framework\TestCase
|
15 | 27 | {
|
| 28 | + /** |
| 29 | + * @var ObjectManager |
| 30 | + */ |
| 31 | + private $objectManager; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ShipmentRepositoryInterface |
| 35 | + */ |
| 36 | + private $shipmentRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp() |
| 42 | + { |
| 43 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 44 | + $this->shipmentRepository = $this->objectManager->get(ShipmentRepositoryInterface::class); |
| 45 | + } |
| 46 | + |
16 | 47 | /**
|
17 | 48 | * Check the correctness and stability of set/get packages of shipment
|
18 | 49 | *
|
19 |
| - * @magentoDataFixture Magento/Sales/_files/order.php |
| 50 | + * @magentoAppArea frontend |
20 | 51 | */
|
21 | 52 | public function testPackages()
|
22 | 53 | {
|
23 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
24 |
| - $objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend'); |
25 |
| - $order = $objectManager->create(\Magento\Sales\Model\Order::class); |
26 |
| - $order->loadByIncrementId('100000001'); |
27 |
| - $order-> setCustomerEmail( '[email protected]'); |
| 54 | + $order = $this->getOrder('100000001'); |
28 | 55 |
|
29 | 56 | $payment = $order->getPayment();
|
30 |
| - $paymentInfoBlock = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
31 |
| - \Magento\Payment\Helper\Data::class |
32 |
| - )->getInfoBlock( |
33 |
| - $payment |
34 |
| - ); |
| 57 | + $paymentInfoBlock = $this->objectManager->get(Data::class) |
| 58 | + ->getInfoBlock($payment); |
35 | 59 | $payment->setBlockMock($paymentInfoBlock);
|
36 | 60 |
|
37 | 61 | $items = [];
|
38 | 62 | foreach ($order->getItems() as $item) {
|
39 | 63 | $items[$item->getId()] = $item->getQtyOrdered();
|
40 | 64 | }
|
41 | 65 | /** @var \Magento\Sales\Model\Order\Shipment $shipment */
|
42 |
| - $shipment = $objectManager->get(ShipmentFactory::class)->create($order, $items); |
| 66 | + $shipment = $this->objectManager->get(ShipmentFactory::class)->create($order, $items); |
43 | 67 |
|
44 | 68 | $packages = [['1'], ['2']];
|
45 | 69 |
|
46 | 70 | $shipment->setPackages($packages);
|
47 |
| - $this->assertEquals($packages, $shipment->getPackages()); |
48 |
| - $shipment->save(); |
49 |
| - $shipment->save(); |
50 |
| - $shipment->load($shipment->getId()); |
51 |
| - $this->assertEquals($packages, $shipment->getPackages()); |
| 71 | + $saved = $this->shipmentRepository->save($shipment); |
| 72 | + self::assertEquals($packages, $saved->getPackages()); |
52 | 73 | }
|
53 | 74 |
|
54 | 75 | /**
|
55 | 76 | * Check that getTracksCollection() always return collection instance.
|
56 |
| - * |
57 |
| - * @magentoDataFixture Magento/Sales/_files/order.php |
58 | 77 | */
|
59 | 78 | public function testAddTrack()
|
60 | 79 | {
|
61 |
| - $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 80 | + $order = $this->getOrder('100000001'); |
62 | 81 |
|
63 |
| - $order = $objectManager->create(\Magento\Sales\Model\Order::class); |
64 |
| - $order->loadByIncrementId('100000001'); |
| 82 | + /** @var ShipmentInterface $shipment */ |
| 83 | + $shipment = $this->objectManager->create(ShipmentInterface::class); |
| 84 | + |
| 85 | + /** @var ShipmentTrackInterface $track */ |
| 86 | + $track = $this->objectManager->create(ShipmentTrackInterface::class); |
| 87 | + $track->setNumber('Test Number') |
| 88 | + ->setTitle('Test Title') |
| 89 | + ->setCarrierCode('Test CODE'); |
65 | 90 |
|
66 | 91 | $items = [];
|
67 | 92 | foreach ($order->getItems() as $item) {
|
68 | 93 | $items[$item->getId()] = $item->getQtyOrdered();
|
69 | 94 | }
|
70 | 95 | /** @var \Magento\Sales\Model\Order\Shipment $shipment */
|
71 |
| - $shipment = $objectManager->get(ShipmentFactory::class)->create($order, $items); |
| 96 | + $shipment = $this->objectManager->get(ShipmentFactory::class)->create($order, $items); |
| 97 | + $shipment->addTrack($track); |
72 | 98 | $shipment->save();
|
| 99 | + $saved = $this->shipmentRepository->save($shipment); |
| 100 | + self::assertNotEmpty($saved->getTracks()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Checks adding comment to the shipment entity. |
| 105 | + */ |
| 106 | + public function testAddComment() |
| 107 | + { |
| 108 | + $message1 = 'Test Comment 1'; |
| 109 | + $message2 = 'Test Comment 2'; |
| 110 | + $order = $this->getOrder('100000001'); |
| 111 | + |
| 112 | + /** @var ShipmentInterface $shipment */ |
| 113 | + $shipment = $this->objectManager->create(ShipmentInterface::class); |
| 114 | + $shipment->setOrder($order) |
| 115 | + ->addItem($this->objectManager->create(ShipmentItemInterface::class)) |
| 116 | + ->addComment($message1) |
| 117 | + ->addComment($message2); |
73 | 118 |
|
74 |
| - /** @var $track \Magento\Sales\Model\Order\Shipment\Track */ |
75 |
| - $track = $objectManager->get(\Magento\Sales\Model\Order\Shipment\Track::class); |
76 |
| - $track->setNumber('Test Number')->setTitle('Test Title')->setCarrierCode('Test CODE'); |
| 119 | + $saved = $this->shipmentRepository->save($shipment); |
| 120 | + |
| 121 | + $comments = $saved->getComments(); |
| 122 | + $actual = array_map(function (CommentInterface $comment) { |
| 123 | + return $comment->getComment(); |
| 124 | + }, $comments); |
| 125 | + self::assertEquals(2, count($actual)); |
| 126 | + self::assertEquals([$message1, $message2], $actual); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Gets order entity by increment id. |
| 131 | + * |
| 132 | + * @param string $incrementId |
| 133 | + * @return OrderInterface |
| 134 | + */ |
| 135 | + private function getOrder(string $incrementId): OrderInterface |
| 136 | + { |
| 137 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 138 | + $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 139 | + $searchCriteria = $searchCriteriaBuilder->addFilter('increment_id', $incrementId) |
| 140 | + ->create(); |
77 | 141 |
|
78 |
| - $this->assertEmpty($shipment->getTracks()); |
79 |
| - $shipment->addTrack($track)->save(); |
| 142 | + /** @var OrderRepositoryInterface $repository */ |
| 143 | + $repository = $this->objectManager->get(OrderRepositoryInterface::class); |
| 144 | + $items = $repository->getList($searchCriteria) |
| 145 | + ->getItems(); |
80 | 146 |
|
81 |
| - //to empty cache |
82 |
| - $shipment->setTracks(null); |
83 |
| - $this->assertNotEmpty($shipment->getTracks()); |
| 147 | + return array_pop($items); |
84 | 148 | }
|
85 | 149 | }
|
0 commit comments