Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to verify null cast in boolean type #632

Merged
merged 17 commits into from
Oct 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
private function convertSingleBooleanValue($value, $callback)
{
if (null === $value) {
return $callback(false);
return $callback(null);
}

if (is_bool($value) || is_numeric($value)) {
Expand Down Expand Up @@ -812,6 +812,10 @@ public function convertBooleans($item)
return $this->doConvertBooleans(
$item,
function ($boolean) {
if (null === $boolean) {
return 'NULL';
}

return true === $boolean ? 'true' : 'false';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty newline before this line

}
);
Expand All @@ -829,7 +833,7 @@ public function convertBooleansToDatabaseValue($item)
return $this->doConvertBooleans(
$item,
function ($boolean) {
return (int) $boolean;
return null === $boolean ? null : (int) $boolean;
}
);
}
Expand Down
89 changes: 89 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected function setUp()

try {
$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) {
}
$this->running = true;
Expand Down Expand Up @@ -88,4 +89,92 @@ public function testBooleanConversionBoolParamEmulatedPrepares()

$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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this assumption is wrong and is exactly the issue I was talking about. Why should I expect to get false if I have a NULL value in the database? There is no distinction between NULL and false anymore. Is this maybe even a PDO issue of some kind?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't getting from the database, we are inserting into the database.
I'm just using the PDO default behavior. If we tell the PDO that the value is a boolean (PDO::PARAM_BOOL), isn't supposed that the NULL is converted to false? Why can't we rely on that assumption?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because "NULL" is neither "true" or "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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ public function pgBooleanProvider()
array('no', 'false', 0, false),
array('off', 'false', 0, false),
array('0', 'false', 0, false),

array(null, 'NULL', null, null)
);
}

Expand Down