From 07ad008d49b87e835be3722c4dc7ca2558b96ea6 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Wed, 13 Nov 2013 12:23:28 +0200 Subject: [PATCH] Sphinx unit tests advanced. --- extensions/sphinx/Connection.php | 11 ++++ tests/unit/extensions/sphinx/CommandTest.php | 52 ++++++++++++++++++ tests/unit/extensions/sphinx/SchemaTest.php | 71 +++++++++++++++++++++++++ tests/unit/extensions/sphinx/SphinxTestCase.php | 2 +- 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/unit/extensions/sphinx/SchemaTest.php diff --git a/extensions/sphinx/Connection.php b/extensions/sphinx/Connection.php index 8308005..37a4b59 100644 --- a/extensions/sphinx/Connection.php +++ b/extensions/sphinx/Connection.php @@ -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 @@ -50,6 +51,16 @@ class Connection extends \yii\db\Connection } /** + * 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 * @param array $params the parameters to be bound to the SQL statement diff --git a/tests/unit/extensions/sphinx/CommandTest.php b/tests/unit/extensions/sphinx/CommandTest.php index 40cf895..ebca709 100644 --- a/tests/unit/extensions/sphinx/CommandTest.php +++ b/tests/unit/extensions/sphinx/CommandTest.php @@ -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(); diff --git a/tests/unit/extensions/sphinx/SchemaTest.php b/tests/unit/extensions/sphinx/SchemaTest.php new file mode 100644 index 0000000..cdb185a --- /dev/null +++ b/tests/unit/extensions/sphinx/SchemaTest.php @@ -0,0 +1,71 @@ +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); + } +} \ No newline at end of file diff --git a/tests/unit/extensions/sphinx/SphinxTestCase.php b/tests/unit/extensions/sphinx/SphinxTestCase.php index a77f987..6b936a3 100644 --- a/tests/unit/extensions/sphinx/SphinxTestCase.php +++ b/tests/unit/extensions/sphinx/SphinxTestCase.php @@ -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) {