Added tests and fix for lastInsertId() on camel case tables.
This commit is contained in:
parent
626e1fe038
commit
9fc73577ca
|
@ -51,7 +51,7 @@ class Database extends \PDO
|
|||
public function lastInsertIdExtended($table = null)
|
||||
{
|
||||
if ($table && self::POSTGRESQL_TYPE === $this->getAttribute(self::ATTR_DRIVER_NAME)) {
|
||||
return parent::lastInsertId($table . '_id_seq');
|
||||
return parent::lastInsertId('"' . $table . '_id_seq"');
|
||||
}
|
||||
|
||||
return parent::lastInsertId();
|
||||
|
|
|
@ -33,7 +33,7 @@ class DatabaseMysqlTest extends \PHPUnit_Extensions_Database_TestCase
|
|||
$this->connection = $this->createDefaultDBConnection($pdo, MYSQL_DBNAME);
|
||||
|
||||
$this->connection->getConnection()->query('
|
||||
CREATE TABLE IF NOT EXISTS `database_mysql_test` (
|
||||
CREATE TABLE IF NOT EXISTS `databaseMysqlTest` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`projectId` int(11) NOT NULL,
|
||||
`branch` varchar(250) NOT NULL DEFAULT \'master\',
|
||||
|
@ -68,7 +68,7 @@ class DatabaseMysqlTest extends \PHPUnit_Extensions_Database_TestCase
|
|||
protected function getDataSet()
|
||||
{
|
||||
return $this->createArrayDataSet([
|
||||
'database_mysql_test' => [[
|
||||
'databaseMysqlTest' => [[
|
||||
'id' => 1,
|
||||
'projectId' => 1,
|
||||
'branch' => 'master',
|
||||
|
@ -206,22 +206,28 @@ class DatabaseMysqlTest extends \PHPUnit_Extensions_Database_TestCase
|
|||
{
|
||||
$readConnection = Database::getConnection('read');
|
||||
|
||||
$sql = 'SELECT * FROM {{database_mysql_test}} WHERE {{projectId}} = :projectId';
|
||||
$sql = 'SELECT * FROM {{databaseMysqlTest}} WHERE {{projectId}} = :projectId';
|
||||
$query = $readConnection->prepareCommon($sql);
|
||||
|
||||
$query->bindValue(':projectId', 2);
|
||||
$query->bindValue(':projectId', 1);
|
||||
$query->execute();
|
||||
|
||||
$data = $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
self::assertEquals(2, count($data));
|
||||
self::assertEquals(1, count($data));
|
||||
self::assertEquals([[
|
||||
'id' => 1,
|
||||
'projectId' => 1,
|
||||
'branch' => 'master',
|
||||
'createDate' => null,
|
||||
]], $data);
|
||||
}
|
||||
|
||||
public function testLastInsertIdExtended()
|
||||
{
|
||||
$writeConnection = Database::getConnection('write');
|
||||
|
||||
$sql = 'INSERT INTO {{database_mysql_test}} ({{projectId}}) VALUES (3)';
|
||||
$sql = 'INSERT INTO {{databaseMysqlTest}} ({{projectId}}) VALUES (3)';
|
||||
$query = $writeConnection->prepareCommon($sql);
|
||||
|
||||
$query->execute();
|
||||
|
|
|
@ -33,7 +33,7 @@ class DatabasePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
|
|||
$this->connection = $this->createDefaultDBConnection($pdo, POSTGRESQL_DBNAME);
|
||||
|
||||
$this->connection->getConnection()->query('
|
||||
CREATE TABLE IF NOT EXISTS "database_mysql_test" (
|
||||
CREATE TABLE IF NOT EXISTS "databasePostgresqlTest" (
|
||||
"id" SERIAL,
|
||||
"projectId" integer NOT NULL,
|
||||
"branch" character varying(250) NOT NULL DEFAULT \'master\',
|
||||
|
@ -68,7 +68,7 @@ class DatabasePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
|
|||
protected function getDataSet()
|
||||
{
|
||||
return $this->createArrayDataSet([
|
||||
'database_mysql_test' => [[
|
||||
'databasePostgresqlTest' => [[
|
||||
'id' => 1,
|
||||
'projectId' => 1,
|
||||
'branch' => 'master',
|
||||
|
@ -206,30 +206,36 @@ class DatabasePostgresqlTest extends \PHPUnit_Extensions_Database_TestCase
|
|||
{
|
||||
$readConnection = Database::getConnection('read');
|
||||
|
||||
$sql = 'SELECT * FROM {{database_mysql_test}} WHERE {{projectId}} = :projectId';
|
||||
$sql = 'SELECT * FROM {{databasePostgresqlTest}} WHERE {{projectId}} = :projectId';
|
||||
$query = $readConnection->prepareCommon($sql);
|
||||
|
||||
$query->bindValue(':projectId', 2);
|
||||
$query->bindValue(':projectId', 1);
|
||||
$query->execute();
|
||||
|
||||
$data = $query->fetchAll(\PDO::FETCH_ASSOC);
|
||||
|
||||
self::assertEquals(2, count($data));
|
||||
self::assertEquals(1, count($data));
|
||||
self::assertEquals([[
|
||||
'id' => 1,
|
||||
'projectId' => 1,
|
||||
'branch' => 'master',
|
||||
'createDate' => null,
|
||||
]], $data);
|
||||
}
|
||||
|
||||
public function testLastInsertIdExtended()
|
||||
{
|
||||
$this->connection->getConnection()->query('
|
||||
ALTER SEQUENCE "database_mysql_test_id_seq" RESTART WITH 4;
|
||||
ALTER SEQUENCE "databasePostgresqlTest_id_seq" RESTART WITH 4;
|
||||
');
|
||||
|
||||
$writeConnection = Database::getConnection('write');
|
||||
|
||||
$sql = 'INSERT INTO {{database_mysql_test}} ({{projectId}}) VALUES (3)';
|
||||
$sql = 'INSERT INTO {{databasePostgresqlTest}} ({{projectId}}) VALUES (3)';
|
||||
$query = $writeConnection->prepareCommon($sql);
|
||||
|
||||
$query->execute();
|
||||
|
||||
self::assertEquals(4, $writeConnection->lastInsertIdExtended('database_mysql_test'));
|
||||
self::assertEquals(4, $writeConnection->lastInsertIdExtended('databasePostgresqlTest'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue