QueryTest.php 11.4 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\extensions\sphinx;

5
use yii\db\Expression;
6 7 8 9 10 11 12
use yii\sphinx\Query;

/**
 * @group sphinx
 */
class QueryTest extends SphinxTestCase
{
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    public function testSelect()
    {
        // default
        $query = new Query;
        $query->select('*');
        $this->assertEquals(['*'], $query->select);
        $this->assertNull($query->distinct);
        $this->assertEquals(null, $query->selectOption);

        $query = new Query;
        $query->select('id, name', 'something')->distinct(true);
        $this->assertEquals(['id', 'name'], $query->select);
        $this->assertTrue($query->distinct);
        $this->assertEquals('something', $query->selectOption);
    }

    public function testFrom()
    {
        $query = new Query;
32 33
        $query->from('user');
        $this->assertEquals(['user'], $query->from);
34 35 36 37 38 39 40 41 42 43 44
    }

    public function testMatch()
    {
        $query = new Query;
        $match = 'test match';
        $query->match($match);
        $this->assertEquals($match, $query->match);

        $command = $query->createCommand($this->getConnection(false));
        $this->assertContains('MATCH(', $command->getSql(), 'No MATCH operator present!');
45
        $this->assertContains($match, $command->params, 'No match query among params!');
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    }

    public function testWhere()
    {
        $query = new Query;
        $query->where('id = :id', [':id' => 1]);
        $this->assertEquals('id = :id', $query->where);
        $this->assertEquals([':id' => 1], $query->params);

        $query->andWhere('name = :name', [':name' => 'something']);
        $this->assertEquals(['and', 'id = :id', 'name = :name'], $query->where);
        $this->assertEquals([':id' => 1, ':name' => 'something'], $query->params);

        $query->orWhere('age = :age', [':age' => '30']);
        $this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->where);
        $this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->params);
    }

Carsten Brandt committed
64
    public function testFilterWhere()
65
    {
Alexander Makarov committed
66 67
        // should work with hash format
        $query = new Query;
68
        $query->filterWhere([
Alexander Makarov committed
69 70 71 72 73
            'id' => 0,
            'title' => '   ',
            'author_ids' => [],
        ]);
        $this->assertEquals(['id' => 0], $query->where);
74

Carsten Brandt committed
75
        $query->andFilterWhere(['status' => null]);
Alexander Makarov committed
76 77
        $this->assertEquals(['id' => 0], $query->where);

Carsten Brandt committed
78
        $query->orFilterWhere(['name' => '']);
Alexander Makarov committed
79
        $this->assertEquals(['id' => 0], $query->where);
80

Alexander Makarov committed
81
        // should work with operator format
82
        $query = new Query;
Alexander Makarov committed
83
        $condition = ['like', 'name', 'Alex'];
84
        $query->filterWhere($condition);
Alexander Makarov committed
85 86
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
87
        $query->andFilterWhere(['between', 'id', null, null]);
Alexander Makarov committed
88 89
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
90
        $query->orFilterWhere(['not between', 'id', null, null]);
Alexander Makarov committed
91 92
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
93
        $query->andFilterWhere(['in', 'id', []]);
Alexander Makarov committed
94 95
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
96
        $query->andFilterWhere(['not in', 'id', []]);
Alexander Makarov committed
97 98
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
99
        $query->andFilterWhere(['not in', 'id', []]);
Alexander Makarov committed
100 101
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
102
        $query->andFilterWhere(['like', 'id', '']);
Alexander Makarov committed
103 104
        $this->assertEquals($condition, $query->where);

Carsten Brandt committed
105
        $query->andFilterWhere(['or like', 'id', '']);
Alexander Makarov committed
106
        $this->assertEquals($condition, $query->where);
107

Carsten Brandt committed
108
        $query->andFilterWhere(['not like', 'id', '   ']);
Alexander Makarov committed
109
        $this->assertEquals($condition, $query->where);
110

Carsten Brandt committed
111
        $query->andFilterWhere(['or not like', 'id', null]);
Alexander Makarov committed
112
        $this->assertEquals($condition, $query->where);
113 114
    }

Carsten Brandt committed
115
    public function testFilterWhereRecursively()
116 117
    {
        $query = new Query();
118
        $query->filterWhere(['and', ['like', 'name', ''], ['like', 'title', ''], ['id' => 1], ['not', ['like', 'name', '']]]);
Qiang Xue committed
119
        $this->assertEquals(['and', ['id' => 1]], $query->where);
120 121
    }

122 123 124 125 126 127 128 129 130 131 132 133 134
    public function testGroup()
    {
        $query = new Query;
        $query->groupBy('team');
        $this->assertEquals(['team'], $query->groupBy);

        $query->addGroupBy('company');
        $this->assertEquals(['team', 'company'], $query->groupBy);

        $query->addGroupBy('age');
        $this->assertEquals(['team', 'company', 'age'], $query->groupBy);
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    public function testHaving()
    {
        $query = new Query;
        $query->having('id = :id', [':id' => 1]);
        $this->assertEquals('id = :id', $query->having);
        $this->assertEquals([':id' => 1], $query->params);

        $query->andHaving('name = :name', [':name' => 'something']);
        $this->assertEquals(['and', 'id = :id', 'name = :name'], $query->having);
        $this->assertEquals([':id' => 1, ':name' => 'something'], $query->params);

        $query->orHaving('age = :age', [':age' => '30']);
        $this->assertEquals(['or', ['and', 'id = :id', 'name = :name'], 'age = :age'], $query->having);
        $this->assertEquals([':id' => 1, ':name' => 'something', ':age' => '30'], $query->params);
    }

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    public function testOrder()
    {
        $query = new Query;
        $query->orderBy('team');
        $this->assertEquals(['team' => SORT_ASC], $query->orderBy);

        $query->addOrderBy('company');
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->orderBy);

        $query->addOrderBy('age');
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->orderBy);

        $query->addOrderBy(['age' => SORT_DESC]);
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->orderBy);

        $query->addOrderBy('age ASC, company DESC');
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->orderBy);
    }

    public function testLimitOffset()
    {
        $query = new Query;
        $query->limit(10)->offset(5);
        $this->assertEquals(10, $query->limit);
        $this->assertEquals(5, $query->offset);
    }

    public function testWithin()
    {
        $query = new Query;
        $query->within('team');
        $this->assertEquals(['team' => SORT_ASC], $query->within);

        $query->addWithin('company');
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->within);

        $query->addWithin('age');
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_ASC], $query->within);

        $query->addWithin(['age' => SORT_DESC]);
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->within);

        $query->addWithin('age ASC, company DESC');
        $this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->within);
    }

    public function testOptions()
    {
        $query = new Query;
        $options = [
            'cutoff' => 50,
            'max_matches' => 50,
        ];
        $query->options($options);
        $this->assertEquals($options, $query->options);

        $newMaxMatches = $options['max_matches'] + 10;
        $query->addOptions(['max_matches' => $newMaxMatches]);
        $this->assertEquals($newMaxMatches, $query->options['max_matches']);
    }

    public function testRun()
    {
        $connection = $this->getConnection();

        $query = new Query;
        $rows = $query->from('yii2_test_article_index')
            ->match('about')
            ->options([
                'cutoff' => 50,
                'field_weights' => [
                    'title' => 10,
                    'content' => 3,
                ],
            ])
            ->all($connection);
        $this->assertNotEmpty($rows);
    }

    /**
     * @depends testRun
     */
    public function testSnippet()
    {
        $connection = $this->getConnection();

        $match = 'about';
        $snippetPrefix = 'snippet#';
        $snippetCallback = function () use ($match, $snippetPrefix) {
            return [
                $snippetPrefix . '1: ' . $match,
                $snippetPrefix . '2: ' . $match,
            ];
        };
        $snippetOptions = [
            'before_match' => '[',
            'after_match' => ']',
        ];

        $query = new Query;
        $rows = $query->from('yii2_test_article_index')
            ->match($match)
            ->snippetCallback($snippetCallback)
            ->snippetOptions($snippetOptions)
            ->all($connection);
        $this->assertNotEmpty($rows);
        foreach ($rows as $row) {
            $this->assertContains($snippetPrefix, $row['snippet'], 'Snippet source not present!');
            $this->assertContains($snippetOptions['before_match'] . $match, $row['snippet'] . $snippetOptions['after_match'], 'Options not applied!');
        }
    }

    public function testCount()
    {
        $connection = $this->getConnection();

        $query = new Query;
        $count = $query->from('yii2_test_article_index')
            ->match('about')
            ->count('*', $connection);
        $this->assertEquals(2, $count);
    }
273 274 275 276

    /**
     * @depends testRun
     */
277
    public function testWhereSpecialCharValue()
278 279 280 281 282 283 284 285 286 287 288
    {
        $connection = $this->getConnection();

        $query = new Query;
        $rows = $query->from('yii2_test_article_index')
            ->andWhere(['author_id' => 'some"'])
            ->all($connection);
        $this->assertEmpty($rows);
    }

    /**
289 290 291 292 293 294 295 296 297 298 299
     * Data provider for [[testMatchSpecialCharValue()]]
     * @return array test data
     */
    public function dataProviderMatchSpecialCharValue()
    {
        return [
            ["'"],
            ['"'],
            ['@'],
            ['\\'],
            ['()'],
300 301
            ['<<<'],
            ['>>>'],
302 303 304
            ["\x00"],
            ["\n"],
            ["\r"],
305 306 307
            ["\x1a"],
            ['\\' . "'"],
            ['\\' . '"'],
308 309 310 311 312
        ];
    }

    /**
     * @dataProvider dataProviderMatchSpecialCharValue
313 314
     * @depends testRun
     *
315 316
     * @param string $char char to be tested
     *
317 318
     * @see https://github.com/yiisoft/yii2/issues/3668
     */
319
    public function testMatchSpecialCharValue($char)
320 321 322 323 324
    {
        $connection = $this->getConnection();

        $query = new Query;
        $rows = $query->from('yii2_test_article_index')
325
            ->match('about' . $char)
326
            ->all($connection);
327
        $this->assertTrue(is_array($rows)); // no query error
328
    }
329 330 331 332 333 334 335 336 337 338

    /**
     * @depends testMatchSpecialCharValue
     */
    public function testMatchComplex()
    {
        $connection = $this->getConnection();

        $query = new Query;
        $rows = $query->from('yii2_test_article_index')
339
            ->match(new Expression(':match', ['match' => '@(content) ' . $connection->escapeMatchValue('about\\"')]))
340 341 342
            ->all($connection);
        $this->assertNotEmpty($rows);
    }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

    /**
     * @depends testRun
     *
     * @see https://github.com/yiisoft/yii2/issues/4375
     */
    public function testRunOnDistributedIndex()
    {
        $connection = $this->getConnection();

        $query = new Query;
        $rows = $query->from('yii2_test_distributed')
            ->match('about')
            ->options([
                'cutoff' => 50,
                'field_weights' => [
                    'title' => 10,
                    'content' => 3,
                ],
            ])
            ->all($connection);
        $this->assertNotEmpty($rows);
    }
AlexGx committed
366
}