ExternalActiveRelationTest.php 2.01 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 9
use yiiunit\data\ar\sphinx\ArticleIndex;
use yiiunit\data\ar\sphinx\ArticleDb;
use yiiunit\data\ar\sphinx\TagDb;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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

	// Tests :

	public function testFindLazy()
	{
		/** @var ArticleIndex $article */
		$article = ArticleIndex::find(['id' => 2]);
29 30

		// has one :
31 32 33 34
		$this->assertFalse($article->isRelationPopulated('source'));
		$source = $article->source;
		$this->assertTrue($article->isRelationPopulated('source'));
		$this->assertTrue($source instanceof ArticleDb);
35
		$this->assertEquals(1, count($article->relatedRecords));
36 37 38 39 40 41 42

		// has many :
		/*$this->assertFalse($article->isRelationPopulated('tags'));
		$tags = $article->tags;
		$this->assertTrue($article->isRelationPopulated('tags'));
		$this->assertEquals(3, count($tags));
		$this->assertTrue($tags[0] instanceof TagDb);*/
43 44 45 46
	}

	public function testFindEager()
	{
47
		// has one :
48 49 50 51 52 53
		$articles = ArticleIndex::find()->with('source')->all();
		$this->assertEquals(2, count($articles));
		$this->assertTrue($articles[0]->isRelationPopulated('source'));
		$this->assertTrue($articles[1]->isRelationPopulated('source'));
		$this->assertTrue($articles[0]->source instanceof ArticleDb);
		$this->assertTrue($articles[1]->source instanceof ArticleDb);
54 55 56 57 58 59

		// has many :
		/*$articles = ArticleIndex::find()->with('tags')->all();
		$this->assertEquals(2, count($articles));
		$this->assertTrue($articles[0]->isRelationPopulated('tags'));
		$this->assertTrue($articles[1]->isRelationPopulated('tags'));*/
60 61 62 63 64 65 66 67 68 69 70 71 72 73
	}

	/**
	 * @depends testFindEager
	 */
	public function testFindWithSnippets()
	{
		$articles = ArticleIndex::find()
			->match('about')
			->with('source')
			->snippetByModel()
			->all();
		$this->assertEquals(2, count($articles));
	}
74
}