Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/code/Magento/CatalogInventory/Model/Source/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ public function getAllOptions()
['value' => \Magento\CatalogInventory\Model\Stock::STOCK_OUT_OF_STOCK, 'label' => __('Out of Stock')]
];
}

/**
* Add Value Sort To Collection Select.
*
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
* @param string $dir
*
* @return $this
*/
public function addValueSortToCollection($collection, $dir = \Magento\Framework\Data\Collection::SORT_ORDER_DESC)
{
$collection->getSelect()->joinLeft(
['stock_item_table' => 'cataloginventory_stock_item'],
"e.entity_id=stock_item_table.product_id",
[]
);
$collection->getSelect()->order("stock_item_table.qty $dir");
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogInventory\Test\Unit\Model\Source;

use PHPUnit\Framework\TestCase;

class StockTest extends TestCase
{
/**
* @var \Magento\CatalogInventory\Model\Source\Stock
*/
private $model;

protected function setUp()
{
$this->model = new \Magento\CatalogInventory\Model\Source\Stock();
}

public function testAddValueSortToCollection()
{
$selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
$collectionMock = $this->createMock(\Magento\Eav\Model\Entity\Collection\AbstractCollection::class);
$collectionMock->expects($this->atLeastOnce())->method('getSelect')->willReturn($selectMock);

$selectMock->expects($this->once())
->method('joinLeft')
->with(
['stock_item_table' => 'cataloginventory_stock_item'],
"e.entity_id=stock_item_table.product_id",
[]
)
->willReturnSelf();
$selectMock->expects($this->once())
->method('order')
->with("stock_item_table.qty DESC")
->willReturnSelf();

$this->model->addValueSortToCollection($collectionMock);
}
}