QueryTest.php 5.17 KB
Newer Older
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
<?php

namespace yiiunit\extensions\sphinx;

use yii\sphinx\Query;

/**
 * @group sphinx
 */
class QueryTest extends SphinxTestCase
{
	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;
		$query->from('tbl_user');
		$this->assertEquals(['tbl_user'], $query->from);
	}

35 36 37 38 39 40
	public function testMatch()
	{
		$query = new Query;
		$match = 'test match';
		$query->match($match);
		$this->assertEquals($match, $query->match);
41 42 43 44

		$command = $query->createCommand($this->getConnection(false));
		$this->assertContains('MATCH(', $command->getSql(), 'No MATCH operator present!');
		$this->assertContains($match, $command->params, 'No match query among params!');
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
	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);
	}

	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);
	}

	public function testOrder()
	{
		$query = new Query;
		$query->orderBy('team');
Paul Klimov committed
80
		$this->assertEquals(['team' => SORT_ASC], $query->orderBy);
81 82

		$query->addOrderBy('company');
Paul Klimov committed
83
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->orderBy);
84 85

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

Paul Klimov committed
88 89
		$query->addOrderBy(['age' => SORT_DESC]);
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->orderBy);
90 91

		$query->addOrderBy('age ASC, company DESC');
Paul Klimov committed
92
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->orderBy);
93 94 95 96 97 98 99 100 101 102 103 104 105 106
	}

	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');
Paul Klimov committed
107
		$this->assertEquals(['team' => SORT_ASC], $query->within);
108 109

		$query->addWithin('company');
Paul Klimov committed
110
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC], $query->within);
111 112

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

Paul Klimov committed
115 116
		$query->addWithin(['age' => SORT_DESC]);
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_ASC, 'age' => SORT_DESC], $query->within);
117 118

		$query->addWithin('age ASC, company DESC');
Paul Klimov committed
119
		$this->assertEquals(['team' => SORT_ASC, 'company' => SORT_DESC, 'age' => SORT_ASC], $query->within);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	}

	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')
143
			->match('about')
144 145
			->options([
				'cutoff' => 50,
146 147 148 149
				'field_weights' => [
					'title' => 10,
					'content' => 3,
				],
150 151 152 153
			])
			->all($connection);
		$this->assertNotEmpty($rows);
	}
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

	/**
	 * @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!');
		}
	}
187
}