public function testBuild()
    {

        $this->scoreBuilder->expects($this->once())->method('build')->willReturn('score condition');
        $subSelect = $this->getMockBuilder('Magento\Framework\DB\Select')
            ->disableOriginalConstructor()
            ->getMock();
        $this->indexBuilder->expects($this->once())->method('build')->willReturn($subSelect);
        $subSelect->expects($this->once())->method('columns')->with('score condition');
        $this->request->expects($this->once())->method('getSize')->willReturn(1000);
        $subSelect->expects($this->once())->method('limit')->with(1000);

        $result = $this->queryContainer->addMatchQuery($this->select, $this->query, Bool::QUERY_CONDITION_MUST);
        $this->assertEquals($this->select, $result);
    }
Esempio n. 2
0
 /**
  * @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]);
 }
Esempio n. 3
0
    /**
     * @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;
    }
Esempio n. 4
0
 /**
  * @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;
 }
Esempio n. 5
0
 /**
  * @return Select
  */
 private function createSelect()
 {
     return $this->indexBuilder->build($this->request);
 }