|
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\Reports\Test\Unit\Model\ResourceModel\Product\Sold\Collection;
|
8 | 9 |
|
| 10 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
9 | 11 | use Magento\Framework\DB\Select;
|
10 |
| -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
11 | 12 | use Magento\Reports\Model\ResourceModel\Product\Sold\Collection;
|
| 13 | +use Magento\Sales\Model\Order; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
12 | 16 |
|
13 | 17 | /**
|
| 18 | + * Verify data collection class. |
| 19 | + * |
14 | 20 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
15 | 21 | */
|
16 |
| -class CollectionTest extends \PHPUnit\Framework\TestCase |
| 22 | +class CollectionTest extends TestCase |
17 | 23 | {
|
18 | 24 | /**
|
19 |
| - * @var ObjectManager |
| 25 | + * @var AdapterInterface|MockObject |
20 | 26 | */
|
21 |
| - protected $objectManager; |
| 27 | + protected $adapterMock; |
22 | 28 |
|
23 | 29 | /**
|
24 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 30 | + * @var Select|MockObject |
25 | 31 | */
|
26 | 32 | protected $selectMock;
|
27 | 33 |
|
| 34 | + /** |
| 35 | + * @var Collection; |
| 36 | + */ |
| 37 | + protected $collection; |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritDoc |
| 41 | + */ |
28 | 42 | protected function setUp()
|
29 | 43 | {
|
30 |
| - $this->objectManager = new ObjectManager($this); |
31 | 44 | $this->selectMock = $this->createMock(Select::class);
|
32 |
| - } |
33 |
| - |
34 |
| - public function testGetSelectCountSql() |
35 |
| - { |
36 |
| - /** @var $collection \PHPUnit_Framework_MockObject_MockObject */ |
37 |
| - $collection = $this->getMockBuilder(Collection::class) |
38 |
| - ->setMethods(['getSelect']) |
| 45 | + $this->adapterMock = $this->createMock(AdapterInterface::class); |
| 46 | + $this->collection = $this->getMockBuilder(Collection::class) |
| 47 | + ->setMethods([ |
| 48 | + 'getSelect', |
| 49 | + 'getConnection', |
| 50 | + 'getTable' |
| 51 | + ]) |
39 | 52 | ->disableOriginalConstructor()
|
40 | 53 | ->getMock();
|
| 54 | + } |
41 | 55 |
|
42 |
| - $collection->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock); |
43 |
| - |
44 |
| - $this->selectMock->expects($this->atLeastOnce())->method('reset')->willReturnSelf(); |
45 |
| - $this->selectMock->expects($this->exactly(2))->method('columns')->willReturnSelf(); |
| 56 | + /** |
| 57 | + * Verify get select count sql. |
| 58 | + * |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + public function testGetSelectCountSql(): void |
| 62 | + { |
| 63 | + $this->collection->expects($this->atLeastOnce()) |
| 64 | + ->method('getSelect') |
| 65 | + ->willReturn($this->selectMock); |
| 66 | + $this->selectMock->expects($this->atLeastOnce()) |
| 67 | + ->method('reset') |
| 68 | + ->willReturnSelf(); |
| 69 | + $this->selectMock->expects($this->exactly(2)) |
| 70 | + ->method('columns') |
| 71 | + ->willReturnSelf(); |
| 72 | + $this->selectMock->expects($this->at(6)) |
| 73 | + ->method('columns') |
| 74 | + ->with('COUNT(DISTINCT main_table.entity_id)'); |
| 75 | + $this->selectMock->expects($this->at(7)) |
| 76 | + ->method('reset') |
| 77 | + ->with(Select::COLUMNS); |
| 78 | + $this->selectMock->expects($this->at(8)) |
| 79 | + ->method('columns') |
| 80 | + ->with('COUNT(DISTINCT order_items.item_id)'); |
46 | 81 |
|
47 |
| - $this->selectMock->expects($this->at(6))->method('columns')->with('COUNT(DISTINCT main_table.entity_id)'); |
| 82 | + $this->assertEquals($this->selectMock, $this->collection->getSelectCountSql()); |
| 83 | + } |
48 | 84 |
|
49 |
| - $this->selectMock->expects($this->at(7))->method('reset')->with(Select::COLUMNS); |
50 |
| - $this->selectMock->expects($this->at(8))->method('columns')->with('COUNT(DISTINCT order_items.item_id)'); |
| 85 | + /** |
| 86 | + * Verify add ordered qty. |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public function testAddOrderedQty(): void |
| 91 | + { |
| 92 | + $this->collection->expects($this->once()) |
| 93 | + ->method('getConnection') |
| 94 | + ->willReturn($this->adapterMock); |
| 95 | + $this->adapterMock->expects($this->once()) |
| 96 | + ->method('quoteIdentifier') |
| 97 | + ->with('order') |
| 98 | + ->willReturn('sales_order'); |
| 99 | + $this->adapterMock->expects($this->once()) |
| 100 | + ->method('quoteInto') |
| 101 | + ->with('sales_order.state <> ?', Order::STATE_CANCELED) |
| 102 | + ->willReturn(''); |
| 103 | + $this->collection->expects($this->atLeastOnce()) |
| 104 | + ->method('getSelect') |
| 105 | + ->willReturn($this->selectMock); |
| 106 | + $this->collection->expects($this->exactly(2)) |
| 107 | + ->method('getTable') |
| 108 | + ->withConsecutive( |
| 109 | + ['sales_order_item'], |
| 110 | + ['sales_order'] |
| 111 | + )->willReturnOnConsecutiveCalls( |
| 112 | + 'sales_order_item', |
| 113 | + 'sales_order' |
| 114 | + ); |
| 115 | + $this->selectMock->expects($this->atLeastOnce()) |
| 116 | + ->method('reset') |
| 117 | + ->willReturnSelf(); |
| 118 | + $this->selectMock->expects($this->atLeastOnce()) |
| 119 | + ->method('from') |
| 120 | + ->with( |
| 121 | + [ 'order_items' => 'sales_order_item'], |
| 122 | + [ |
| 123 | + 'ordered_qty' => 'order_items.qty_ordered', |
| 124 | + 'order_items_name' => 'order_items.name', |
| 125 | + 'order_items_sku' => 'order_items.sku' |
| 126 | + ] |
| 127 | + ) |
| 128 | + ->willReturnSelf(); |
| 129 | + $this->selectMock->expects($this->atLeastOnce()) |
| 130 | + ->method('joinInner') |
| 131 | + ->with( |
| 132 | + ['order' => 'sales_order'], |
| 133 | + 'sales_order.entity_id = order_items.order_id AND ', |
| 134 | + [] |
| 135 | + ) |
| 136 | + ->willReturnSelf(); |
| 137 | + $this->selectMock->expects($this->atLeastOnce()) |
| 138 | + ->method('where') |
| 139 | + ->with('order_items.parent_item_id IS NULL') |
| 140 | + ->willReturnSelf(); |
| 141 | + $this->selectMock->expects($this->atLeastOnce()) |
| 142 | + ->method('having') |
| 143 | + ->with('order_items.qty_ordered > ?', 0) |
| 144 | + ->willReturnSelf(); |
| 145 | + $this->selectMock->expects($this->atLeastOnce()) |
| 146 | + ->method('columns') |
| 147 | + ->with('SUM(order_items.qty_ordered) as ordered_qty') |
| 148 | + ->willReturnSelf(); |
| 149 | + $this->selectMock->expects($this->atLeastOnce()) |
| 150 | + ->method('group') |
| 151 | + ->with('order_items.sku') |
| 152 | + ->willReturnSelf(); |
51 | 153 |
|
52 |
| - $this->assertEquals($this->selectMock, $collection->getSelectCountSql()); |
| 154 | + $this->assertEquals($this->collection, $this->collection->addOrderedQty()); |
53 | 155 | }
|
54 | 156 | }
|
0 commit comments