Skip to content

Commit 40311e9

Browse files
committed
Cover unit test
1 parent ffa3d2e commit 40311e9

File tree

1 file changed

+123
-21
lines changed
  • app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/Sold/Collection

1 file changed

+123
-21
lines changed

app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/Sold/Collection/CollectionTest.php

Lines changed: 123 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,154 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Reports\Test\Unit\Model\ResourceModel\Product\Sold\Collection;
89

10+
use Magento\Framework\DB\Adapter\AdapterInterface;
911
use Magento\Framework\DB\Select;
10-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1112
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;
1216

1317
/**
18+
* Verify data collection class.
19+
*
1420
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1521
*/
16-
class CollectionTest extends \PHPUnit\Framework\TestCase
22+
class CollectionTest extends TestCase
1723
{
1824
/**
19-
* @var ObjectManager
25+
* @var AdapterInterface|MockObject
2026
*/
21-
protected $objectManager;
27+
protected $adapterMock;
2228

2329
/**
24-
* @var \PHPUnit_Framework_MockObject_MockObject
30+
* @var Select|MockObject
2531
*/
2632
protected $selectMock;
2733

34+
/**
35+
* @var Collection;
36+
*/
37+
protected $collection;
38+
39+
/**
40+
* @inheritDoc
41+
*/
2842
protected function setUp()
2943
{
30-
$this->objectManager = new ObjectManager($this);
3144
$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+
])
3952
->disableOriginalConstructor()
4053
->getMock();
54+
}
4155

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)');
4681

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+
}
4884

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();
51153

52-
$this->assertEquals($this->selectMock, $collection->getSelectCountSql());
154+
$this->assertEquals($this->collection, $this->collection->addOrderedQty());
53155
}
54156
}

0 commit comments

Comments
 (0)