public function testBuildQuery() { $boost = 3.14; /** @var Select|\PHPUnit_Framework_MockObject_MockObject $select */ $select = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->setMethods(['getMatchQuery', 'match'])->disableOriginalConstructor()->getMock(); $select->expects($this->once())->method('getMatchQuery')->with($this->equalTo('some_field'), $this->equalTo('-some_value'))->will($this->returnValue('matchedQuery')); $select->expects($this->once())->method('match')->with($this->equalTo('some_field'), $this->equalTo('-some_value'), $this->equalTo(true), $this->equalTo(Select::FULLTEXT_MODE_BOOLEAN)); /** @var \Magento\Framework\Search\Request\Query\Match|\PHPUnit_Framework_MockObject_MockObject $query */ $query = $this->getMockBuilder('Magento\\Framework\\Search\\Request\\Query\\Match')->setMethods(['getMatches'])->disableOriginalConstructor()->getMock(); $query->expects($this->once())->method('getMatches')->will($this->returnValue([['field' => 'some_field', 'value' => 'some_value', 'boost' => $boost]])); $this->scoreBuilder->expects($this->once())->method('addCondition')->with($this->equalTo('matchedQuery'), $this->equalTo($boost)); $result = $this->match->build($this->scoreBuilder, $select, $query, Bool::QUERY_CONDITION_NOT); $this->assertEquals($select, $result); }
/** * @param RequestInterface $request * @param MatchContainer[] $matchQueries * @param ScoreBuilder $scoreBuilder * @param Select $select * @param IndexBuilderInterface $indexBuilder * @return Select * @internal param QueryContainer $queryContainer */ private function addMatchQueries(RequestInterface $request, array $matchQueries, ScoreBuilder $scoreBuilder, Select $select, IndexBuilderInterface $indexBuilder) { if (!$matchQueries) { $select->columns($scoreBuilder->build()); $select = $this->createAroundSelect($select, $scoreBuilder); } elseif (count($matchQueries) === 1) { $matchContainer = reset($matchQueries); $this->matchBuilder->build($scoreBuilder, $select, $matchContainer->getRequest(), $matchContainer->getConditionType()); $select->columns($scoreBuilder->build()); $select = $this->createAroundSelect($select, $scoreBuilder); } elseif (count($matchQueries) > 1) { $select->columns($scoreBuilder->build()); $select = $this->createAroundSelect($select, $scoreBuilder); $subSelect = $select; $select = $this->resource->getConnection(Resource::DEFAULT_READ_RESOURCE)->select(); $tables = array_merge(array_keys($matchQueries), ['main_select.relevance']); $relevance = implode('.relevance + ', $tables); $select->from(['main_select' => $subSelect], [$this->entityMetadata->getEntityId() => 'entity_id', 'relevance' => sprintf('(%s)', $relevance)]); foreach ($matchQueries as $matchName => $matchContainer) { $matchSelect = $indexBuilder->build($request); $matchScoreBuilder = $this->scoreBuilderFactory->create(); $matchSelect = $this->matchBuilder->build($matchScoreBuilder, $matchSelect, $matchContainer->getRequest(), $matchContainer->getConditionType()); $matchSelect->columns($matchScoreBuilder->build()); $select->join([$matchName => $this->createAroundSelect($matchSelect, $scoreBuilder)], $matchName . '.entity_id = main_select.entity_id', []); } } return $select; }
public function testBuildQuery() { $boost = 3.14; /** @var Select|\PHPUnit_Framework_MockObject_MockObject $select */ $select = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->setMethods(['getMatchQuery', 'match'])->disableOriginalConstructor()->getMock(); $this->fulltextHelper->expects($this->once())->method('getMatchQuery')->with($this->equalTo(['some_field']), $this->equalTo('-some_value*'))->will($this->returnValue('matchedQuery')); $this->fulltextHelper->expects($this->once())->method('match')->withConsecutive([$select, ['some_field'], '-some_value*', true, Fulltext::FULLTEXT_MODE_BOOLEAN])->willReturn($select); $this->resolver->expects($this->once())->method('resolve')->willReturnArgument(0); /** @var \Magento\Framework\Search\Request\Query\Match|\PHPUnit_Framework_MockObject_MockObject $query */ $query = $this->getMockBuilder('Magento\\Framework\\Search\\Request\\Query\\Match')->setMethods(['getMatches', 'getValue', 'getBoost'])->disableOriginalConstructor()->getMock(); $query->expects($this->once())->method('getValue')->willReturn('some_value '); $query->expects($this->once())->method('getBoost')->willReturn($boost); $query->expects($this->once())->method('getMatches')->willReturn([['field' => 'some_field']]); $this->scoreBuilder->expects($this->once())->method('addCondition'); $result = $this->match->build($this->scoreBuilder, $select, $query, Bool::QUERY_CONDITION_NOT); $this->assertEquals($select, $result); }
/** * @param ScoreBuilder $scoreBuilder * @param Select $select * @param RequestQueryInterface $query * @param string $conditionType * @return Select */ private function buildMatchQuery( ScoreBuilder $scoreBuilder, Select $select, RequestQueryInterface $query, $conditionType ) { $select = $this->matchBuilder->build($scoreBuilder, $select, $query, $conditionType); return $select; }
public function testBuildQuery() { /** @var Select|\PHPUnit_Framework_MockObject_MockObject $select */ $select = $this->getMockBuilder('Magento\Framework\DB\Select') ->setMethods(['getMatchQuery', 'match', 'where']) ->disableOriginalConstructor() ->getMock(); $this->fulltextHelper->expects($this->once()) ->method('getMatchQuery') ->with($this->equalTo(['some_field' => 'some_field']), $this->equalTo('-some_value*')) ->will($this->returnValue('matchedQuery')); $select->expects($this->once()) ->method('where') ->with('matchedQuery') ->willReturnSelf(); $this->resolver->expects($this->once()) ->method('resolve') ->willReturnCallback(function ($fieldList) { $resolvedFields = []; foreach ($fieldList as $column) { $field = $this->getMockBuilder('\Magento\Framework\Search\Adapter\Mysql\Field\FieldInterface') ->disableOriginalConstructor() ->getMockForAbstractClass(); $field->expects($this->any()) ->method('getColumn') ->willReturn($column); $resolvedFields[] = $field; } return $resolvedFields; }); /** @var \Magento\Framework\Search\Request\Query\Match|\PHPUnit_Framework_MockObject_MockObject $query */ $query = $this->getMockBuilder('Magento\Framework\Search\Request\Query\Match') ->setMethods(['getMatches', 'getValue']) ->disableOriginalConstructor() ->getMock(); $query->expects($this->once()) ->method('getValue') ->willReturn('some_value '); $query->expects($this->once()) ->method('getMatches') ->willReturn([['field' => 'some_field']]); $this->scoreBuilder->expects($this->once()) ->method('addCondition'); $result = $this->match->build($this->scoreBuilder, $select, $query, Bool::QUERY_CONDITION_NOT); $this->assertEquals($select, $result); }
/** * @param RequestInterface $request * @param Select $select * @param IndexBuilderInterface $indexBuilder * @param MatchContainer[] $matchQueries * @return Select */ private function addMatchQueries(RequestInterface $request, Select $select, IndexBuilderInterface $indexBuilder, array $matchQueries) { $queriesCount = count($matchQueries); if ($queriesCount) { $table = $this->temporaryStorage->storeDocumentsFromSelect($select); foreach ($matchQueries as $matchContainer) { $queriesCount--; $matchScoreBuilder = $this->scoreBuilderFactory->create(); $matchSelect = $this->matchBuilder->build($matchScoreBuilder, $indexBuilder->build($request), $matchContainer->getRequest(), $matchContainer->getConditionType()); $select = $this->joinPreviousResultToSelect($matchSelect, $table, $matchScoreBuilder); if ($queriesCount) { $previousResultTable = $table; $table = $this->temporaryStorage->storeDocumentsFromSelect($select); $this->getConnection()->dropTable($previousResultTable->getName()); } } } return $select; }
/** * Process query * * @param ScoreBuilder $scoreBuilder * @param RequestQueryInterface $query * @param Select $select * @param string $conditionType * @return Select * @throws \InvalidArgumentException */ protected function processQuery(ScoreBuilder $scoreBuilder, RequestQueryInterface $query, Select $select, $conditionType) { switch ($query->getType()) { case RequestQueryInterface::TYPE_MATCH: /** @var MatchQuery $query */ $select = $this->matchQueryBuilder->build($scoreBuilder, $select, $query, $conditionType); break; case RequestQueryInterface::TYPE_BOOL: /** @var BoolQuery $query */ $select = $this->processBoolQuery($scoreBuilder, $query, $select); break; case RequestQueryInterface::TYPE_FILTER: /** @var FilterQuery $query */ $select = $this->processFilterQuery($scoreBuilder, $query, $select, $conditionType); break; default: throw new \InvalidArgumentException(sprintf('Unknown query type \'%s\'', $query->getType())); } return $select; }