public function testBuild()
 {
     $documentId = 333;
     $fieldName = 'fieldName';
     $fieldValue = 'fieldValue';
     $aggregations = ['aggregations'];
     $document = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\DocumentInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $document->expects($this->once())->method('setCustomAttribute')->with($fieldName, $fieldValue);
     $document->expects($this->once())->method('setId')->with($documentId);
     $this->documentFactory->expects($this->once())->method('create')->willReturn($document);
     /** @var SearchResultInterface|\PHPUnit_Framework_MockObject_MockObject $searchResult */
     $searchResult = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\SearchResultInterface')->disableOriginalConstructor()->getMockForAbstractClass();
     $searchResult->expects($this->once())->method('setItems')->with([$document]);
     $searchResult->expects($this->once())->method('setAggregations')->with($aggregations);
     $this->searchResultFactory->expects($this->once())->method('create')->willReturn($searchResult);
     $field = $this->getMockBuilder('Magento\\Framework\\Search\\DocumentField')->disableOriginalConstructor()->getMock();
     $field->expects($this->once())->method('getName')->willReturn($fieldName);
     $field->expects($this->once())->method('getValue')->willReturn($fieldValue);
     $responseDocument = $this->getMockBuilder('Magento\\Framework\\Search\\Document')->disableOriginalConstructor()->getMock();
     $responseDocument->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$field]));
     $responseDocument->expects($this->once())->method('getId')->willReturn($documentId);
     /** @var QueryResponse|\PHPUnit_Framework_MockObject_MockObject $response */
     $response = $this->getMockBuilder('Magento\\Framework\\Search\\Response\\QueryResponse')->setMethods(['getIterator', 'getAggregations'])->disableOriginalConstructor()->getMockForAbstractClass();
     $response->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator([$responseDocument]));
     $response->expects($this->once())->method('getAggregations')->willReturn($aggregations);
     $result = $this->model->build($response);
     $this->assertInstanceOf('Magento\\Framework\\Api\\Search\\SearchResultInterface', $result);
 }
 /**
  * @param ResponseInterface $response
  * @return SearchResultInterface
  */
 public function build(ResponseInterface $response)
 {
     /** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
     $searchResult = $this->searchResultFactory->create();
     $documents = iterator_to_array($response);
     $searchResult->setItems($documents);
     $searchResult->setAggregations($response->getAggregations());
     $searchResult->setTotalCount(count($documents));
     return $searchResult;
 }
示例#3
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     if ($this->filters) {
         foreach ($this->filters as $attributes) {
             foreach ($attributes as $attributeCode => $attributeValue) {
                 $attributeCode = $this->getAttributeCode($attributeCode);
                 $this->addAttributeToSearch($attributeCode, $attributeValue);
             }
         }
         $searchCriteria = $this->getSearchCriteriaBuilder()->create();
         $searchCriteria->setRequestName('advanced_search_container');
         try {
             $searchResult = $this->getSearch()->search($searchCriteria);
         } catch (EmptyRequestDataException $e) {
             /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
             $searchResult = $this->searchResultFactory->create()->setItems([]);
         } catch (NonExistingRequestNameException $e) {
             $this->_logger->error($e->getMessage());
             throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
         }
         $temporaryStorage = $this->temporaryStorageFactory->create();
         $table = $temporaryStorage->storeApiDocuments($searchResult->getItems());
         $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     }
     parent::_renderFiltersBefore();
 }
 /**
  * @param ResponseInterface $response
  * @return SearchResultInterface
  */
 public function build(ResponseInterface $response)
 {
     /** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
     $searchResult = $this->searchResultFactory->create();
     /** @var \Magento\Framework\Api\Search\DocumentInterface[] $documents */
     $documents = [];
     /** @var \Magento\Framework\Search\Document $responseDocument */
     foreach ($response as $responseDocument) {
         $document = $this->documentFactory->create();
         /** @var \Magento\Framework\Search\DocumentField $field */
         foreach ($responseDocument as $field) {
             $document->setCustomAttribute($field->getName(), $field->getValue());
         }
         $document->setId($responseDocument->getId());
         $documents[] = $document;
     }
     $searchResult->setItems($documents);
     $searchResult->setAggregations($response->getAggregations());
     $searchResult->setTotalCount(count($documents));
     return $searchResult;
 }
示例#5
0
 /**
  * @inheritdoc
  */
 protected function _renderFiltersBefore()
 {
     $this->getSearchCriteriaBuilder();
     $this->getFilterBuilder();
     $this->getSearch();
     if ($this->queryText) {
         $this->filterBuilder->setField('search_term');
         $this->filterBuilder->setValue($this->queryText);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $priceRangeCalculation = $this->_scopeConfig->getValue(\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory::XML_PATH_RANGE_CALCULATION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
     if ($priceRangeCalculation) {
         $this->filterBuilder->setField('price_dynamic_algorithm');
         $this->filterBuilder->setValue($priceRangeCalculation);
         $this->searchCriteriaBuilder->addFilter($this->filterBuilder->create());
     }
     $searchCriteria = $this->searchCriteriaBuilder->create();
     $searchCriteria->setRequestName($this->searchRequestName);
     try {
         $this->searchResult = $this->getSearch()->search($searchCriteria);
     } catch (EmptyRequestDataException $e) {
         /** @var \Magento\Framework\Api\Search\SearchResultInterface $searchResult */
         $this->searchResult = $this->searchResultFactory->create()->setItems([]);
     } catch (NonExistingRequestNameException $e) {
         $this->_logger->error($e->getMessage());
         throw new LocalizedException(__('Sorry, something went wrong. You can find out more in the error log.'));
     }
     $temporaryStorage = $this->temporaryStorageFactory->create();
     $table = $temporaryStorage->storeApiDocuments($this->searchResult->getItems());
     $this->getSelect()->joinInner(['search_result' => $table->getName()], 'e.entity_id = search_result.' . TemporaryStorage::FIELD_ENTITY_ID, []);
     $this->_totalRecords = $this->searchResult->getTotalCount();
     if ($this->order && 'relevance' === $this->order['field']) {
         $this->getSelect()->order('search_result.' . TemporaryStorage::FIELD_SCORE . ' ' . $this->order['dir']);
     }
     return parent::_renderFiltersBefore();
 }