ActiveRelationTest.php 1.25 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 15 16 17 18 19 20 21

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

22 23
	// Tests :

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

	public function testFindEager()
	{
38
		$articles = ArticleDb::find()->with('index')->all();
39
		$this->assertEquals(2, count($articles));
40 41 42 43
		$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);
44
	}
45
}