ActiveRelationTest.php 1.93 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\extensions\sphinx;

5
use yiiunit\data\ar\sphinx\ActiveRecord;
6
use yiiunit\data\ar\ActiveRecord as ActiveRecordDb;
7 8
use yiiunit\data\ar\sphinx\ArticleIndex;
use yiiunit\data\ar\sphinx\ArticleDb;
9 10 11 12 13 14

/**
 * @group sphinx
 */
class ActiveRelationTest extends SphinxTestCase
{
15 16 17 18 19 20
    protected function setUp()
    {
        parent::setUp();
        ActiveRecord::$db = $this->getConnection();
        ActiveRecordDb::$db = $this->getDbConnection();
    }
21

22
    // Tests :
23

24 25
    public function testFindLazy()
    {
26
        /* @var $article ArticleDb */
Alexander Makarov committed
27
        $article = ArticleDb::findOne(['id' => 2]);
28 29 30 31 32 33 34
        $this->assertFalse($article->isRelationPopulated('index'));
        $index = $article->index;
        $this->assertTrue($article->isRelationPopulated('index'));
        $this->assertTrue($index instanceof ArticleIndex);
        $this->assertEquals(1, count($article->relatedRecords));
        $this->assertEquals($article->id, $index->id);
    }
35

36 37 38 39 40 41 42 43 44
    public function testFindEager()
    {
        $articles = ArticleDb::find()->with('index')->all();
        $this->assertEquals(2, count($articles));
        $this->assertTrue($articles[0]->isRelationPopulated('index'));
        $this->assertTrue($articles[1]->isRelationPopulated('index'));
        $this->assertTrue($articles[0]->index instanceof ArticleIndex);
        $this->assertTrue($articles[1]->index instanceof ArticleIndex);
    }
45 46 47 48 49 50 51 52 53 54 55 56 57

    /**
     * @see https://github.com/yiisoft/yii2/issues/4018
     */
    public function testFindCompositeLink()
    {
        $articles = ArticleIndex::find()->with('sourceCompositeLink')->all();
        $this->assertEquals(2, count($articles));
        $this->assertTrue($articles[0]->isRelationPopulated('sourceCompositeLink'));
        $this->assertNotEmpty($articles[0]->sourceCompositeLink);
        $this->assertTrue($articles[1]->isRelationPopulated('sourceCompositeLink'));
        $this->assertNotEmpty($articles[1]->sourceCompositeLink);
    }
58
}