GridAsyncInsertTest.php 3.91 KB
Newer Older
Ketan's avatar
Ketan committed
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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Sales\Model;

use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\ResourceModel\Grid as AbstractGrid;
use Magento\Sales\Model\ResourceModel\Order\Grid;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;

/**
 * Class for testing asynchronous inserts into grid.
 */
class GridAsyncInsertTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ObjectManager
     */
    private $objectManager;

    /**
     * @var GridAsyncInsert
     */
    private $gridAsyncInsert;

    /**
     * @var AdapterInterface
     */
    private $connection;

    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;

    /**
     * @var AbstractGrid
     */
    private $grid;

    protected function setUp()
    {
        $this->objectManager = Bootstrap::getObjectManager();

        /** @var ResourceConnection $resourceConnection */
        $resourceConnection = $this->objectManager->get(ResourceConnection::class);
        $this->connection = $resourceConnection->getConnection('sales');
        $this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
        $this->grid = $this->objectManager->get(Grid::class);

        $this->gridAsyncInsert = $this->objectManager->create(
            GridAsyncInsert::class,
            [
                'entityGrid' => $this->grid,
            ]
        );
    }

    /**
     * Checks a case when order's grid should be updated asynchronously.
     *
     * @magentoConfigFixture default/dev/grid/async_indexing 1
     * @magentoDataFixture Magento/Sales/_files/order.php
     * @return void
     */
    public function testExecuteAsyncUpdateOrderGrid()
    {
        $order = $this->getOrder('100000001');
        $this->performUpdateAssertions($order);

        // to un-sync main table and grid table need to wait at least one second
        sleep(1);
        $order->setStatus('complete');
        $this->orderRepository->save($order);

        $gridRow = $this->getGridRow($order->getEntityId());
        self::assertNotEquals($order->getStatus(), $gridRow['status']);

        $this->gridAsyncInsert->asyncInsert();
        $this->performUpdateAssertions($order);
    }

    /**
     * Loads order entity by provided order increment ID.
     *
     * @param string $incrementId
     * @return OrderInterface
     */
    private function getOrder(string $incrementId) : OrderInterface
    {
        /** @var SearchCriteria $searchCriteria */
        $searchCriteria = $this->objectManager->get(SearchCriteriaBuilder::class)
            ->addFilter('increment_id', $incrementId)
            ->create();

        $items = $this->orderRepository->getList($searchCriteria)
            ->getItems();

        return array_pop($items);
    }

    /**
     * Gets row from `sales_order_grid` table by order's ID.
     *
     * @param int $entityId
     * @return array
     */
    private function getGridRow(int $entityId) : array
    {
        $tableName = $this->grid->getGridTable();
        $select = $this->connection->select()
            ->from($tableName)
            ->where($tableName . '.entity_id = ?', $entityId);

        return $this->connection->fetchRow($select);
    }

    /**
     * Perform assertions for updating grid test.
     *
     * @param OrderInterface $order
     * @return void
     */
    private function performUpdateAssertions(OrderInterface $order)
    {
        $gridRow = $this->getGridRow($order->getEntityId());

        self::assertEquals($order->getStatus(), $gridRow['status']);
        self::assertEquals($order->getUpdatedAt(), $gridRow['updated_at']);
    }
}