MatchTest.php 5.13 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Elasticsearch\Test\Unit\SearchAdapter\Query\Builder;

use Magento\Elasticsearch\Model\Adapter\FieldMapperInterface;
use Magento\Elasticsearch\SearchAdapter\Query\Builder\Match as MatchQueryBuilder;
use Magento\Framework\Search\Request\Query\Match as MatchRequestQuery;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class MatchTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var MatchQueryBuilder
     */
    private $matchQueryBuilder;

    /**
     * @inheritdoc
     */
    protected function setUp()
    {
        $this->matchQueryBuilder = (new ObjectManager($this))->getObject(
            MatchQueryBuilder::class,
            [
                'fieldMapper' => $this->getFieldMapper(),
                'preprocessorContainer' => [],
            ]
        );
    }

    /**
     * Tests that method constructs a correct select query.
     * @see MatchQueryBuilder::build
     *
     * @dataProvider queryValuesInvariantsProvider
     *
     * @param string $rawQueryValue
     * @param string $errorMessage
     */
    public function testBuild($rawQueryValue, $errorMessage)
    {
        $this->assertSelectQuery(
            $this->matchQueryBuilder->build([], $this->getMatchRequestQuery($rawQueryValue), 'not'),
            $errorMessage
        );
    }

    /**
     * @link https://dev.mysql.com/doc/refman/5.7/en/fulltext-boolean.html Fulltext-boolean search docs.
     *
     * @return array
     */
    public function queryValuesInvariantsProvider()
    {
        return [
            ['query_value', 'Select query field must match simple raw query value.'],
            ['query_value+', 'Specifying a trailing plus sign causes InnoDB to report a syntax error.'],
            ['query_value-', 'Specifying a trailing minus sign causes InnoDB to report a syntax error.'],
            ['query_@value', 'The @ symbol is reserved for use by the @distance proximity search operator.'],
            ['query_value+@', 'The @ symbol is reserved for use by the @distance proximity search operator.'],
        ];
    }

    /**
     * Tests that method constructs a correct "match" query depending on query value.
     *
     * @dataProvider matchProvider
     *
     * @param string $rawQueryValue
     * @param string $queryValue
     * @param string $match
     */
    public function testBuildMatchQuery($rawQueryValue, $queryValue, $match)
    {
        $query = $this->matchQueryBuilder->build([], $this->getMatchRequestQuery($rawQueryValue), 'should');

        $expectedSelectQuery = [
            'bool' => [
                'should' => [
                    [
                        $match => [
                            'some_field' => [
                                'query' => $queryValue,
                                'boost' => 43,
                            ],
                        ],
                    ],
                ],
            ],
        ];

        $this->assertEquals(
            $expectedSelectQuery,
            $query,
            sprintf('Wrong "match" query. Should be processed with "%s"', $match)
        );
    }

    /**
     * @return array
     */
    public function matchProvider()
    {
        return [
            ['query_value', 'query_value', 'match'],
            ['"query value"', 'query value', 'match_phrase'],
        ];
    }

    /**
     * @param array $selectQuery
     * @param string $errorMessage
     */
    private function assertSelectQuery($selectQuery, $errorMessage)
    {
        $expectedSelectQuery = [
            'bool' => [
                'must_not' => [
                    [
                        'match' => [
                            'some_field' => [
                                'query' => 'query_value',
                                'boost' => 43,
                            ],
                        ],
                    ],
                ],
            ],
        ];

        $this->assertEquals($expectedSelectQuery, $selectQuery, $errorMessage);
    }

    /**
     * Gets fieldMapper mock object.
     *
     * @return FieldMapperInterface|MockObject
     */
    private function getFieldMapper()
    {
        $fieldMapper = $this->getMockBuilder(FieldMapperInterface::class)
            ->getMockForAbstractClass();

        $fieldMapper->method('getFieldName')
            ->with('some_field', ['type' => FieldMapperInterface::TYPE_QUERY])
            ->willReturnArgument(0);

        return $fieldMapper;
    }

    /**
     * Gets RequestQuery mock object.
     *
     * @param string $rawQueryValue
     * @return MatchRequestQuery|MockObject
     */
    private function getMatchRequestQuery($rawQueryValue)
    {
        $matchRequestQuery = $this->getMockBuilder(MatchRequestQuery::class)
            ->disableOriginalConstructor()
            ->getMock();

        $matchRequestQuery->method('getValue')
            ->willReturn($rawQueryValue);
        $matchRequestQuery->method('getMatches')
            ->willReturn([['field' => 'some_field', 'boost' => 42]]);

        return $matchRequestQuery;
    }
}