ActiveRecordTest.php 6.92 KB
Newer Older
1 2 3 4 5
<?php

namespace yiiunit\extensions\sphinx;

use yii\sphinx\ActiveQuery;
6 7 8
use yiiunit\data\ar\sphinx\ActiveRecord;
use yiiunit\data\ar\sphinx\ArticleIndex;
use yiiunit\data\ar\sphinx\RuntimeIndex;
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

/**
 * @group sphinx
 */
class ActiveRecordTest extends SphinxTestCase
{
	protected function setUp()
	{
		parent::setUp();
		ActiveRecord::$db = $this->getConnection();
	}

	protected function tearDown()
	{
		$this->truncateRuntimeIndex('yii2_test_rt_index');
		parent::tearDown();
	}

	// Tests :

	public function testFind()
	{
		// find one
		$result = ArticleIndex::find();
		$this->assertTrue($result instanceof ActiveQuery);
		$article = $result->one();
		$this->assertTrue($article instanceof ArticleIndex);

		// find all
		$articles = ArticleIndex::find()->all();
		$this->assertEquals(2, count($articles));
		$this->assertTrue($articles[0] instanceof ArticleIndex);
		$this->assertTrue($articles[1] instanceof ArticleIndex);

		// find fulltext
44 45 46
		$article = ArticleIndex::find(2);
		$this->assertTrue($article instanceof ArticleIndex);
		$this->assertEquals(2, $article->id);
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

		// find by column values
		$article = ArticleIndex::find(['id' => 2, 'author_id' => 2]);
		$this->assertTrue($article instanceof ArticleIndex);
		$this->assertEquals(2, $article->id);
		$this->assertEquals(2, $article->author_id);
		$article = ArticleIndex::find(['id' => 2, 'author_id' => 1]);
		$this->assertNull($article);

		// find by attributes
		$article = ArticleIndex::find()->where(['author_id' => 2])->one();
		$this->assertTrue($article instanceof ArticleIndex);
		$this->assertEquals(2, $article->id);

		// find custom column
		$article = ArticleIndex::find()->select(['*', '(5*2) AS custom_column'])
			->where(['author_id' => 1])->one();
		$this->assertEquals(1, $article->id);
		$this->assertEquals(10, $article->custom_column);

		// find count, sum, average, min, max, scalar
		$this->assertEquals(2, ArticleIndex::find()->count());
		$this->assertEquals(1, ArticleIndex::find()->where('id=1')->count());
		$this->assertEquals(3, ArticleIndex::find()->sum('id'));
		$this->assertEquals(1.5, ArticleIndex::find()->average('id'));
		$this->assertEquals(1, ArticleIndex::find()->min('id'));
		$this->assertEquals(2, ArticleIndex::find()->max('id'));
		$this->assertEquals(2, ArticleIndex::find()->select('COUNT(*)')->scalar());

		// scope
		$this->assertEquals(1, ArticleIndex::find()->favoriteAuthor()->count());

		// asArray
		$article = ArticleIndex::find()->where('id=2')->asArray()->one();
Paul Klimov committed
81
		unset($article['add_date']);
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
		$this->assertEquals([
			'id' => '2',
			'author_id' => '2',
			'tag' => '3,4',
		], $article);

		// indexBy
		$articles = ArticleIndex::find()->indexBy('author_id')->orderBy('id DESC')->all();
		$this->assertEquals(2, count($articles));
		$this->assertTrue($articles['1'] instanceof ArticleIndex);
		$this->assertTrue($articles['2'] instanceof ArticleIndex);

		// indexBy callable
		$articles = ArticleIndex::find()->indexBy(function ($article) {
			return $article->id . '-' . $article->author_id;
		})->orderBy('id DESC')->all();
		$this->assertEquals(2, count($articles));
		$this->assertTrue($articles['1-1'] instanceof ArticleIndex);
		$this->assertTrue($articles['2-2'] instanceof ArticleIndex);
	}

	public function testFindBySql()
	{
		// find one
		$article = ArticleIndex::findBySql('SELECT * FROM yii2_test_article_index ORDER BY id DESC')->one();
		$this->assertTrue($article instanceof ArticleIndex);
		$this->assertEquals(2, $article->author_id);

		// find all
		$articles = ArticleIndex::findBySql('SELECT * FROM yii2_test_article_index')->all();
		$this->assertEquals(2, count($articles));

		// find with parameter binding
		$article = ArticleIndex::findBySql('SELECT * FROM yii2_test_article_index WHERE id=:id', [':id' => 2])->one();
		$this->assertTrue($article instanceof ArticleIndex);
		$this->assertEquals(2, $article->author_id);
	}

	public function testInsert()
	{
		$record = new RuntimeIndex;
		$record->id = 15;
		$record->title = 'test title';
		$record->content = 'test content';
		$record->type_id = 7;
		$record->category = [1, 2];

		$this->assertTrue($record->isNewRecord);

		$record->save();

		$this->assertEquals(15, $record->id);
		$this->assertFalse($record->isNewRecord);
	}

	/**
	 * @depends testInsert
	 */
	public function testUpdate()
	{
		$record = new RuntimeIndex;
		$record->id = 2;
		$record->title = 'test title';
		$record->content = 'test content';
		$record->type_id = 7;
		$record->category = [1, 2];
		$record->save();

		// save
151
		$record = RuntimeIndex::find(2);
152 153 154 155 156 157 158 159 160 161 162
		$this->assertTrue($record instanceof RuntimeIndex);
		$this->assertEquals(7, $record->type_id);
		$this->assertFalse($record->isNewRecord);

		$record->type_id = 9;
		$record->save();
		$this->assertEquals(9, $record->type_id);
		$this->assertFalse($record->isNewRecord);
		$record2 = RuntimeIndex::find(['id' => 2]);
		$this->assertEquals(9, $record2->type_id);

163 164
		// replace
		$query = 'replace';
165
		$rows = RuntimeIndex::find()->match($query)->all();
166
		$this->assertEmpty($rows);
167
		$record = RuntimeIndex::find(2);
168 169
		$record->content = 'Test content with ' . $query;
		$record->save();
170
		$rows = RuntimeIndex::find()->match($query);
171 172
		$this->assertNotEmpty($rows);

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		// updateAll
		$pk = ['id' => 2];
		$ret = RuntimeIndex::updateAll(['type_id' => 55], $pk);
		$this->assertEquals(1, $ret);
		$record = RuntimeIndex::find($pk);
		$this->assertEquals(55, $record->type_id);
	}

	/**
	 * @depends testInsert
	 */
	public function testDelete()
	{
		// delete
		$record = new RuntimeIndex;
		$record->id = 2;
		$record->title = 'test title';
		$record->content = 'test content';
		$record->type_id = 7;
		$record->category = [1, 2];
		$record->save();

195
		$record = RuntimeIndex::find(2);
196
		$record->delete();
197
		$record = RuntimeIndex::find(2);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		$this->assertNull($record);

		// deleteAll
		$record = new RuntimeIndex;
		$record->id = 2;
		$record->title = 'test title';
		$record->content = 'test content';
		$record->type_id = 7;
		$record->category = [1, 2];
		$record->save();

		$ret = RuntimeIndex::deleteAll('id = 2');
		$this->assertEquals(1, $ret);
		$records = RuntimeIndex::find()->all();
		$this->assertEquals(0, count($records));
	}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	public function testCallSnippets()
	{
		$query = 'pencil';
		$source = 'Some data sentence about ' . $query;

		$snippet = ArticleIndex::callSnippets($source, $query);
		$this->assertNotEmpty($snippet, 'Unable to call snippets!');
		$this->assertContains('<b>' . $query . '</b>', $snippet, 'Query not present in the snippet!');

		$rows = ArticleIndex::callSnippets([$source], $query);
		$this->assertNotEmpty($rows, 'Unable to call snippets!');
		$this->assertContains('<b>' . $query . '</b>', $rows[0], 'Query not present in the snippet!');
	}

	public function testCallKeywords()
	{
		$text = 'table pencil';
		$rows = ArticleIndex::callKeywords($text);
		$this->assertNotEmpty($rows, 'Unable to call keywords!');
		$this->assertArrayHasKey('tokenized', $rows[0], 'No tokenized keyword!');
		$this->assertArrayHasKey('normalized', $rows[0], 'No normalized keyword!');
	}
237
}