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 7 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 @@ -723,7 +723,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 @@ -787,6 +787,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 @@ -804,7 +808,7 @@ public function convertBooleansToDatabaseValue($item)
return $this->doConvertBooleans(
$item,
function ($boolean) {
return (int) $boolean;
return null === $boolean ? null : (int) $boolean;
}
);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,31 @@ public function testBooleanConversionBoolParamEmulatedPrepares()

$this->assertFalse($row['bool_col']);
}

public function testBooleanConversionNullParamEmulatedPrepares()
{
$this->_conn->exec('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN);');
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure but I think you have to move the table creation statement to the setUp() method to avoid errors if the table already exists (like if you run the test suite a second time).


$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(null));
$stmt->execute();

$id = $this->_conn->lastInsertId('dbal630_id_seq');

$this->assertNotEmpty($id);

$row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));

$this->assertNull($row['bool_col']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,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