Skip to content

Commit 211fff1

Browse files
Merge branch 'MDVA-158' into 2-0-8-backlog
2 parents a5f1855 + 3079122 commit 211fff1

File tree

2 files changed

+157
-5
lines changed

2 files changed

+157
-5
lines changed

app/code/Magento/Sales/Model/OrderRepository.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Sales\Api\Data\ShippingAssignmentInterface;
1616
use Magento\Framework\Exception\NoSuchEntityException;
1717
use Magento\Framework\Exception\InputException;
18+
use Magento\Framework\Api\SortOrder;
1819

1920
/**
2021
* Repository class for @see OrderInterface
@@ -96,15 +97,25 @@ public function get($id)
9697
*/
9798
public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria)
9899
{
99-
//@TODO: fix search logic
100100
/** @var \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult */
101101
$searchResult = $this->searchResultFactory->create();
102102
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
103-
foreach ($filterGroup->getFilters() as $filter) {
104-
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
105-
$searchResult->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
106-
}
103+
$this->addFilterGroupToCollection($filterGroup, $searchResult);
104+
}
105+
106+
$sortOrders = $searchCriteria->getSortOrders();
107+
if ($sortOrders === null) {
108+
$sortOrders = [];
109+
}
110+
/** @var \Magento\Framework\Api\SortOrder $sortOrder */
111+
foreach ($sortOrders as $sortOrder) {
112+
$field = $sortOrder->getField();
113+
$searchResult->addOrder(
114+
$field,
115+
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
116+
);
107117
}
118+
$searchResult->setSearchCriteria($searchCriteria);
108119
$searchResult->setCurPage($searchCriteria->getCurrentPage());
109120
$searchResult->setPageSize($searchCriteria->getPageSize());
110121
foreach ($searchResult->getItems() as $order) {
@@ -202,4 +213,28 @@ private function getShippingAssignmentBuilderDependency()
202213
}
203214
return $this->shippingAssignmentBuilder;
204215
}
216+
217+
/**
218+
* Helper function that adds a FilterGroup to the collection.
219+
*
220+
* @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
221+
* @param \Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult
222+
* @return void
223+
* @throws \Magento\Framework\Exception\InputException
224+
*/
225+
protected function addFilterGroupToCollection(
226+
\Magento\Framework\Api\Search\FilterGroup $filterGroup,
227+
\Magento\Sales\Api\Data\OrderSearchResultInterface $searchResult
228+
) {
229+
$fields = [];
230+
$conditions = [];
231+
foreach ($filterGroup->getFilters() as $filter) {
232+
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
233+
$conditions[] = [$condition => $filter->getValue()];
234+
$fields[] = $filter->getField();
235+
}
236+
if ($fields) {
237+
$searchResult->addFieldToFilter($fields, $conditions);
238+
}
239+
}
205240
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Test\Unit\Model;
7+
8+
use Magento\Sales\Model\OrderRepository;
9+
use Magento\Sales\Model\ResourceModel\Metadata;
10+
use Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory as SearchResultFactory;
11+
use Magento\Framework\Api\SortOrder;
12+
13+
/**
14+
* Class OrderRepositoryTest
15+
*/
16+
class OrderRepositoryTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @var \Magento\Sales\Model\OrderRepository
20+
*/
21+
protected $model;
22+
23+
/**
24+
* @var Metadata|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $metadata;
27+
28+
/**
29+
* @var SearchResultFactory|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $searchResultFactory;
32+
33+
/**
34+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
35+
*/
36+
protected $objectManager;
37+
38+
/**
39+
* Setup the test
40+
*/
41+
protected function setUp()
42+
{
43+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
44+
45+
$className = 'Magento\Sales\Model\ResourceModel\Metadata';
46+
$this->metadata = $this->getMock($className, [], [], '', false);
47+
48+
$className = 'Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory';
49+
$this->searchResultFactory = $this->getMock($className, ['create'], [], '', false);
50+
51+
$this->model = $this->objectManager->getObject(
52+
'\Magento\Sales\Model\OrderRepository',
53+
[
54+
'metadata' => $this->metadata,
55+
'searchResultFactory' => $this->searchResultFactory,
56+
]
57+
);
58+
}
59+
60+
/**
61+
* TODO: Cover with unit tests the other methods in the repository
62+
* test GetList
63+
*/
64+
public function testGetList()
65+
{
66+
$fieldName = 'field';
67+
$searchCriteriaMock = $this->getMock('Magento\Framework\Api\SearchCriteria', [], [], '', false);
68+
69+
$collectionMock = $this->getMock('Magento\Sales\Model\ResourceModel\Order\Collection', [], [], '', false);
70+
71+
$filterGroupMock = $this->getMock('\Magento\Framework\Api\Search\FilterGroup', [], [], '', false);
72+
$filterGroupFilterMock = $this->getMock('\Magento\Framework\Api\Filter', [], [], '', false);
73+
$sortOrderMock = $this->getMock('\Magento\Framework\Api\SortOrder', [], [], '', false);
74+
$itemsMock = $this->getMock('Magento\Sales\Model\Order', [], [], '', false);
75+
76+
$extensionAttributes = $this->getMock(
77+
'\Magento\Sales\Api\Data\OrderExtension',
78+
['getShippingAssignments'],
79+
[],
80+
'',
81+
false
82+
);
83+
$shippingAssignmentBuilder = $this->getMock(
84+
'\Magento\Sales\Model\Order\ShippingAssignmentBuilder',
85+
[],
86+
[],
87+
'',
88+
false
89+
);
90+
91+
$itemsMock->expects($this->once())->method('getExtensionAttributes')->willReturn($extensionAttributes);
92+
$extensionAttributes->expects($this->any())
93+
->method('getShippingAssignments')
94+
->willReturn($shippingAssignmentBuilder);
95+
96+
$this->searchResultFactory->expects($this->once())->method('create')->willReturn($collectionMock);
97+
98+
$searchCriteriaMock->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroupMock]);
99+
$filterGroupMock->expects($this->once())->method('getFilters')->willReturn([$filterGroupFilterMock]);
100+
$filterGroupFilterMock->expects($this->exactly(2))->method('getConditionType')->willReturn('eq');
101+
$filterGroupFilterMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
102+
$filterGroupFilterMock->expects($this->once())->method('getValue')->willReturn('value');
103+
$sortOrderMock->expects($this->once())->method('getDirection');
104+
$searchCriteriaMock->expects($this->once())->method('getSortOrders')->willReturn([$sortOrderMock]);
105+
$sortOrderMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
106+
$collectionMock->expects($this->once())->method('addFieldToFilter')
107+
->willReturn(SortOrder::SORT_ASC);
108+
$collectionMock->expects($this->once())->method('addOrder')->with($fieldName, 'DESC');
109+
$searchCriteriaMock->expects($this->once())->method('getCurrentPage')->willReturn(4);
110+
$collectionMock->expects($this->once())->method('setCurPage')->with(4);
111+
$searchCriteriaMock->expects($this->once())->method('getPageSize')->willReturn(42);
112+
$collectionMock->expects($this->once())->method('setPageSize')->with(42);
113+
$collectionMock->expects($this->once())->method('getItems')->willReturn([$itemsMock]);
114+
115+
$this->assertEquals($collectionMock, $this->model->getList($searchCriteriaMock));
116+
}
117+
}

0 commit comments

Comments
 (0)