Skip to content

Commit dc333b4

Browse files
author
James Halsall
committed
Fix count SQL on products sold collection
1 parent 3f75fac commit dc333b4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace Magento\Reports\Model\ResourceModel\Product\Sold;
1313

14+
use Magento\Framework\DB\Select;
15+
1416
/**
1517
* @SuppressWarnings(PHPMD.DepthOfInheritance)
1618
*/
@@ -109,6 +111,19 @@ public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
109111
return $this;
110112
}
111113

114+
/**
115+
* @return Select
116+
*/
117+
public function getSelectCountSql()
118+
{
119+
$countSelect = clone parent::getSelectCountSql();
120+
121+
$countSelect->reset(Select::COLUMNS);
122+
$countSelect->columns('COUNT(DISTINCT order_items.item_id)');
123+
124+
return $countSelect;
125+
}
126+
112127
/**
113128
* Prepare between sql
114129
*
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Magento\Reports\Test\Unit\Model\ResourceModel\Product\Sold\Collection;
4+
5+
use Magento\Framework\DB\Select;
6+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
7+
use Magento\Reports\Model\ResourceModel\Product\Sold\Collection;
8+
9+
/**
10+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11+
*/
12+
class CollectionTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var ObjectManager
16+
*/
17+
protected $objectManager;
18+
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
protected $selectMock;
23+
24+
protected function setUp()
25+
{
26+
$this->objectManager = new ObjectManager($this);
27+
$this->selectMock = $this->getMock(Select::class, [], [], '', false);
28+
}
29+
30+
public function testGetSelectCountSql()
31+
{
32+
/** @var $collection \PHPUnit_Framework_MockObject_MockObject */
33+
$constructArgs = $this->objectManager->getConstructArguments(Collection::class);
34+
$collection = $this->getMock(Collection::class, ['getSelect'], $constructArgs, '', false);
35+
36+
$collection->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock);
37+
38+
$this->selectMock->expects($this->atLeastOnce())->method('reset')->willReturnSelf();
39+
$this->selectMock->expects($this->exactly(2))->method('columns')->willReturnSelf();
40+
41+
$this->selectMock->expects($this->at(6))->method('columns')->with('COUNT(DISTINCT main_table.entity_id)');
42+
43+
$this->selectMock->expects($this->at(7))->method('reset')->with(Select::COLUMNS);
44+
$this->selectMock->expects($this->at(8))->method('columns')->with('COUNT(DISTINCT order_items.item_id)');
45+
46+
$this->assertEquals($this->selectMock, $collection->getSelectCountSql());
47+
}
48+
}

0 commit comments

Comments
 (0)