Commit d1267280 authored by Steve Müller's avatar Steve Müller

Merge pull request #632 from tiagobrito/master

Add test to verify null cast in boolean type
parents a67db17b e2b11774
...@@ -748,7 +748,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -748,7 +748,7 @@ class PostgreSqlPlatform extends AbstractPlatform
private function convertSingleBooleanValue($value, $callback) private function convertSingleBooleanValue($value, $callback)
{ {
if (null === $value) { if (null === $value) {
return $callback(false); return $callback(null);
} }
if (is_bool($value) || is_numeric($value)) { if (is_bool($value) || is_numeric($value)) {
...@@ -812,6 +812,10 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -812,6 +812,10 @@ class PostgreSqlPlatform extends AbstractPlatform
return $this->doConvertBooleans( return $this->doConvertBooleans(
$item, $item,
function ($boolean) { function ($boolean) {
if (null === $boolean) {
return 'NULL';
}
return true === $boolean ? 'true' : 'false'; return true === $boolean ? 'true' : 'false';
} }
); );
...@@ -829,7 +833,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -829,7 +833,7 @@ class PostgreSqlPlatform extends AbstractPlatform
return $this->doConvertBooleans( return $this->doConvertBooleans(
$item, $item,
function ($boolean) { function ($boolean) {
return (int) $boolean; return null === $boolean ? null : (int) $boolean;
} }
); );
} }
......
...@@ -24,6 +24,7 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -24,6 +24,7 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
try { try {
$this->_conn->exec('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL);'); $this->_conn->exec('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL);');
$this->_conn->exec('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN);');
} catch (DBALException $e) { } catch (DBALException $e) {
} }
$this->running = true; $this->running = true;
...@@ -88,4 +89,92 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -88,4 +89,92 @@ class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
$this->assertFalse($row['bool_col']); $this->assertFalse($row['bool_col']);
} }
/**
* @dataProvider booleanTypeConversionWithoutPdoTypeProvider
*/
public function testBooleanConversionNullParamEmulatedPrepares(
$statementValue,
$databaseConvertedValue
) {
$this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
// PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT is deprecated in php 5.6. PDO::ATTR_EMULATE_PREPARES should
// be used instead. so should only it be set when it is supported.
if (PHP_VERSION_ID < 50600) {
$this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
}
$platform = $this->_conn->getDatabasePlatform();
$stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
$stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue));
$stmt->execute();
$id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
$this->assertNotEmpty($id);
$row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
$this->assertSame($databaseConvertedValue, $row['bool_col']);
}
/**
* @dataProvider booleanTypeConversionUsingBooleanTypeProvider
*/
public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInBindValue(
$statementValue,
$databaseConvertedValue
) {
$this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
// PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT is deprecated in php 5.6. PDO::ATTR_EMULATE_PREPARES should
// be used instead. so should only it be set when it is supported.
if (PHP_VERSION_ID < 50600) {
$this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
}
$platform = $this->_conn->getDatabasePlatform();
$stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
$stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue), PDO::PARAM_BOOL);
$stmt->execute();
$id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
$this->assertNotEmpty($id);
$row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
$this->assertSame($databaseConvertedValue, $row['bool_col']);
}
/**
* Boolean conversion mapping provider
* @return array
*/
public function booleanTypeConversionUsingBooleanTypeProvider()
{
return array(
// statement value, database converted value result
array(true, true),
array(false, false),
array(null, false)
);
}
/**
* Boolean conversion mapping provider
* @return array
*/
public function booleanTypeConversionWithoutPdoTypeProvider()
{
return array(
// statement value, database converted value result
array(true, true),
array(false, false),
array(null, null)
);
}
} }
...@@ -602,6 +602,8 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -602,6 +602,8 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
array('no', 'false', 0, false), array('no', 'false', 0, false),
array('off', 'false', 0, false), array('off', 'false', 0, false),
array('0', 'false', 0, false), array('0', 'false', 0, false),
array(null, 'NULL', null, null)
); );
} }
......
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