Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SearchResponseBuilderTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class SearchResponseBuilderTest extends \PHPUnit\Framework\TestCase
13 {
17  private $model;
18 
22  private $searchResultFactory;
23 
27  private $documentFactory;
28 
29  protected function setUp()
30  {
31  $this->searchResultFactory = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchResultFactory::class)
32  ->disableOriginalConstructor()
33  ->getMock();
34 
35  $this->documentFactory = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentFactory::class)
36  ->disableOriginalConstructor()
37  ->getMock();
38 
39  $this->model = (new ObjectManager($this))->getObject(
40  \Magento\Framework\Search\SearchResponseBuilder::class,
41  ['searchResultFactory' => $this->searchResultFactory]
42  );
43  }
44 
45  public function testBuild()
46  {
47  $aggregations = ['aggregations'];
48 
49  $document = $this->getMockBuilder(\Magento\Framework\Api\Search\DocumentInterface::class)
50  ->disableOriginalConstructor()
51  ->getMockForAbstractClass();
52 
54  $searchResult = $this->getMockBuilder(\Magento\Framework\Api\Search\SearchResultInterface::class)
55  ->disableOriginalConstructor()
56  ->getMockForAbstractClass();
57  $searchResult->expects($this->once())
58  ->method('setItems')
59  ->with([$document]);
60  $searchResult->expects($this->once())
61  ->method('setAggregations')
62  ->with($aggregations);
63 
64  $this->searchResultFactory->expects($this->once())
65  ->method('create')
66  ->willReturn($searchResult);
67 
69  $response = $this->getMockBuilder(\Magento\Framework\Search\Response\QueryResponse::class)
70  ->setMethods(['getIterator', 'getAggregations'])
71  ->disableOriginalConstructor()
72  ->getMockForAbstractClass();
73  $response->expects($this->any())
74  ->method('getIterator')
75  ->willReturn(new \ArrayIterator([$document]));
76  $response->expects($this->once())
77  ->method('getAggregations')
78  ->willReturn($aggregations);
79 
80  $result = $this->model->build($response);
81 
82  $this->assertInstanceOf(\Magento\Framework\Api\Search\SearchResultInterface::class, $result);
83  }
84 }
$response
Definition: 404.php:11