Skip to content

Commit

Permalink
Fix count SQL on products sold collection
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halsall committed Mar 16, 2017
1 parent 3f75fac commit dc333b4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
namespace Magento\Reports\Model\ResourceModel\Product\Sold;

use Magento\Framework\DB\Select;

/**
* @SuppressWarnings(PHPMD.DepthOfInheritance)
*/
Expand Down Expand Up @@ -109,6 +111,19 @@ public function setOrder($attribute, $dir = self::SORT_ORDER_DESC)
return $this;
}

/**
* @return Select
*/
public function getSelectCountSql()
{
$countSelect = clone parent::getSelectCountSql();

$countSelect->reset(Select::COLUMNS);
$countSelect->columns('COUNT(DISTINCT order_items.item_id)');

return $countSelect;
}

/**
* Prepare between sql
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

use Magento\Framework\DB\Select;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Reports\Model\ResourceModel\Product\Sold\Collection;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CollectionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $selectMock;

protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->selectMock = $this->getMock(Select::class, [], [], '', false);
}

public function testGetSelectCountSql()
{
/** @var $collection \PHPUnit_Framework_MockObject_MockObject */
$constructArgs = $this->objectManager->getConstructArguments(Collection::class);
$collection = $this->getMock(Collection::class, ['getSelect'], $constructArgs, '', false);

$collection->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock);

$this->selectMock->expects($this->atLeastOnce())->method('reset')->willReturnSelf();
$this->selectMock->expects($this->exactly(2))->method('columns')->willReturnSelf();

$this->selectMock->expects($this->at(6))->method('columns')->with('COUNT(DISTINCT main_table.entity_id)');

$this->selectMock->expects($this->at(7))->method('reset')->with(Select::COLUMNS);
$this->selectMock->expects($this->at(8))->method('columns')->with('COUNT(DISTINCT order_items.item_id)');

$this->assertEquals($this->selectMock, $collection->getSelectCountSql());
}
}

0 comments on commit dc333b4

Please sign in to comment.