Commit 62968971 by Klimov Paul

Sphinx unit test "ActiveRelationTest" created.

parent fa086742
......@@ -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',
],
]
];
<?php
namespace yiiunit\data\sphinx\ar;
use yiiunit\data\ar;
class ArticleDb extends ActiveRecord
{
public static function tableName()
{
return 'yii2_test_article';
}
}
\ No newline at end of file
......@@ -2,6 +2,8 @@
namespace yiiunit\data\sphinx\ar;
use yii\db\ActiveRelation;
class ArticleIndex extends ActiveRecord
{
public $custom_column;
......@@ -15,4 +17,15 @@ class ArticleIndex extends ActiveRecord
{
$query->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
<?php
namespace yiiunit\data\sphinx\ar;
use yiiunit\data\ar;
class ItemDb extends ActiveRecord
{
public static function tableName()
{
return 'yii2_test_item';
}
}
\ No newline at end of file
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/**
* This is the MySQL database schema for creation of the test Sphinx index sources.
*/
DROP TABLE IF EXISTS yii2_test_article;
DROP TABLE IF EXISTS yii2_test_item;
DROP TABLE IF EXISTS yii2_test_article_tag;
CREATE TABLE IF NOT EXISTS `yii2_test_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
......@@ -10,10 +15,6 @@ CREATE TABLE IF NOT EXISTS `yii2_test_article` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `yii2_test_article` (`id`, `title`, `content`, `author_id`, `create_date`) VALUES
(1, 'About cats', 'This article is about cats', 1, '2013-10-23 00:00:00'),
(2, 'About dogs', 'This article is about dogs', 2, '2013-11-15 00:00:00');
CREATE TABLE IF NOT EXISTS `yii2_test_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
......@@ -23,16 +24,20 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
INSERT INTO `yii2_test_item` (`id`, `name`, `description`, `category_id`, `price`) VALUES
(1, 'pencil', 'Simple pencil', 1, 2.5),
(2, 'table', 'Wooden table', 2, 100);
CREATE TABLE IF NOT EXISTS `yii2_test_article_tag` (
`article_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
PRIMARY KEY (`article_id`,`tag_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `yii2_test_article` (`id`, `title`, `content`, `author_id`, `create_date`) VALUES
(1, 'About cats', 'This article is about cats', 1, '2013-10-23 00:00:00'),
(2, 'About dogs', 'This article is about dogs', 2, '2013-11-15 00:00:00');
INSERT INTO `yii2_test_item` (`id`, `name`, `description`, `category_id`, `price`) VALUES
(1, 'pencil', 'Simple pencil', 1, 2.5),
(2, 'table', 'Wooden table', 2, 100);
INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES
(1, 1),
(1, 2),
......
......@@ -2,7 +2,7 @@
#
# Setup test environment:
# - initialize test database source:
# mysql -D yii2test -u test < /path/to/yii/tests/unit/data/sphinx/sphinx.sql
# mysql -D yii2test -u test < /path/to/yii/tests/unit/data/sphinx/source.sql
# - setup test Sphinx indexes:
# indexer --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf --all [--rotate]
# - run the "searchd" daemon:
......
<?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 ActiveRelationTest extends SphinxTestCase
{
protected function setUp()
{
parent::setUp();
ActiveRecord::$db = $this->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
......@@ -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
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