diff --git a/tests/unit/data/config.php b/tests/unit/data/config.php index 304c3fc..5fd7e3e 100644 --- a/tests/unit/data/config.php +++ b/tests/unit/data/config.php @@ -30,4 +30,17 @@ return [ 'fixture' => __DIR__ . '/postgres.sql', ], ], + 'sphinx' => [ + 'sphinx' => [ + 'dsn' => 'mysql:host=127.0.0.1;port=9306;', + 'username' => '', + 'password' => '', + ], + 'db' => [ + 'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest', + 'username' => 'travis', + 'password' => '', + 'fixture' => __DIR__ . '/sphinx/source.sql', + ], + ] ]; diff --git a/tests/unit/data/sphinx/ar/ArticleDb.php b/tests/unit/data/sphinx/ar/ArticleDb.php new file mode 100644 index 0000000..de9ca0f --- /dev/null +++ b/tests/unit/data/sphinx/ar/ArticleDb.php @@ -0,0 +1,13 @@ +andWhere('author_id=1'); } + + public function getSource() + { + $config = [ + 'modelClass' => ArticleDb::className(), + 'primaryModel' => $this, + 'link' => ['id' => 'id'], + 'multiple' => false, + ]; + return new ActiveRelation($config); + } } \ No newline at end of file diff --git a/tests/unit/data/sphinx/ar/ItemDb.php b/tests/unit/data/sphinx/ar/ItemDb.php new file mode 100644 index 0000000..128e473 --- /dev/null +++ b/tests/unit/data/sphinx/ar/ItemDb.php @@ -0,0 +1,13 @@ +getConnection(); + ActiveRecordDb::$db = $this->getDbConnection(); + } + + 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); + } +} \ No newline at end of file diff --git a/tests/unit/extensions/sphinx/SphinxTestCase.php b/tests/unit/extensions/sphinx/SphinxTestCase.php index 6b936a3..899ea72 100644 --- a/tests/unit/extensions/sphinx/SphinxTestCase.php +++ b/tests/unit/extensions/sphinx/SphinxTestCase.php @@ -21,9 +21,21 @@ class SphinxTestCase extends TestCase 'password' => '', ]; /** - * @var Connection + * @var Connection Sphinx connection instance. */ protected $sphinx; + /** + * @var array Database connection configuration. + */ + protected $dbConfig = [ + 'dsn' => 'mysql:host=127.0.0.1;', + 'username' => '', + 'password' => '', + ]; + /** + * @var \yii\db\Connection database connection instance. + */ + protected $db; public static function setUpBeforeClass() { @@ -33,10 +45,14 @@ class SphinxTestCase extends TestCase protected function setUp() { parent::setUp(); - //$this->sphinxConfig = $this->getParam('sphinx'); if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) { $this->markTestSkipped('pdo and pdo_mysql extension are required.'); } + $config = $this->getParam('sphinx'); + if (!empty($config)) { + $this->sphinxConfig = $config['sphinx']; + $this->dbConfig = $config['db']; + } $this->mockApplication(); static::loadClassMap(); } @@ -67,15 +83,15 @@ class SphinxTestCase extends TestCase /** * @param bool $reset whether to clean up the test database - * @param bool $open whether to open and populate test database + * @param bool $open whether to open test database * @return \yii\sphinx\Connection */ - public function getConnection($reset = true, $open = true) + public function getConnection($reset = false, $open = true) { if (!$reset && $this->sphinx) { return $this->sphinx; } - $db = new \yii\sphinx\Connection; + $db = new Connection; $db->dsn = $this->sphinxConfig['dsn']; if (isset($this->sphinxConfig['username'])) { $db->username = $this->sphinxConfig['username']; @@ -86,14 +102,6 @@ class SphinxTestCase extends TestCase } if ($open) { $db->open(); - if (!empty($this->sphinxConfig['fixture'])) { - $lines = explode(';', file_get_contents($this->sphinxConfig['fixture'])); - foreach ($lines as $line) { - if (trim($line) !== '') { - $db->pdo->exec($line); - } - } - } } $this->sphinx = $db; return $db; @@ -109,4 +117,38 @@ class SphinxTestCase extends TestCase $this->sphinx->createCommand('TRUNCATE RTINDEX ' . $indexName)->execute(); } } + + /** + * @param bool $reset whether to clean up the test database + * @param bool $open whether to open and populate test database + * @return \yii\db\Connection + */ + public function getDbConnection($reset = true, $open = true) + { + if (!$reset && $this->db) { + return $this->db; + } + $db = new \yii\db\Connection; + $db->dsn = $this->dbConfig['dsn']; + if (isset($this->dbConfig['username'])) { + $db->username = $this->dbConfig['username']; + $db->password = $this->dbConfig['password']; + } + if (isset($this->dbConfig['attributes'])) { + $db->attributes = $this->dbConfig['attributes']; + } + if ($open) { + $db->open(); + if (!empty($this->dbConfig['fixture'])) { + $lines = explode(';', file_get_contents($this->dbConfig['fixture'])); + foreach ($lines as $line) { + if (trim($line) !== '') { + $db->pdo->exec($line); + } + } + } + } + $this->db = $db; + return $db; + } } \ No newline at end of file