Commit 6bfc888c by Paul Klimov

Sphinx Active Relation created

parent c355fed2
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\sphinx;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
/**
* Class ActiveRelation
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
{
use ActiveRelationTrait;
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace yiiunit\data\sphinx\ar; namespace yiiunit\data\sphinx\ar;
use yii\sphinx\ActiveRelation;
use yiiunit\data\ar\ActiveRecord as ActiveRecordDb; use yiiunit\data\ar\ActiveRecord as ActiveRecordDb;
class ArticleDb extends ActiveRecordDb class ArticleDb extends ActiveRecordDb
...@@ -10,4 +11,15 @@ class ArticleDb extends ActiveRecordDb ...@@ -10,4 +11,15 @@ class ArticleDb extends ActiveRecordDb
{ {
return 'yii2_test_article'; return 'yii2_test_article';
} }
public function getIndex()
{
$config = [
'modelClass' => ArticleIndex::className(),
'primaryModel' => $this,
'link' => ['id' => 'id'],
'multiple' => false,
];
return new ActiveRelation($config);
}
} }
\ No newline at end of file
...@@ -19,37 +19,26 @@ class ActiveRelationTest extends SphinxTestCase ...@@ -19,37 +19,26 @@ class ActiveRelationTest extends SphinxTestCase
ActiveRecordDb::$db = $this->getDbConnection(); ActiveRecordDb::$db = $this->getDbConnection();
} }
// Tests :
public function testFindLazy() public function testFindLazy()
{ {
/** @var ArticleIndex $article */ /** @var ArticleDb $article */
$article = ArticleIndex::find(['id' => 2]); $article = ArticleDb::find(['id' => 2]);
$this->assertFalse($article->isRelationPopulated('source')); $this->assertFalse($article->isRelationPopulated('index'));
$source = $article->source; $index = $article->index;
$this->assertTrue($article->isRelationPopulated('source')); $this->assertTrue($article->isRelationPopulated('index'));
$this->assertTrue($source instanceof ArticleDb); $this->assertTrue($index instanceof ArticleIndex);
$this->assertEquals(1, count($article->populatedRelations)); $this->assertEquals(1, count($article->populatedRelations));
} }
public function testFindEager() public function testFindEager()
{ {
$articles = ArticleIndex::find()->with('source')->all(); $articles = ArticleDb::find()->with('index')->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);
}
/**
* @depends testFindEager
*/
public function testFindWithSnippets()
{
$articles = ArticleIndex::find()
->match('about')
->with('source')
->snippetByModel()
->all();
$this->assertEquals(2, count($articles)); $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);
} }
} }
\ No newline at end of file
<?php
namespace yiiunit\extensions\sphinx;
use yiiunit\data\sphinx\ar\ActiveRecord;
use yiiunit\data\ar\ActiveRecord as ActiveRecordDb;
use yiiunit\data\sphinx\ar\ArticleIndex;
use yiiunit\data\sphinx\ar\ArticleDb;
/**
* @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]);
$this->assertFalse($article->isRelationPopulated('source'));
$source = $article->source;
$this->assertTrue($article->isRelationPopulated('source'));
$this->assertTrue($source instanceof ArticleDb);
$this->assertEquals(1, count($article->populatedRelations));
}
public function testFindEager()
{
$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);
}
/**
* @depends testFindEager
*/
public function testFindWithSnippets()
{
$articles = ArticleIndex::find()
->match('about')
->with('source')
->snippetByModel()
->all();
$this->assertEquals(2, count($articles));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment