Browse Source

Sphinx unit test "ActiveRelationTest" created.

tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
62968971b6
  1. 13
      tests/unit/data/config.php
  2. 13
      tests/unit/data/sphinx/ar/ArticleDb.php
  3. 13
      tests/unit/data/sphinx/ar/ArticleIndex.php
  4. 13
      tests/unit/data/sphinx/ar/ItemDb.php
  5. 25
      tests/unit/data/sphinx/source.sql
  6. 2
      tests/unit/data/sphinx/sphinx.conf
  7. 42
      tests/unit/extensions/sphinx/ActiveRelationTest.php
  8. 68
      tests/unit/extensions/sphinx/SphinxTestCase.php

13
tests/unit/data/config.php

@ -30,4 +30,17 @@ return [
'fixture' => __DIR__ . '/postgres.sql', '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',
],
]
]; ];

13
tests/unit/data/sphinx/ar/ArticleDb.php

@ -0,0 +1,13 @@
<?php
namespace yiiunit\data\sphinx\ar;
use yiiunit\data\ar;
class ArticleDb extends ActiveRecord
{
public static function tableName()
{
return 'yii2_test_article';
}
}

13
tests/unit/data/sphinx/ar/ArticleIndex.php

@ -2,6 +2,8 @@
namespace yiiunit\data\sphinx\ar; namespace yiiunit\data\sphinx\ar;
use yii\db\ActiveRelation;
class ArticleIndex extends ActiveRecord class ArticleIndex extends ActiveRecord
{ {
public $custom_column; public $custom_column;
@ -15,4 +17,15 @@ class ArticleIndex extends ActiveRecord
{ {
$query->andWhere('author_id=1'); $query->andWhere('author_id=1');
} }
public function getSource()
{
$config = [
'modelClass' => ArticleDb::className(),
'primaryModel' => $this,
'link' => ['id' => 'id'],
'multiple' => false,
];
return new ActiveRelation($config);
}
} }

13
tests/unit/data/sphinx/ar/ItemDb.php

@ -0,0 +1,13 @@
<?php
namespace yiiunit\data\sphinx\ar;
use yiiunit\data\ar;
class ItemDb extends ActiveRecord
{
public static function tableName()
{
return 'yii2_test_item';
}
}

25
tests/unit/data/sphinx/sphinx.sql → tests/unit/data/sphinx/source.sql

@ -1,5 +1,10 @@
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` ( CREATE TABLE IF NOT EXISTS `yii2_test_article` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
@ -10,10 +15,6 @@ CREATE TABLE IF NOT EXISTS `yii2_test_article` (
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; ) 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` ( CREATE TABLE IF NOT EXISTS `yii2_test_item` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL, `name` varchar(255) NOT NULL,
@ -23,16 +24,20 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` (
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; ) 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` ( CREATE TABLE IF NOT EXISTS `yii2_test_article_tag` (
`article_id` int(11) NOT NULL, `article_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL, `tag_id` int(11) NOT NULL,
PRIMARY KEY (`article_id`,`tag_id`) PRIMARY KEY (`article_id`,`tag_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ) 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 INSERT INTO `yii2_test_article_tag` (`article_id`, `tag_id`) VALUES
(1, 1), (1, 1),
(1, 2), (1, 2),

2
tests/unit/data/sphinx/sphinx.conf

@ -2,7 +2,7 @@
# #
# Setup test environment: # Setup test environment:
# - initialize test database source: # - 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: # - setup test Sphinx indexes:
# indexer --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf --all [--rotate] # indexer --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf --all [--rotate]
# - run the "searchd" daemon: # - run the "searchd" daemon:

42
tests/unit/extensions/sphinx/ActiveRelationTest.php

@ -0,0 +1,42 @@
<?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);
}
}

68
tests/unit/extensions/sphinx/SphinxTestCase.php

@ -21,9 +21,21 @@ class SphinxTestCase extends TestCase
'password' => '', 'password' => '',
]; ];
/** /**
* @var Connection * @var Connection Sphinx connection instance.
*/ */
protected $sphinx; 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() public static function setUpBeforeClass()
{ {
@ -33,10 +45,14 @@ class SphinxTestCase extends TestCase
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
//$this->sphinxConfig = $this->getParam('sphinx');
if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) { if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
$this->markTestSkipped('pdo and pdo_mysql extension are required.'); $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(); $this->mockApplication();
static::loadClassMap(); static::loadClassMap();
} }
@ -67,15 +83,15 @@ class SphinxTestCase extends TestCase
/** /**
* @param bool $reset whether to clean up the test database * @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 * @return \yii\sphinx\Connection
*/ */
public function getConnection($reset = true, $open = true) public function getConnection($reset = false, $open = true)
{ {
if (!$reset && $this->sphinx) { if (!$reset && $this->sphinx) {
return $this->sphinx; return $this->sphinx;
} }
$db = new \yii\sphinx\Connection; $db = new Connection;
$db->dsn = $this->sphinxConfig['dsn']; $db->dsn = $this->sphinxConfig['dsn'];
if (isset($this->sphinxConfig['username'])) { if (isset($this->sphinxConfig['username'])) {
$db->username = $this->sphinxConfig['username']; $db->username = $this->sphinxConfig['username'];
@ -86,14 +102,6 @@ class SphinxTestCase extends TestCase
} }
if ($open) { if ($open) {
$db->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; $this->sphinx = $db;
return $db; return $db;
@ -109,4 +117,38 @@ class SphinxTestCase extends TestCase
$this->sphinx->createCommand('TRUNCATE RTINDEX ' . $indexName)->execute(); $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;
}
} }
Loading…
Cancel
Save