/** * @return \PHPUnit_Framework_MockObject_MockObject|\Zend_Db_Adapter_Abstract */ public function testSetAddOrder() { $adapter = $this->getMockForAbstractClass('Zend_Db_Adapter_Abstract', [], '', false, true, true, ['fetchAll']); $this->collection->setConnection($adapter); $select = $this->collection->getSelect(); $this->assertEmpty($select->getPart(\Zend_Db_Select::ORDER)); /* Direct access to select object is available and many places are using it for sort order declaration */ $select->order('select_field', \Magento\Framework\Data\Collection::SORT_ORDER_ASC); $this->collection->addOrder('some_field', \Magento\Framework\Data\Collection::SORT_ORDER_ASC); $this->collection->setOrder('other_field', \Magento\Framework\Data\Collection::SORT_ORDER_ASC); $this->collection->addOrder('other_field', \Magento\Framework\Data\Collection::SORT_ORDER_DESC); $this->collection->load(); $selectOrders = $select->getPart(\Zend_Db_Select::ORDER); $this->assertEquals(['select_field', 'ASC'], array_shift($selectOrders)); $this->assertEquals('some_field ASC', (string) array_shift($selectOrders)); $this->assertEquals('other_field DESC', (string) array_shift($selectOrders)); $this->assertEmpty(array_shift($selectOrders)); return $adapter; }
/** * Set sorting order * * Parameter $attribute can also be an array of attributes * * @param string|array $attribute * @param string $dir * @return $this */ public function setOrder($attribute, $dir = self::SORT_ORDER_ASC) { if (is_array($attribute)) { foreach ($attribute as $attr) { parent::setOrder($attr, $dir); } return $this; } return parent::setOrder($attribute, $dir); }