Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdapterTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Elasticsearch\SearchAdapter\QueryContainerFactory;
12 
16 class AdapterTest extends \PHPUnit\Framework\TestCase
17 {
21  private $queryContainerFactory;
22 
26  protected $model;
27 
31  protected $connectionManager;
32 
36  protected $mapper;
37 
41  protected $responseFactory;
42 
46  protected $request;
47 
52 
57  protected function setUp()
58  {
59  $this->connectionManager = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ConnectionManager::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62 
63  $this->mapper = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Mapper::class)
64  ->setMethods([
65  'buildQuery',
66  ])
67  ->disableOriginalConstructor()
68  ->getMock();
69 
70  $this->responseFactory = $this->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\ResponseFactory::class)
71  ->setMethods(['create'])
72  ->disableOriginalConstructor()
73  ->getMock();
74 
75  $this->request = $this->getMockBuilder(\Magento\Framework\Search\RequestInterface::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78 
79  $this->aggregationBuilder = $this->getMockBuilder(
80  \Magento\Elasticsearch\SearchAdapter\Aggregation\Builder::class
81  )
82  ->setMethods([
83  'build',
84  'setQuery'
85  ])
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->queryContainerFactory = $this->getMockBuilder(QueryContainerFactory::class)
90  ->setMethods(['create'])
91  ->disableOriginalConstructor()
92  ->getMock();
93 
94  $objectManager = new ObjectManagerHelper($this);
95  $this->model = $objectManager->getObject(
96  \Magento\Elasticsearch\SearchAdapter\Adapter::class,
97  [
98  'connectionManager' => $this->connectionManager,
99  'mapper' => $this->mapper,
100  'responseFactory' => $this->responseFactory,
101  'aggregationBuilder' => $this->aggregationBuilder,
102  'queryContainerFactory' => $this->queryContainerFactory,
103  ]
104  );
105  }
106 
112  public function testQuery()
113  {
114  $searchQuery = [
115  'index' => 'indexName',
116  'type' => 'product',
117  'body' => [
118  'from' => 0,
119  'size' => 1000,
120  'fields' => ['_id', '_score'],
121  'query' => [],
122  ],
123  ];
124 
125  $client = $this->getMockBuilder(\Magento\Elasticsearch\Model\Client\Elasticsearch::class)
126  ->setMethods(['query'])
127  ->disableOriginalConstructor()
128  ->getMock();
129 
130  $this->connectionManager->expects($this->once())
131  ->method('getConnection')
132  ->willReturn($client);
133 
134  $queryContainer = $this->getMockBuilder(QueryContainer::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137 
138  $this->queryContainerFactory->expects($this->once())
139  ->method('create')
140  ->with(['query' => $searchQuery])
141  ->willReturn($queryContainer);
142 
143  $this->aggregationBuilder->expects($this->once())
144  ->method('setQuery')
145  ->with($queryContainer);
146 
147  $this->mapper->expects($this->once())
148  ->method('buildQuery')
149  ->with($this->request)
150  ->willReturn($searchQuery);
151 
152  $client->expects($this->once())
153  ->method('query')
154  ->willReturn([
155  'hits' => [
156  'total' => 1,
157  'hits' => [
158  [
159  '_index' => 'indexName',
160  '_type' => 'product',
161  '_id' => 1,
162  '_score' => 1.0,
163  ],
164  ],
165  ],
166  ]);
167  $this->aggregationBuilder->expects($this->once())
168  ->method('build')
169  ->willReturn($client);
170 
171  $this->model->query($this->request);
172  }
173 }
$objectManager
Definition: bootstrap.php:17