Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SearchResultProcessorTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Data\SearchResultProcessor;
9 
13 class SearchResultProcessorTest extends \PHPUnit\Framework\TestCase
14 {
19 
24 
29 
30  protected function setUp()
31  {
32  $this->searchCriteriaMock = $this->getMockBuilder(\Magento\Framework\Api\CriteriaInterface::class)
33  ->disableOriginalConstructor()
34  ->getMock();
35 
36  $this->searchResultCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\AbstractSearchResult::class)
37  ->disableOriginalConstructor()
38  ->setMethods(['getSearchCriteria', 'getItems', 'getItemId'])
39  ->getMockForAbstractClass();
40  $this->searchResultCollectionMock->expects($this->any())
41  ->method('getSearchCriteria')
42  ->willReturn($this->searchCriteriaMock);
43  $this->searchResultProcessor = new SearchResultProcessor($this->searchResultCollectionMock);
44  }
45 
46  public function testGetCurrentPage()
47  {
48  $page = 42;
49  $this->searchCriteriaMock->expects($this->once())
50  ->method('getLimit')
51  ->willReturn([$page]);
52  $this->assertEquals($page, $this->searchResultProcessor->getCurrentPage());
53  }
54 
55  public function testGetPageSize()
56  {
57  $size = 42;
58  $this->searchCriteriaMock->expects($this->once())
59  ->method('getLimit')
60  ->willReturn([null, $size]);
61  $this->assertEquals($size, $this->searchResultProcessor->getPageSize());
62  }
63 
64  public function testGetFirstItem()
65  {
66  $itemData = ['id' => 1];
67  $itemData2 = ['id' => 2];
68 
69  $testItem = new \Magento\Framework\DataObject($itemData);
70  $testItem2 = new \Magento\Framework\DataObject($itemData2);
71 
72  $this->searchResultCollectionMock->expects($this->once())
73  ->method('getItems')
74  ->willReturn([$testItem, $testItem2]);
75 
76  $this->assertEquals($testItem, $this->searchResultProcessor->getFirstItem());
77  }
78 
79  public function testGetLastItem()
80  {
81  $itemData = ['id' => 1];
82  $itemData2 = ['id' => 2];
83 
84  $testItem = new \Magento\Framework\DataObject($itemData);
85  $testItem2 = new \Magento\Framework\DataObject($itemData2);
86 
87  $this->searchResultCollectionMock->expects($this->once())
88  ->method('getItems')
89  ->willReturn([$testItem, $testItem2]);
90 
91  $this->assertEquals($testItem2, $this->searchResultProcessor->getLastItem());
92  }
93 
94  public function testGetAllIds()
95  {
96  $itemData = ['id' => 1];
97  $ids = [1];
98 
99  $testItem = new \Magento\Framework\DataObject($itemData);
100 
101  $this->searchResultCollectionMock->expects($this->once())
102  ->method('getItems')
103  ->willReturn([$testItem]);
104  $this->searchResultCollectionMock->expects($this->once())
105  ->method('getItemId')
106  ->with($testItem)
107  ->willReturn(1);
108 
109  $this->assertEquals($ids, $this->searchResultProcessor->getAllIds());
110  }
111 
112  public function testGetItemById()
113  {
114  $itemData = ['id' => 1];
115  $itemData2 = ['id' => 2];
116 
117  $testItem = new \Magento\Framework\DataObject($itemData);
118  $testItem2 = new \Magento\Framework\DataObject($itemData2);
119 
120  $this->searchResultCollectionMock->expects($this->once())
121  ->method('getItems')
122  ->willReturn([1 => $testItem, $testItem2]);
123 
124  $this->assertEquals($testItem2, $this->searchResultProcessor->getItemById(2));
125  }
126 
127  public function testGetColumnValues()
128  {
129  $columnKey = 'columnKey';
130  $columnValue = 'columnValue';
131  $itemData = ['id' => 1, $columnKey => $columnValue];
132 
133  $testItem = new \Magento\Framework\DataObject($itemData);
134 
135  $this->searchResultCollectionMock->expects($this->once())
136  ->method('getItems')
137  ->willReturn([$testItem]);
138  $this->assertEquals([$columnValue], $this->searchResultProcessor->getColumnValues($columnKey));
139  }
140 
141  public function testGetItemsByColumnValue()
142  {
143  $columnKey = 'columnKey';
144  $columnValue = 'columnValue';
145  $itemData = ['id' => 1, $columnKey => $columnValue];
146  $itemData2 = ['id' => 2, $columnKey => $columnValue];
147 
148  $testItem = new \Magento\Framework\DataObject($itemData);
149  $testItem2 = new \Magento\Framework\DataObject($itemData2);
150 
151  $this->searchResultCollectionMock->expects($this->once())
152  ->method('getItems')
153  ->willReturn([$testItem, $testItem2]);
154 
155  $this->assertEquals(
156  [$testItem, $testItem2],
157  $this->searchResultProcessor->getItemsByColumnValue($columnKey, $columnValue)
158  );
159  }
160 
161  public function testGetItemByColumnValue()
162  {
163  $columnKey = 'columnKey';
164  $columnValue = 'columnValue';
165  $columnValue2 = 'columnValue2';
166  $itemData = ['id' => 1, $columnKey => $columnValue];
167  $itemData2 = ['id' => 2, $columnKey => $columnValue2];
168 
169  $testItem = new \Magento\Framework\DataObject($itemData);
170  $testItem2 = new \Magento\Framework\DataObject($itemData2);
171 
172  $this->searchResultCollectionMock->expects($this->once())
173  ->method('getItems')
174  ->willReturn([$testItem, $testItem2]);
175 
176  $this->assertEquals($testItem2, $this->searchResultProcessor->getItemByColumnValue($columnKey, $columnValue2));
177  }
178 }
$page
Definition: pages.php:8