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 testBuildMatchQuery() { $query = $this->createMatchQuery(); $this->request->expects($this->once()) ->method('getDimensions') ->will( $this->returnValue([$this->createDimension()]) ); $this->dimensionsBuilder->expects($this->any()) ->method('build') ->will($this->returnValue('a = b')); $this->queryContainer->expects($this->any())->method('addMatchQuery') ->with( $this->equalTo($this->select), $this->equalTo($query), $this->equalTo(Bool::QUERY_CONDITION_MUST) ) ->will($this->returnValue($this->select)); $this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query)); $this->select->expects($this->once())->method('columns')->will($this->returnValue($this->select)); $response = $this->mapper->buildQuery($this->request); $this->assertEquals($this->select, $response); }
protected function setUp() { $helper = new ObjectManager($this); $this->select = $this->getMockBuilder('Magento\Framework\DB\Select') ->disableOriginalConstructor() ->getMock(); $this->scoreBuilder = $this->getMockBuilder('Magento\Framework\Search\Adapter\Mysql\ScoreBuilder') ->disableOriginalConstructor() ->getMock(); $this->scoreBuilderFactory = $this->getMockBuilder('Magento\Framework\Search\Adapter\Mysql\ScoreBuilderFactory') ->setMethods(['create']) ->disableOriginalConstructor() ->getMock(); $this->scoreBuilderFactory->expects($this->any())->method('create')->willReturn($this->scoreBuilder); $this->query = $this->getMockBuilder('Magento\Framework\Search\Request\QueryInterface') ->disableOriginalConstructor() ->getMock(); $this->matchBuilder = $this->getMockBuilder('Magento\Framework\Search\Adapter\Mysql\Query\Builder\Match') ->setMethods(['build']) ->disableOriginalConstructor() ->getMock(); $this->matchBuilder->expects($this->any())->method('build')->willReturnArgument(1); $this->indexBuilder = $this->getMockBuilder('Magento\Framework\Search\Adapter\Mysql\IndexBuilderInterface') ->setMethods(['build']) ->disableOriginalConstructor() ->getMock(); $this->request = $this->getMockBuilder('\Magento\Framework\Search\RequestInterface') ->disableOriginalConstructor() ->getMock(); $this->queryContainer = $helper->getObject( 'Magento\Framework\Search\Adapter\Mysql\Query\QueryContainer', [ 'scoreBuilderFactory' => $this->scoreBuilderFactory, 'matchBuilder' => $this->matchBuilder, 'indexBuilder' => $this->indexBuilder, 'request' => $this->request ] ); }
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 $query * @throws \Exception * @dataProvider buildQueryDataProvider */ public function testBuildQuery($query, $derivedQueries = []) { $this->filterBuilder->expects($this->any())->method('build')->will($this->returnValue('(1)')); $this->queryContainer->expects($this->any())->method('getDerivedQueries')->willReturn($derivedQueries); $this->select->expects($this->any())->method('columns')->will($this->returnValue($this->select)); $this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query)); $response = $this->mapper->buildQuery($this->request); $this->assertEquals($this->select, $response); }
/** * @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; }
/** * @param ResolverInterface $resolver * @param Fulltext $fulltextHelper * @param DbHelper $dbHelper * @param QueryHelper $queryHelper * @param string $fulltextSearchMode */ public function __construct(ResolverInterface $resolver, Fulltext $fulltextHelper, DbHelper $dbHelper, QueryHelper $queryHelper, $fulltextSearchMode = Fulltext::FULLTEXT_MODE_BOOLEAN) { $this->resolver = $resolver; $this->replaceSymbols = str_split(self::SPECIAL_CHARACTERS, 1); $this->fulltextHelper = $fulltextHelper; $this->queryHelper = $queryHelper; $this->dbHelper = $dbHelper; $this->fulltextSearchMode = $fulltextSearchMode; parent::__construct($resolver, $fulltextHelper, $fulltextSearchMode); }
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; }
/** * @param Select $select */ private function mockBuilders(Select $select) { $helper = new ObjectManager($this); $this->scoreBuilder = $this->getMockBuilder('Magento\\Framework\\Search\\Adapter\\Mysql\\ScoreBuilder')->setMethods(['clear'])->disableOriginalConstructor()->getMock(); $this->scoreBuilderFactory = $this->getMockBuilder('Magento\\Framework\\Search\\Adapter\\Mysql\\ScoreBuilderFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $this->scoreBuilderFactory->expects($this->any())->method('create')->will($this->returnValue($this->scoreBuilder)); $this->filterBuilder = $this->getMockBuilder('Magento\\Framework\\Search\\Adapter\\Mysql\\Filter\\Builder')->setMethods(['build'])->disableOriginalConstructor()->getMock(); $this->matchBuilder = $this->getMockBuilder('\\Magento\\Framework\\Search\\Adapter\\Mysql\\Query\\Builder\\Match')->setMethods(['build'])->disableOriginalConstructor()->getMock(); $this->matchBuilder->expects($this->any())->method('build')->willReturnArgument(1); $this->indexBuilder = $this->getMockBuilder('\\Magento\\Framework\\Search\\Adapter\\Mysql\\IndexBuilderInterface')->disableOriginalConstructor()->setMethods(['build'])->getMockForAbstractClass(); $this->indexBuilder->expects($this->any())->method('build')->will($this->returnValue($select)); $temporaryStorageFactory = $this->getMockBuilder('\\Magento\\Framework\\Search\\Adapter\\Mysql\\TemporaryStorageFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $temporaryStorageFactory->expects($this->any())->method('create')->willReturn($this->temporaryStorage); $queryContainerFactory = $this->getMockBuilder('Magento\\Framework\\Search\\Adapter\\Mysql\\Query\\QueryContainerFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $queryContainerFactory->expects($this->any())->method('create')->willReturn($this->queryContainer); $entityMetadata = $this->getMockBuilder('\\Magento\\Framework\\Search\\EntityMetadata')->setMethods(['getEntityId'])->disableOriginalConstructor()->getMock(); $entityMetadata->expects($this->any())->method('getEntityId')->willReturn(self::METADATA_ENTITY_ID); $this->mapper = $helper->getObject('Magento\\Framework\\Search\\Adapter\\Mysql\\Mapper', ['resource' => $this->resource, 'scoreBuilderFactory' => $this->scoreBuilderFactory, 'queryContainerFactory' => $queryContainerFactory, 'filterBuilder' => $this->filterBuilder, 'matchBuilder' => $this->matchBuilder, 'indexProviders' => [self::INDEX_NAME => $this->indexBuilder], 'temporaryStorageFactory' => $temporaryStorageFactory, 'entityMetadata' => $entityMetadata]); }
/** * 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; }
public function testBuildBoolQuery() { $query = $this->createBoolQuery(); $this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query)); $this->matchQueryBuilder->expects($this->exactly(4))->method('build')->will($this->returnValue($this->select)); $matchQuery = $this->createMatchQuery(); $filterMatchQuery = $this->createFilterQuery(); $filterMatchQuery->expects($this->once())->method('getReferenceType')->will($this->returnValue(Filter::REFERENCE_QUERY)); $filterMatchQuery->expects($this->once())->method('getReference')->will($this->returnValue($matchQuery)); $filterQuery = $this->createFilterQuery(); $filterQuery->expects($this->once())->method('getReferenceType')->will($this->returnValue(Filter::REFERENCE_FILTER)); $filterQuery->expects($this->once())->method('getReference')->will($this->returnValue($this->filter)); $this->request->expects($this->once())->method('getQuery')->will($this->returnValue($query)); $this->filterBuilder->expects($this->once())->method('build')->will($this->returnValue('(1)')); $this->select->expects($this->once())->method('columns')->will($this->returnValue($this->select)); $query->expects($this->once())->method('getMust')->will($this->returnValue([$this->createMatchQuery(), $this->createFilterQuery()])); $query->expects($this->once())->method('getShould')->will($this->returnValue([$this->createMatchQuery(), $filterMatchQuery])); $query->expects($this->once())->method('getMustNot')->will($this->returnValue([$this->createMatchQuery(), $filterQuery])); $response = $this->mapper->buildQuery($this->request); $this->assertEquals($this->select, $response); }