ActiveRecordTest.php 7.97 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

/**
 * @group sphinx
 */
class ActiveRecordTest extends SphinxTestCase
{
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
    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
Alexander Makarov committed
44
        $article = ArticleIndex::findOne(2);
45 46 47 48
        $this->assertTrue($article instanceof ArticleIndex);
        $this->assertEquals(2, $article->id);

        // find by column values
Alexander Makarov committed
49
        $article = ArticleIndex::findOne(['id' => 2, 'author_id' => 2]);
50 51 52
        $this->assertTrue($article instanceof ArticleIndex);
        $this->assertEquals(2, $article->id);
        $this->assertEquals(2, $article->author_id);
Alexander Makarov committed
53
        $article = ArticleIndex::findOne(['id' => 2, 'author_id' => 1]);
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
        $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();
        unset($article['add_date']);
        $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
Alexander Makarov committed
151
        $record = RuntimeIndex::findOne(2);
152 153 154 155 156 157 158 159
        $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);
Alexander Makarov committed
160
        $record2 = RuntimeIndex::findOne(['id' => 2]);
161 162 163 164 165 166
        $this->assertEquals(9, $record2->type_id);

        // replace
        $query = 'replace';
        $rows = RuntimeIndex::find()->match($query)->all();
        $this->assertEmpty($rows);
Alexander Makarov committed
167
        $record = RuntimeIndex::findOne(2);
168 169 170 171 172 173 174 175 176
        $record->content = 'Test content with ' . $query;
        $record->save();
        $rows = RuntimeIndex::find()->match($query);
        $this->assertNotEmpty($rows);

        // updateAll
        $pk = ['id' => 2];
        $ret = RuntimeIndex::updateAll(['type_id' => 55], $pk);
        $this->assertEquals(1, $ret);
Alexander Makarov committed
177
        $record = RuntimeIndex::findOne($pk);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        $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();

Alexander Makarov committed
195
        $record = RuntimeIndex::findOne(2);
196
        $record->delete();
Alexander Makarov committed
197
        $record = RuntimeIndex::findOne(2);
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
        $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));
    }

    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!');
    }
AlexGx committed
237
}