Skip to content

Commit

Permalink
Merge pull request #75 from storekeeper-company/CU-86950b4bv_Order-sh…
Browse files Browse the repository at this point in the history
…ipment-sync-M2---SK-Integration-test-queue-removed

CU-8694eqmtm Adjusted order creation integration test
  • Loading branch information
ikulis committed Jul 10, 2024
2 parents 822ad56 + e3fc0e1 commit 00cbab6
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Helper/Api/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,19 @@ public function getOrders(string $storeId, int $page, int $pageSize): OrderSearc
return $this->orderRepository->getList($searchCriteria);
}

/**
* @param $order
* @return bool
*/
public function allowShipmnetCreation(\Magento\Sales\Api\Data\OrderInterface $order): bool
{
if ($order->getStorekeeperId() && !$order->getStorekeeperShipmentId() && !$order->getOrderDetached()) {
return true;
} else {
return false;
}
}

/**
* Get Order Item price
*
Expand Down
2 changes: 1 addition & 1 deletion Observers/SalesOrderShipmentSaveBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function execute(Observer $observer)
$order = $shipment->getOrder();
$storekeeperId = $order->getStorekeeperId();

if ($order->getStorekeeperId() && !$order->getStorekeeperShipmentId() && !$order->getOrderDetached()) {
if ($this->apiOrders->allowShipmnetCreation($order)) {
$shipmentData = [
'order_id' => $order->getId(),
'storekeeper_id' => $storekeeperId,
Expand Down
92 changes: 92 additions & 0 deletions Test/Integration/ShipmentCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace StoreKeeper\StoreKeeper\Test\Integration;

use Magento\Framework\Event;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\TestFramework\Helper\Bootstrap;

/**
* @magentoDbIsolation enabled
*/
class ShipmentCreationTest extends AbstractTestCase
{

const SHIPMENT_ID = 1;

const SHIPMENT_QUEUE = [
"order_id" => "1",
"storekeeper_id" => "200",
"store_id" => "1",
"items" => [["id" => "999", "quantity" => 1]]
];

protected $shipmentFactory;
protected $shipmentRepository;
protected $shipmentObserver;
protected $json;
protected $apiOrdersMock;
protected $shipment;
protected $orderApiClientMock;

protected function setUp(): void
{
parent::setUp();
$objectManager = new ObjectManager($this);
$this->shipmentFactory = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order\ShipmentFactory::class);
$this->shipmentRepository = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order\ShipmentRepository::class);
$this->json = Bootstrap::getObjectManager()->create(\Magento\Framework\Serialize\Serializer\Json::class);
$this->apiOrdersMock = $this->createMock(\StoreKeeper\StoreKeeper\Helper\Api\Orders::class);
$this->orderApiClientMock = $this->createMock(\StoreKeeper\StoreKeeper\Api\OrderApiClient::class);

$this->orderApiClientMock->method('newOrderShipment')->willReturn(self::SHIPMENT_ID);
$this->apiOrdersMock->method('getStoreKeeperOrder')->willReturn(
['order_items' => [0 => ['is_shipping' => false, 'id' => 999, 'quantity' => 1]]]
);
$this->apiOrdersMock->method('allowShipmnetCreation')->willReturn(true);

$this->shipment = $objectManager->getObject(
\StoreKeeper\StoreKeeper\Model\OrderSync\Shipment::class,
[
'orderApiClient' => $this->orderApiClientMock,
'orderRepository' => $this->orderRepository,
'orderResource' => $this->orderResource
]
);

$this->shipmentObserver = $objectManager->getObject(
\StoreKeeper\StoreKeeper\Observers\SalesOrderShipmentSaveBefore::class,
[
'json' => $this->json,
'apiOrders' => $this->apiOrdersMock,
'shipment' => $this->shipment
]
);
}

/**
* @magentoDataFixture StoreKeeper_StoreKeeper::Test/Integration/_files/order_shipping.php
* @magentoDbIsolation enabled
*/
public function testShipmentCreation()
{
$existingOrder = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order::class)
->loadByIncrementId('100000001');
$shipment = $existingOrder->getShipmentsCollection()->getFirstItem();

//Mock and trigger shipment_save_after event with current order shipment record
$observer = $this->getMockBuilder(Event\Observer::class)
->disableOriginalConstructor()
->getMock();
$observer->method('getEvent')->willReturn(new Event());
$observer->getEvent()->setData('shipment', $shipment);
$this->shipmentObserver->execute($observer);

//Reload order with fresh data
$existingOrder = Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order::class)
->loadByIncrementId('100000001');

//Assert that storekeeper shipment id were generated and assigned to order during consumer run
$this->assertEquals(self::SHIPMENT_ID, $existingOrder->getStorekeeperShipmentId());
}
}
57 changes: 57 additions & 0 deletions Test/Integration/_files/order_shipping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;

Resolver::getInstance()->requireDataFixture('Magento/Sales/_files/order.php');

$objectManager = Bootstrap::getObjectManager();
/** @var \Magento\Sales\Model\Order $order */
$order = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order::class);
$order->loadByIncrementId('100000001');

$order->setData(
'base_to_global_rate',
2
)->setData(
'base_shipping_amount',
20
)->setData(
'base_shipping_canceled',
2
)->setData(
'base_shipping_invoiced',
20
)->setData(
'base_shipping_refunded',
3
)->setData(
'is_virtual',
0
)->setData(
'storekeeper_id',
200
)->setData(
'order_detached',
true
)->save();

$orderItems = $order->getItems();
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
$orderItem = array_values($orderItems)[0];

/** @var \Magento\Sales\Api\Data\ShipmentItemCreationInterface $shipmentItem */
$invoiceItem = $objectManager->create(\Magento\Sales\Api\Data\InvoiceItemCreationInterface::class);
$invoiceItem->setOrderItemId($orderItem->getItemId());
$invoiceItem->setQty($orderItem->getQtyOrdered());
/** @var \Magento\Sales\Api\InvoiceOrderInterface $invoiceOrder */
$invoiceOrder = $objectManager->create(\Magento\Sales\Api\InvoiceOrderInterface::class);
$invoiceOrder->execute($order->getEntityId(), false, [$invoiceItem]);

/** @var \Magento\Sales\Api\Data\ShipmentItemCreationInterface $shipmentItem */
$shipmentItem = $objectManager->create(\Magento\Sales\Api\Data\ShipmentItemCreationInterface::class);
$shipmentItem->setOrderItemId($orderItem->getItemId());
$shipmentItem->setQty($orderItem->getQtyOrdered());
/** @var \Magento\Sales\Api\ShipOrderInterface $shipOrder */
$shipOrder = $objectManager->create(\Magento\Sales\Api\ShipOrderInterface::class);
$shipOrder->execute($order->getEntityId(), [$shipmentItem]);

0 comments on commit 00cbab6

Please sign in to comment.