|
| 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