Skip to content

Commit 118a6b5

Browse files
author
Robert He
committed
MAGETWO-33665 : Refactor Quote module to use mutable data object interfaces
-- fixes from code review -- fixes to testcases
1 parent fcf588d commit 118a6b5

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

app/code/Magento/Quote/Api/Data/CartSearchResultsInterface.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ interface CartSearchResultsInterface extends \Magento\Framework\Api\SearchResult
1212
*/
1313
const KEY_ITEMS = 'items';
1414

15+
const KEY_SEARCH_CRITERIA = 'search_criteria';
16+
17+
const KEY_TOTAL_COUNT = 'total_count';
18+
1519
/**#@-*/
1620

1721
/**
@@ -28,4 +32,34 @@ public function getItems();
2832
* @return $this
2933
*/
3034
public function setItems(array $items = null);
35+
36+
/**
37+
* Get search criteria.
38+
*
39+
* @return \Magento\Framework\Api\SearchCriteriaInterface
40+
*/
41+
public function getSearchCriteria();
42+
43+
/**
44+
* Set search criteria.
45+
*
46+
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
47+
* @return $this
48+
*/
49+
public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
50+
51+
/**
52+
* Get total count.
53+
*
54+
* @return int
55+
*/
56+
public function getTotalCount();
57+
58+
/**
59+
* Set total count.
60+
*
61+
* @param int $totalCount
62+
* @return $this
63+
*/
64+
public function setTotalCount($totalCount);
3165
}

app/code/Magento/Quote/Model/QuoteRepository.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ class QuoteRepository implements \Magento\Quote\Api\CartRepositoryInterface
4141
protected $quoteCollection;
4242

4343
/**
44-
* @var \Magento\Quote\Api\Data\CartSearchResultsDataBuilder
44+
* @var \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory
4545
*/
46-
protected $searchResultsBuilder;
46+
protected $searchResultsDataFactory;
4747

4848
/**
4949
* @param QuoteFactory $quoteFactory
5050
* @param StoreManagerInterface $storeManager
5151
* @param \Magento\Quote\Model\Resource\Quote\Collection $quoteCollection
52-
* @param \Magento\Quote\Api\Data\CartSearchResultsDataBuilder $searchResultsBuilder
52+
* @param \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory
5353
*/
5454
public function __construct(
5555
QuoteFactory $quoteFactory,
5656
StoreManagerInterface $storeManager,
5757
\Magento\Quote\Model\Resource\Quote\Collection $quoteCollection,
58-
\Magento\Quote\Api\Data\CartSearchResultsDataBuilder $searchResultsBuilder
58+
\Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory
5959
) {
6060
$this->quoteFactory = $quoteFactory;
6161
$this->storeManager = $storeManager;
62-
$this->searchResultsBuilder = $searchResultsBuilder;
62+
$this->searchResultsDataFactory = $searchResultsDataFactory;
6363
$this->quoteCollection = $quoteCollection;
6464
}
6565

@@ -201,13 +201,14 @@ protected function loadQuote($loadMethod, $loadField, $identifier, array $shared
201201
*/
202202
public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria)
203203
{
204-
$this->searchResultsBuilder->setSearchCriteria($searchCriteria);
204+
$searchData = $this->searchResultsDataFactory->create();
205+
$searchData->setSearchCriteria($searchCriteria);
205206

206207
foreach ($searchCriteria->getFilterGroups() as $group) {
207208
$this->addFilterGroupToCollection($group, $this->quoteCollection);
208209
}
209210

210-
$this->searchResultsBuilder->setTotalCount($this->quoteCollection->getSize());
211+
$searchData->setTotalCount($this->quoteCollection->getSize());
211212
$sortOrders = $searchCriteria->getSortOrders();
212213
if ($sortOrders) {
213214
foreach ($sortOrders as $sortOrder) {
@@ -220,9 +221,9 @@ public function getList(\Magento\Framework\Api\SearchCriteria $searchCriteria)
220221
$this->quoteCollection->setCurPage($searchCriteria->getCurrentPage());
221222
$this->quoteCollection->setPageSize($searchCriteria->getPageSize());
222223

223-
$this->searchResultsBuilder->setItems($this->quoteCollection->getItems());
224+
$searchData->setItems($this->quoteCollection->getItems());
224225

225-
return $this->searchResultsBuilder->create();
226+
return $searchData;
226227
}
227228

228229
/**

dev/tests/unit/testsuite/Magento/Quote/Model/QuoteRepositoryTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class QuoteRepositoryTest extends \PHPUnit_Framework_TestCase
3939
/**
4040
* @var \PHPUnit_Framework_MockObject_MockObject
4141
*/
42-
protected $searchResultsBuilderMock;
42+
protected $searchResultsDataFactory;
4343

4444
/**
4545
* @var \PHPUnit_Framework_MockObject_MockObject
@@ -61,8 +61,8 @@ protected function setUp()
6161
false
6262
);
6363
$this->storeMock = $this->getMock('\Magento\Store\Model\Store', [], [], '', false);
64-
$this->searchResultsBuilderMock = $this->getMock(
65-
'\Magento\Quote\Api\Data\CartSearchResultsDataBuilder',
64+
$this->searchResultsDataFactory = $this->getMock(
65+
'\Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory',
6666
['setSearchCriteria', 'setTotalCount', 'setItems', 'create'],
6767
[],
6868
'',
@@ -76,7 +76,7 @@ protected function setUp()
7676
[
7777
'quoteFactory' => $this->quoteFactoryMock,
7878
'storeManager' => $this->storeManagerMock,
79-
'searchResultsBuilder' => $this->searchResultsBuilderMock,
79+
'searchResultsDataFactory' => $this->searchResultsDataFactory,
8080
'quoteCollection' => $this->quoteCollectionMock,
8181
]
8282
);
@@ -316,7 +316,12 @@ public function testGetListSuccess($direction, $expectedDirection)
316316
$filterMock = $this->getMock('\Magento\Framework\Api\Filter', [], [], '', false);
317317
$pageSize = 10;
318318

319-
$this->searchResultsBuilderMock
319+
$this->searchResultsDataFactory
320+
->expects($this->once())
321+
->method('create')
322+
->will($this->returnValue($searchResult));
323+
324+
$searchResult
320325
->expects($this->once())
321326
->method('setSearchCriteria');
322327

@@ -334,7 +339,7 @@ public function testGetListSuccess($direction, $expectedDirection)
334339

335340
//back in getList()
336341
$this->quoteCollectionMock->expects($this->once())->method('getSize')->willReturn($pageSize);
337-
$this->searchResultsBuilderMock->expects($this->once())->method('setTotalCount')->with($pageSize);
342+
$searchResult->expects($this->once())->method('setTotalCount')->with($pageSize);
338343
$sortOrderMock = $this->getMockBuilder('Magento\Framework\Api\SortOrder')
339344
->setMethods(['getField', 'getDirection'])
340345
->disableOriginalConstructor()
@@ -357,11 +362,8 @@ public function testGetListSuccess($direction, $expectedDirection)
357362

358363

359364
$this->quoteCollectionMock->expects($this->once())->method('getItems')->willReturn([$cartMock]);
360-
$this->searchResultsBuilderMock->expects($this->once())->method('setItems')->with([$cartMock]);
361-
$this->searchResultsBuilderMock
362-
->expects($this->once())
363-
->method('create')
364-
->will($this->returnValue($searchResult));
365+
$searchResult->expects($this->once())->method('setItems')->with([$cartMock]);
366+
365367
$this->assertEquals($searchResult, $this->model->getList($searchCriteriaMock));
366368
}
367369

0 commit comments

Comments
 (0)