public function testQuery()
 {
     $selectResult = ['documents' => [['product_id' => 1, 'sku' => 'Product']], 'aggregations' => ['aggregation_name' => ['aggregation1' => [1, 3], 'aggregation2' => [2, 4]]]];
     $select = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->disableOriginalConstructor()->getMock();
     $this->connectionAdapter->expects($this->once())->method('select')->willReturn($select);
     $table = $this->getMockBuilder('Magento\\Framework\\DB\\Ddl\\Table')->disableOriginalConstructor()->getMock();
     $this->temporaryStorage->expects($this->any())->method('storeDocumentsFromSelect')->willReturn($table);
     $this->connectionAdapter->expects($this->any())->method('fetchAssoc')->will($this->returnValue($selectResult['documents']));
     $this->mapper->expects($this->once())->method('buildQuery')->with($this->request)->will($this->returnValue($this->select));
     $this->responseFactory->expects($this->once())->method('create')->with($selectResult)->will($this->returnArgument(0));
     $this->aggregatioBuilder->expects($this->once())->method('build')->willReturn($selectResult['aggregations']);
     $response = $this->adapter->query($this->request);
     $this->assertEquals($selectResult, $response);
 }
 /**
  * @param $query
  * @param array $derivedQueries
  * @param int $queriesCount
  * @throws \Exception
  * @dataProvider buildQueryDataProvider
  */
 public function testBuildQuery($query, array $derivedQueries = [], $queriesCount = 0)
 {
     $select = $this->createSelectMock(null, false, false);
     $this->mockBuilders($select);
     $previousSelect = $select;
     $selects = [];
     for ($i = $queriesCount; $i >= 0; $i--) {
         $isLast = $i === 0;
         $select = $this->createSelectMock($previousSelect, $isLast, true);
         $previousSelect = $select;
         $selects[] = $select;
     }
     $this->addSelects($selects);
     $this->request->expects($this->once())->method('getSize')->willReturn(self::REQUEST_LIMIT);
     $this->filterBuilder->expects($this->any())->method('build')->will($this->returnValue('(1)'));
     $table = $this->getMockBuilder('\\Magento\\Framework\\DB\\Ddl\\Table')->disableOriginalConstructor()->getMock();
     $this->temporaryStorage->expects($this->any())->method('storeDocumentsFromSelect')->willReturn($table);
     $table->expects($this->any())->method('getName')->willReturn('table_name');
     $this->queryContainer->expects($this->any())->method('getMatchQueries')->willReturn($derivedQueries);
     $this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query));
     $response = $this->mapper->buildQuery($this->request);
     $this->assertEquals(end($selects), $response);
 }