Commit 08b83087 authored by Davi Koscianski Vidal's avatar Davi Koscianski Vidal Committed by lucasvanlierop

DRYing up tests and abstraction layer

parent 4090136a
...@@ -754,28 +754,16 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -754,28 +754,16 @@ class PostgreSqlPlatform extends AbstractPlatform
return parent::convertBooleansToDatabaseValue($item); return parent::convertBooleansToDatabaseValue($item);
} }
$item = $this->convertBooleans($item);
if (is_array($item)) { if (is_array($item)) {
foreach ($item as $key => $value) { $item = array_map(
if (is_bool($value) || is_numeric($value)) { function ($element) {
$item[$key] = $value ? 1 : 0; return (int) ('true' === $element);
} elseif (is_string($value)) { },
if (in_array(trim(strtolower($item)), $this->booleanLiterals['false'])) { $item
$item[$key] = 0; );
} else {
$item[$key] = 1;
}
}
}
} else {
if (is_bool($item) || is_numeric($item)) {
$item = $item ? 1 : 0;
} elseif (is_string($item)) {
if (in_array(trim(strtolower($item)), $this->booleanLiterals['false'])) {
$item = 0;
} else { } else {
$item = 1; $item = (int) ('true' === $item);
}
}
} }
return $item; return $item;
......
...@@ -290,27 +290,13 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -290,27 +290,13 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
/** /**
* @group DBAL-457 * @group DBAL-457
* @dataProvider pgStringBooleanProvider
*/ */
public function testConvertBooleanAsLiteralStrings() public function testConvertBooleanAsLiteralStrings($expected, $input)
{ {
$platform = $this->createPlatform(); $platform = $this->createPlatform();
$this->assertEquals('true', $platform->convertBooleans(true)); $this->assertEquals($expected, $platform->convertBooleans($input));
$this->assertEquals('true', $platform->convertBooleans('t'));
$this->assertEquals('true', $platform->convertBooleans('true'));
$this->assertEquals('true', $platform->convertBooleans('y'));
$this->assertEquals('true', $platform->convertBooleans('yes'));
$this->assertEquals('true', $platform->convertBooleans('on'));
$this->assertEquals('true', $platform->convertBooleans('1'));
$this->assertEquals('false', $platform->convertBooleans(false));
$this->assertEquals('false', $platform->convertBooleans('f'));
$this->assertEquals('false', $platform->convertBooleans('false'));
$this->assertEquals('false', $platform->convertBooleans('n'));
$this->assertEquals('false', $platform->convertBooleans('no'));
$this->assertEquals('false', $platform->convertBooleans('off'));
$this->assertEquals('false', $platform->convertBooleans('0'));
} }
/** /**
...@@ -321,19 +307,22 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -321,19 +307,22 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
$platform = $this->createPlatform(); $platform = $this->createPlatform();
$platform->setUseBooleanTrueFalseStrings(false); $platform->setUseBooleanTrueFalseStrings(false);
$this->assertEquals('1', $platform->convertBooleans(true)); $this->assertEquals(1, $platform->convertBooleans(true));
$this->assertEquals('0', $platform->convertBooleans(false)); $this->assertEquals(1, $platform->convertBooleans('1'));
$this->assertEquals(0, $platform->convertBooleans(false));
$this->assertEquals(0, $platform->convertBooleans('0'));
} }
/** /**
* @group DBAL-630 * @group DBAL-630
* @dataProvider pgStringBooleanDatabaseValueProvider
*/ */
public function testConvertBooleanAsDatabaseValueStrings() public function testConvertBooleanAsDatabaseValueStrings($expected, $input)
{ {
$platform = $this->createPlatform(); $platform = $this->createPlatform();
$this->assertEquals(1, $platform->convertBooleansToDatabaseValue(true)); $this->assertEquals($expected, $platform->convertBooleansToDatabaseValue($input));
$this->assertEquals(0, $platform->convertBooleansToDatabaseValue(false));
} }
/** /**
...@@ -588,6 +577,56 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -588,6 +577,56 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
); );
} }
/**
* PostgreSQL boolean strings provider
* @return array
*/
public function pgStringBooleanProvider()
{
return array(
array('true', true),
array('true', 't'),
array('true', 'true'),
array('true', 'y'),
array('true', 'yes'),
array('true', 'on'),
array('true', '1'),
array('false', false),
array('false', 'f'),
array('false', 'false'),
array('false', 'n'),
array('false', 'no'),
array('false', 'off'),
array('false', '0'),
);
}
/**
* PostgreSQL boolean strings as database values provider
* @return array
*/
public function pgStringBooleanDatabaseValueProvider()
{
return array(
array(1, true),
array(1, 't'),
array(1, 'true'),
array(1, 'y'),
array(1, 'yes'),
array(1, 'on'),
array(1, '1'),
array(0, false),
array(0, 'f'),
array(0, 'false'),
array(0, 'n'),
array(0, 'no'),
array(0, 'off'),
array(0, '0'),
);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
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