mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-19 18:00:51 +08:00
Sphinx unit tests advanced.
This commit is contained in:
@ -10,6 +10,7 @@ namespace yii\sphinx;
|
||||
/**
|
||||
* Class Connection
|
||||
*
|
||||
* @property Schema $schema The schema information for this Sphinx connection. This property is read-only.
|
||||
* @method Schema getSchema() The schema information for this Sphinx connection
|
||||
*
|
||||
* @author Paul Klimov <klimov.paul@gmail.com>
|
||||
@ -49,6 +50,16 @@ class Connection extends \yii\db\Connection
|
||||
return $this->getSchema()->quoteIndexName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias of [[quoteIndexName()]].
|
||||
* @param string $name table name
|
||||
* @return string the properly quoted table name
|
||||
*/
|
||||
public function quoteTableName($name)
|
||||
{
|
||||
return $this->quoteIndexName($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a command for execution.
|
||||
* @param string $sql the SQL statement to be executed
|
||||
|
@ -18,6 +18,58 @@ class CommandTest extends SphinxTestCase
|
||||
|
||||
// Tests :
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$db = $this->getConnection(false);
|
||||
|
||||
// null
|
||||
$command = $db->createCommand();
|
||||
$this->assertEquals(null, $command->sql);
|
||||
|
||||
// string
|
||||
$sql = 'SELECT * FROM yii2_test_item_index';
|
||||
$params = [
|
||||
'name' => 'value'
|
||||
];
|
||||
$command = $db->createCommand($sql, $params);
|
||||
$this->assertEquals($sql, $command->sql);
|
||||
$this->assertEquals($params, $command->params);
|
||||
}
|
||||
|
||||
public function testGetSetSql()
|
||||
{
|
||||
$db = $this->getConnection(false);
|
||||
|
||||
$sql = 'SELECT * FROM yii2_test_item_index';
|
||||
$command = $db->createCommand($sql);
|
||||
$this->assertEquals($sql, $command->sql);
|
||||
|
||||
$sql2 = 'SELECT * FROM yii2_test_item_index';
|
||||
$command->sql = $sql2;
|
||||
$this->assertEquals($sql2, $command->sql);
|
||||
}
|
||||
|
||||
public function testAutoQuoting()
|
||||
{
|
||||
$db = $this->getConnection(false);
|
||||
|
||||
$sql = 'SELECT [[id]], [[t.name]] FROM {{yii2_test_item_index}} t';
|
||||
$command = $db->createCommand($sql);
|
||||
$this->assertEquals("SELECT `id`, `t`.`name` FROM `yii2_test_item_index` t", $command->sql);
|
||||
}
|
||||
|
||||
public function testPrepareCancel()
|
||||
{
|
||||
$db = $this->getConnection(false);
|
||||
|
||||
$command = $db->createCommand('SELECT * FROM yii2_test_item_index');
|
||||
$this->assertEquals(null, $command->pdoStatement);
|
||||
$command->prepare();
|
||||
$this->assertNotEquals(null, $command->pdoStatement);
|
||||
$command->cancel();
|
||||
$this->assertEquals(null, $command->pdoStatement);
|
||||
}
|
||||
|
||||
public function testExecute()
|
||||
{
|
||||
$db = $this->getConnection();
|
||||
|
71
tests/unit/extensions/sphinx/SchemaTest.php
Normal file
71
tests/unit/extensions/sphinx/SchemaTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace yiiunit\extensions\sphinx;
|
||||
|
||||
use yii\caching\FileCache;
|
||||
use yii\sphinx\Schema;
|
||||
|
||||
/**
|
||||
* @group sphinx
|
||||
*/
|
||||
class SchemaTest extends SphinxTestCase
|
||||
{
|
||||
public function testFindIndexNames()
|
||||
{
|
||||
$schema = $this->getConnection()->schema;
|
||||
|
||||
$indexes = $schema->getIndexNames();
|
||||
$this->assertContains('yii2_test_article_index', $indexes);
|
||||
$this->assertContains('yii2_test_item_index', $indexes);
|
||||
$this->assertContains('yii2_test_rt_index', $indexes);
|
||||
}
|
||||
|
||||
public function testGetIndexSchemas()
|
||||
{
|
||||
$schema = $this->getConnection()->schema;
|
||||
|
||||
$indexes = $schema->getTableSchemas();
|
||||
$this->assertEquals(count($schema->getIndexNames()), count($indexes));
|
||||
foreach($indexes as $index) {
|
||||
$this->assertInstanceOf('yii\sphinx\IndexSchema', $index);
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetNonExistingIndexSchema()
|
||||
{
|
||||
$this->assertNull($this->getConnection()->schema->getIndexSchema('non_existing_index'));
|
||||
}
|
||||
|
||||
public function testSchemaRefresh()
|
||||
{
|
||||
$schema = $this->getConnection()->schema;
|
||||
|
||||
$schema->db->enableSchemaCache = true;
|
||||
$schema->db->schemaCache = new FileCache();
|
||||
$noCacheIndex = $schema->getIndexSchema('yii2_test_rt_index', true);
|
||||
$cachedIndex = $schema->getIndexSchema('yii2_test_rt_index', true);
|
||||
$this->assertEquals($noCacheIndex, $cachedIndex);
|
||||
}
|
||||
|
||||
public function testGetPDOType()
|
||||
{
|
||||
$values = [
|
||||
[null, \PDO::PARAM_NULL],
|
||||
['', \PDO::PARAM_STR],
|
||||
['hello', \PDO::PARAM_STR],
|
||||
[0, \PDO::PARAM_INT],
|
||||
[1, \PDO::PARAM_INT],
|
||||
[1337, \PDO::PARAM_INT],
|
||||
[true, \PDO::PARAM_BOOL],
|
||||
[false, \PDO::PARAM_BOOL],
|
||||
[$fp=fopen(__FILE__, 'rb'), \PDO::PARAM_LOB],
|
||||
];
|
||||
|
||||
$schema = $this->getConnection()->schema;
|
||||
|
||||
foreach($values as $value) {
|
||||
$this->assertEquals($value[1], $schema->getPdoType($value[0]));
|
||||
}
|
||||
fclose($fp);
|
||||
}
|
||||
}
|
@ -68,7 +68,7 @@ class SphinxTestCase extends TestCase
|
||||
/**
|
||||
* @param bool $reset whether to clean up the test database
|
||||
* @param bool $open whether to open and populate test database
|
||||
* @return \yii\db\Connection
|
||||
* @return \yii\sphinx\Connection
|
||||
*/
|
||||
public function getConnection($reset = true, $open = true)
|
||||
{
|
||||
|
Reference in New Issue
Block a user