Skip to content

Commit f6f64f5

Browse files
authored
MAGETWO-65730: [GitHub][PR] Fix count SQL on products sold collection #8812
2 parents c2c4f9d + efd4280 commit f6f64f5

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Reports\Test\Unit\Model\ResourceModel\Product\Sold\Collection;
8+
9+
use Magento\Framework\DB\Select;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Reports\Model\ResourceModel\Product\Sold\Collection;
12+
13+
/**
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15+
*/
16+
class CollectionTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var ObjectManager
20+
*/
21+
protected $objectManager;
22+
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $selectMock;
27+
28+
protected function setUp()
29+
{
30+
$this->objectManager = new ObjectManager($this);
31+
$this->selectMock = $this->getMock(Select::class, [], [], '', false);
32+
}
33+
34+
public function testGetSelectCountSql()
35+
{
36+
/** @var $collection \PHPUnit_Framework_MockObject_MockObject */
37+
$constructArgs = $this->objectManager->getConstructArguments(Collection::class);
38+
$collection = $this->getMock(Collection::class, ['getSelect'], $constructArgs, '', false);
39+
40+
$collection->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock);
41+
42+
$this->selectMock->expects($this->atLeastOnce())->method('reset')->willReturnSelf();
43+
$this->selectMock->expects($this->exactly(2))->method('columns')->willReturnSelf();
44+
45+
$this->selectMock->expects($this->at(6))->method('columns')->with('COUNT(DISTINCT main_table.entity_id)');
46+
47+
$this->selectMock->expects($this->at(7))->method('reset')->with(Select::COLUMNS);
48+
$this->selectMock->expects($this->at(8))->method('columns')->with('COUNT(DISTINCT order_items.item_id)');
49+
50+
$this->assertEquals($this->selectMock, $collection->getSelectCountSql());
51+
}
52+
}

0 commit comments

Comments
 (0)