|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Test\Unit\Model\ResourceModel; |
| 9 | + |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 11 | +use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface; |
| 12 | +use Magento\Framework\DB\Adapter\AdapterInterface as ConnectionAdapterInterface; |
| 13 | +use Magento\Sales\Model\ResourceModel\Grid; |
| 14 | + |
| 15 | +/** |
| 16 | + * Unit tests for \Magento\Sales\Model\ResourceModel\Grid class |
| 17 | + */ |
| 18 | +class GridTest extends \PHPUnit\Framework\TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Grid |
| 22 | + */ |
| 23 | + private $grid; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var NotSyncedDataProviderInterface|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $notSyncedDataProvider; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ConnectionAdapterInterface|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $connection; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + private $mainTable = 'sales_order'; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + private $gridTable = 'sales_order_grid'; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var array |
| 47 | + */ |
| 48 | + private $columns = [ |
| 49 | + 'column_1_key' => 'column_1_value', |
| 50 | + 'column_2_key' => 'column_2_value' |
| 51 | + ]; |
| 52 | + |
| 53 | + /** |
| 54 | + * @inheritdoc |
| 55 | + */ |
| 56 | + protected function setUp() |
| 57 | + { |
| 58 | + $objectManager = new ObjectManager($this); |
| 59 | + $this->notSyncedDataProvider = $this->getMockBuilder(NotSyncedDataProviderInterface::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->setMethods(['getIds']) |
| 62 | + ->getMockForAbstractClass(); |
| 63 | + $this->connection = $this->getMockBuilder(ConnectionAdapterInterface::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->setMethods(['select', 'fetchAll', 'insertOnDuplicate']) |
| 66 | + ->getMockForAbstractClass(); |
| 67 | + |
| 68 | + $this->grid = $objectManager->getObject( |
| 69 | + \Magento\Sales\Model\ResourceModel\Grid::class, |
| 70 | + [ |
| 71 | + 'notSyncedDataProvider' => $this->notSyncedDataProvider, |
| 72 | + 'mainTableName' => $this->mainTable, |
| 73 | + 'gridTableName' => $this->gridTable, |
| 74 | + 'connection' => $this->connection, |
| 75 | + '_tables' => ['sales_order' => $this->mainTable, 'sales_order_grid' => $this->gridTable], |
| 76 | + 'columns' => $this->columns |
| 77 | + ] |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test for refreshBySchedule() method |
| 83 | + */ |
| 84 | + public function testRefreshBySchedule() |
| 85 | + { |
| 86 | + $notSyncedIds = ['1', '2', '3']; |
| 87 | + $fetchResult = ['column_1' => '1', 'column_2' => '2']; |
| 88 | + |
| 89 | + $this->notSyncedDataProvider->expects($this->atLeastOnce())->method('getIds')->willReturn($notSyncedIds); |
| 90 | + $select = $this->getMockBuilder(\Magento\Framework\DB\Select::class) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->setMethods(['from', 'columns', 'where']) |
| 93 | + ->getMock(); |
| 94 | + $select->expects($this->atLeastOnce())->method('from')->with(['sales_order' => $this->mainTable], []) |
| 95 | + ->willReturnSelf(); |
| 96 | + $select->expects($this->atLeastOnce())->method('columns')->willReturnSelf(); |
| 97 | + $select->expects($this->atLeastOnce())->method('where') |
| 98 | + ->with($this->mainTable . '.entity_id IN (?)', $notSyncedIds) |
| 99 | + ->willReturnSelf(); |
| 100 | + |
| 101 | + $this->connection->expects($this->atLeastOnce())->method('select')->willReturn($select); |
| 102 | + $this->connection->expects($this->atLeastOnce())->method('fetchAll')->with($select)->willReturn($fetchResult); |
| 103 | + $this->connection->expects($this->atLeastOnce())->method('insertOnDuplicate') |
| 104 | + ->with($this->gridTable, $fetchResult, array_keys($this->columns)) |
| 105 | + ->willReturn(array_count_values($notSyncedIds)); |
| 106 | + |
| 107 | + $this->grid->refreshBySchedule(); |
| 108 | + } |
| 109 | +} |
0 commit comments