Skip to content

Commit f10d802

Browse files
cparticaAnna Bukatar
authored andcommitted
MAGETWO-49212: [GITHUB] Magento\Sales\Model\OrderRepository::getList() is incomplete #3018
- adding unit a test for the new logic, this repository wasn't covered by unit tests
1 parent 8c99643 commit f10d802

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
* @var Metadata|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
protected $metadata;
26+
27+
/**
28+
* @var SearchResultFactory|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
protected $searchResultFactory;
31+
32+
/**
33+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
34+
*/
35+
protected $objectManager;
36+
37+
/**
38+
* Setup the test
39+
*/
40+
protected function setUp()
41+
{
42+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43+
44+
$className = 'Magento\Sales\Model\ResourceModel\Metadata';
45+
$this->metadata = $this->getMock($className, [], [], '', false);
46+
47+
$className = 'Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory';
48+
$this->searchResultFactory = $this->getMock($className, ['create'], [], '', false);
49+
50+
$this->model = $this->objectManager->getObject(
51+
'\Magento\Sales\Model\OrderRepository',
52+
[
53+
'metadata' => $this->metadata,
54+
'searchResultFactory' => $this->searchResultFactory,
55+
]
56+
);
57+
}
58+
59+
/**
60+
* TODO: Cover with unit tests the other methods in the repository
61+
* test GetList
62+
*/
63+
public function testGetList()
64+
{
65+
$fieldName = 'field';
66+
$searchCriteriaMock = $this->getMock('Magento\Framework\Api\SearchCriteria', [], [], '', false);
67+
68+
$collectionMock = $this->getMock('Magento\Sales\Model\ResourceModel\Order\Collection', [], [], '', false);
69+
70+
$filterGroupMock = $this->getMock('\Magento\Framework\Api\Search\FilterGroup', [], [], '', false);
71+
$filterGroupFilterMock = $this->getMock('\Magento\Framework\Api\Filter', [], [], '', false);
72+
$sortOrderMock = $this->getMock('\Magento\Framework\Api\SortOrder', [], [], '', false);
73+
$itemsMock = $this->getMock('Magento\Sales\Model\Order', [], [], '', false);
74+
75+
76+
$extensionAttributes = $this->getMock('\Magento\Sales\Api\Data\OrderExtension', [], [], '', false);
77+
$shippingAssignmentBuilder = $this->getMock(
78+
'\Magento\Sales\Model\Order\ShippingAssignmentBuilder',
79+
[],
80+
[],
81+
'',
82+
false
83+
);
84+
85+
$itemsMock->expects($this->once())->method('getExtensionAttributes')->willReturn($extensionAttributes);
86+
$extensionAttributes->expects($this->once())
87+
->method('getShippingAssignments')
88+
->willReturn($shippingAssignmentBuilder);
89+
90+
$this->searchResultFactory->expects($this->once())->method('create')->willReturn($collectionMock);
91+
92+
$searchCriteriaMock->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroupMock]);
93+
$filterGroupMock->expects($this->once())->method('getFilters')->willReturn([$filterGroupFilterMock]);
94+
$filterGroupFilterMock->expects($this->exactly(2))->method('getConditionType')->willReturn('eq');
95+
$filterGroupFilterMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
96+
$filterGroupFilterMock->expects($this->once())->method('getValue')->willReturn('value');
97+
$sortOrderMock->expects($this->once())->method('getDirection');
98+
$searchCriteriaMock->expects($this->once())->method('getSortOrders')->willReturn([$sortOrderMock]);
99+
$sortOrderMock->expects($this->atLeastOnce())->method('getField')->willReturn($fieldName);
100+
$collectionMock->expects($this->once())->method('addFieldToFilter')
101+
->willReturn(SortOrder::SORT_ASC);
102+
$collectionMock->expects($this->once())->method('addOrder')->with($fieldName, 'DESC');
103+
$searchCriteriaMock->expects($this->once())->method('getCurrentPage')->willReturn(4);
104+
$collectionMock->expects($this->once())->method('setCurPage')->with(4);
105+
$searchCriteriaMock->expects($this->once())->method('getPageSize')->willReturn(42);
106+
$collectionMock->expects($this->once())->method('setPageSize')->with(42);
107+
$collectionMock->expects($this->once())->method('getItems')->willReturn([$itemsMock]);
108+
109+
$this->assertEquals($collectionMock, $this->model->getList($searchCriteriaMock));
110+
}
111+
}

0 commit comments

Comments
 (0)