ExternalActiveRelationTest.php 2.83 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
use yiiunit\data\ar\sphinx\TagDb;
10 11 12 13 14 15

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

23
    // Tests :
24

25 26
    public function testFindLazy()
    {
27
        /* @var $article ArticleIndex */
Alexander Makarov committed
28
        $article = ArticleIndex::findOne(['id' => 2]);
29

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

37
        // has many :
38
        $this->assertFalse($article->isRelationPopulated('tags'));
39 40
        $tags = $article->tags;
        $this->assertTrue($article->isRelationPopulated('tags'));
41 42 43 44 45
        $this->assertEquals(count($article->tag), count($tags));
        $this->assertTrue($tags[0] instanceof TagDb);
        foreach ($tags as $tag) {
            $this->assertTrue(in_array($tag->id, $article->tag));
        }
46
    }
47

48 49 50 51 52 53 54 55 56
    public function testFindEager()
    {
        // has one :
        $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);
57

58
        // has many :
59
        $articles = ArticleIndex::find()->with('tags')->all();
60 61
        $this->assertEquals(2, count($articles));
        $this->assertTrue($articles[0]->isRelationPopulated('tags'));
62 63 64 65 66 67 68 69 70 71 72
        $this->assertTrue($articles[1]->isRelationPopulated('tags'));
        foreach ($articles as $article) {
            $this->assertTrue($article->isRelationPopulated('tags'));
            $tags = $article->tags;
            $this->assertEquals(count($article->tag), count($tags));
            //var_dump($tags);
            $this->assertTrue($tags[0] instanceof TagDb);
            foreach ($tags as $tag) {
                $this->assertTrue(in_array($tag->id, $article->tag));
            }
        }
73
    }
74

75 76 77 78 79 80 81 82 83 84 85 86
    /**
     * @depends testFindEager
     */
    public function testFindWithSnippets()
    {
        $articles = ArticleIndex::find()
            ->match('about')
            ->with('source')
            ->snippetByModel()
            ->all();
        $this->assertEquals(2, count($articles));
    }
87
}