From 78d43097cab1674b551cf5701346835c5e625466 Mon Sep 17 00:00:00 2001 From: jwage Date: Sat, 20 Jun 2009 15:09:59 +0000 Subject: [PATCH] [2.0] Testing all dbal types and making sure they are fully implemented --- .../DBAL/Platforms/AbstractPlatform.php | 5 +++ lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php | 8 +++++ lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 8 +++++ .../DBAL/Platforms/OraclePlatform.php | 8 +++++ .../DBAL/Platforms/PostgreSqlPlatform.php | 8 +++++ lib/Doctrine/DBAL/Types/ArrayType.php | 17 ++++++++- lib/Doctrine/DBAL/Types/BigIntType.php | 2 +- lib/Doctrine/DBAL/Types/BooleanType.php | 20 +++++------ lib/Doctrine/DBAL/Types/CharType.php | 13 ------- lib/Doctrine/DBAL/Types/DateTimeType.php | 13 ------- lib/Doctrine/DBAL/Types/DateType.php | 13 ------- lib/Doctrine/DBAL/Types/DecimalType.php | 2 +- lib/Doctrine/DBAL/Types/IntegerType.php | 2 +- lib/Doctrine/DBAL/Types/ObjectType.php | 31 ++++++++++++++++ lib/Doctrine/DBAL/Types/Type.php | 3 ++ tests/Doctrine/Tests/DBAL/AllTests.php | 6 ++++ tests/Doctrine/Tests/DBAL/Types/ArrayTest.php | 35 +++++++++++++++++++ .../Doctrine/Tests/DBAL/Types/BooleanTest.php | 35 +++++++++++++++++++ .../Doctrine/Tests/DBAL/Types/DecimalTest.php | 28 +++++++++++++++ .../Doctrine/Tests/DBAL/Types/IntegerTest.php | 28 +++++++++++++++ .../Doctrine/Tests/DBAL/Types/ObjectTest.php | 35 +++++++++++++++++++ .../Tests/DBAL/Types/SmallIntTest.php | 28 +++++++++++++++ 22 files changed, 295 insertions(+), 53 deletions(-) delete mode 100644 lib/Doctrine/DBAL/Types/CharType.php create mode 100644 lib/Doctrine/DBAL/Types/ObjectType.php create mode 100644 tests/Doctrine/Tests/DBAL/Types/ArrayTest.php create mode 100644 tests/Doctrine/Tests/DBAL/Types/BooleanTest.php create mode 100644 tests/Doctrine/Tests/DBAL/Types/DecimalTest.php create mode 100644 tests/Doctrine/Tests/DBAL/Types/IntegerTest.php create mode 100644 tests/Doctrine/Tests/DBAL/Types/ObjectTest.php create mode 100644 tests/Doctrine/Tests/DBAL/Types/SmallIntTest.php diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index a2448a699..e2c576c6d 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -1529,6 +1529,11 @@ abstract class AbstractPlatform */ abstract public function getVarcharTypeDeclarationSql(array $field); + public function getBooleanTypeDeclarationSql(array $field) + { + return $this->getIntegerTypeDeclarationSql($field); + } + /** * Get the platform name for this instance * diff --git a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php index c025028bc..1c3f5a9f2 100644 --- a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php @@ -408,6 +408,14 @@ class MsSqlPlatform extends AbstractPlatform return 'CHAR(' . strlen('YYYY-MM-DD HH:MM:SS') . ')'; } + /** + * @override + */ + public function getBooleanTypeDeclarationSql(array $field) + { + return 'BIT'; + } + /** * Get the platform name for this instance * diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 1ee6583d2..81e0e4ce8 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -266,6 +266,14 @@ class MySqlPlatform extends AbstractPlatform return 'DATETIME'; } + /** + * @override + */ + public function getBooleanTypeDeclarationSql(array $field) + { + return 'TINYINT(1)'; + } + /** * Obtain DBMS specific SQL code portion needed to set the COLLATION * of a field declaration to be used in statements like CREATE TABLE. diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index a1955626b..8db4f2210 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -184,6 +184,14 @@ class OraclePlatform extends AbstractPlatform return 'DATE'; } + /** + * @override + */ + public function getBooleanTypeDeclarationSql(array $field) + { + return 'NUMBER(1)'; + } + /** * @override */ diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 5a136d053..05bb79343 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -698,6 +698,14 @@ class PostgreSqlPlatform extends AbstractPlatform return 'TIMESTAMP without time zone'; } + /** + * @override + */ + public function getBooleanTypeDeclarationSql(array $field) + { + return 'BOOLEAN'; + } + /** * @override */ diff --git a/lib/Doctrine/DBAL/Types/ArrayType.php b/lib/Doctrine/DBAL/Types/ArrayType.php index fd201aa34..c0a27a017 100644 --- a/lib/Doctrine/DBAL/Types/ArrayType.php +++ b/lib/Doctrine/DBAL/Types/ArrayType.php @@ -3,12 +3,27 @@ namespace Doctrine\DBAL\Types; /** - * Type that maps a PHP array to a VARCHAR SQL type. + * Type that maps a PHP array to a clob SQL type. * * @since 2.0 */ class ArrayType extends Type { + public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return $platform->getClobDeclarationSql($fieldDeclaration); + } + + public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return serialize($value); + } + + public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return unserialize($value); + } + public function getName() { return 'Array'; diff --git a/lib/Doctrine/DBAL/Types/BigIntType.php b/lib/Doctrine/DBAL/Types/BigIntType.php index 433ec6021..cf32bf37c 100644 --- a/lib/Doctrine/DBAL/Types/BigIntType.php +++ b/lib/Doctrine/DBAL/Types/BigIntType.php @@ -12,7 +12,7 @@ class BigIntType extends Type { public function getName() { - return "BigInteger"; + return 'BigInteger'; } public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) diff --git a/lib/Doctrine/DBAL/Types/BooleanType.php b/lib/Doctrine/DBAL/Types/BooleanType.php index ec50c98a3..415cead0b 100644 --- a/lib/Doctrine/DBAL/Types/BooleanType.php +++ b/lib/Doctrine/DBAL/Types/BooleanType.php @@ -9,23 +9,23 @@ namespace Doctrine\DBAL\Types; */ class BooleanType extends Type { - /** - * {@inheritdoc} - * - * @override - */ + public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return $platform->getBooleanDeclarationSql(); + } + public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return $platform->convertBooleans($value); } - /** - * {@inheritdoc} - * - * @override - */ public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return (bool) $value; } + + public function getName() + { + return 'boolean'; + } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Types/CharType.php b/lib/Doctrine/DBAL/Types/CharType.php deleted file mode 100644 index 133e10116..000000000 --- a/lib/Doctrine/DBAL/Types/CharType.php +++ /dev/null @@ -1,13 +0,0 @@ -getDateTimeTypeDeclarationSql($fieldDeclaration); } - /** - * {@inheritdoc} - * - * @override - */ public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return $value->format($platform->getDateTimeFormatString()); } - /** - * {@inheritdoc} - * - * @override - */ public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value); diff --git a/lib/Doctrine/DBAL/Types/DateType.php b/lib/Doctrine/DBAL/Types/DateType.php index 616a68772..7695e48c1 100644 --- a/lib/Doctrine/DBAL/Types/DateType.php +++ b/lib/Doctrine/DBAL/Types/DateType.php @@ -14,29 +14,16 @@ class DateType extends Type return 'Date'; } - /** - * {@inheritdoc} - */ public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return $platform->getDateTypeDeclarationSql($fieldDeclaration); } - /** - * {@inheritdoc} - * - * @override - */ public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return $value->format($platform->getDateFormatString()); } - /** - * {@inheritdoc} - * - * @override - */ public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return \DateTime::createFromFormat($platform->getDateFormatString(), $value); diff --git a/lib/Doctrine/DBAL/Types/DecimalType.php b/lib/Doctrine/DBAL/Types/DecimalType.php index 868773cc0..a4ff6d533 100644 --- a/lib/Doctrine/DBAL/Types/DecimalType.php +++ b/lib/Doctrine/DBAL/Types/DecimalType.php @@ -11,7 +11,7 @@ class DecimalType extends Type { public function getName() { - return "Decimal"; + return 'Decimal'; } public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) diff --git a/lib/Doctrine/DBAL/Types/IntegerType.php b/lib/Doctrine/DBAL/Types/IntegerType.php index 7bfd8dd2a..d3cecf1b5 100644 --- a/lib/Doctrine/DBAL/Types/IntegerType.php +++ b/lib/Doctrine/DBAL/Types/IntegerType.php @@ -10,7 +10,7 @@ class IntegerType extends Type { public function getName() { - return "Integer"; + return 'Integer'; } public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) diff --git a/lib/Doctrine/DBAL/Types/ObjectType.php b/lib/Doctrine/DBAL/Types/ObjectType.php new file mode 100644 index 000000000..fb168dc4a --- /dev/null +++ b/lib/Doctrine/DBAL/Types/ObjectType.php @@ -0,0 +1,31 @@ +getClobDeclarationSql($fieldDeclaration); + } + + public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return serialize($value); + } + + public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return unserialize($value); + } + + public function getName() + { + return 'Object'; + } +} \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Types/Type.php b/lib/Doctrine/DBAL/Types/Type.php index f38dccebd..07dc46d30 100644 --- a/lib/Doctrine/DBAL/Types/Type.php +++ b/lib/Doctrine/DBAL/Types/Type.php @@ -26,6 +26,9 @@ abstract class Type private static $_typeObjects = array(); private static $_typesMap = array( + 'array' => 'Doctrine\DBAL\Types\ArrayType', + 'object' => 'Doctrine\DBAL\Types\ObjectType', + 'boolean' => 'Doctrine\DBAL\Types\BooleanType', 'integer' => 'Doctrine\DBAL\Types\IntegerType', 'int' => 'Doctrine\DBAL\Types\IntegerType', 'smallint' => 'Doctrine\DBAL\Types\SmallIntType', diff --git a/tests/Doctrine/Tests/DBAL/AllTests.php b/tests/Doctrine/Tests/DBAL/AllTests.php index a6577040d..e1b5372cc 100644 --- a/tests/Doctrine/Tests/DBAL/AllTests.php +++ b/tests/Doctrine/Tests/DBAL/AllTests.php @@ -30,9 +30,15 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MsSqlPlatformTest'); // Type tests + $suite->addTestSuite('Doctrine\Tests\DBAL\Types\ArrayTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Types\ObjectTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\TimeTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Types\BooleanTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Types\DecimalTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Types\IntegerTest'); + $suite->addTestSuite('Doctrine\Tests\DBAL\Types\SmallIntTest'); $suite->addTest(Functional\AllTests::suite()); diff --git a/tests/Doctrine/Tests/DBAL/Types/ArrayTest.php b/tests/Doctrine/Tests/DBAL/Types/ArrayTest.php new file mode 100644 index 000000000..dc4a223a9 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Types/ArrayTest.php @@ -0,0 +1,35 @@ +_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); + $this->_type = Type::getType('array'); + } + + public function testArrayConvertsToDatabaseValue() + { + $this->assertTrue( + is_string($this->_type->convertToDatabaseValue(array(), $this->_platform)) + ); + } + + public function testArrayConvertsToPHPValue() + { + $this->assertTrue( + is_array($this->_type->convertToPHPValue(serialize(array()), $this->_platform)) + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Types/BooleanTest.php b/tests/Doctrine/Tests/DBAL/Types/BooleanTest.php new file mode 100644 index 000000000..1e9cf2a44 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Types/BooleanTest.php @@ -0,0 +1,35 @@ +_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); + $this->_type = Type::getType('boolean'); + } + + public function testBooleanConvertsToDatabaseValue() + { + $this->assertTrue( + is_integer($this->_type->convertToDatabaseValue(1, $this->_platform)) + ); + } + + public function testBooleanConvertsToPHPValue() + { + $this->assertTrue( + is_bool($this->_type->convertToPHPValue(0, $this->_platform)) + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Types/DecimalTest.php b/tests/Doctrine/Tests/DBAL/Types/DecimalTest.php new file mode 100644 index 000000000..ee5e2491a --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Types/DecimalTest.php @@ -0,0 +1,28 @@ +_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); + $this->_type = Type::getType('decimal'); + } + + public function testDecimalConvertsToPHPValue() + { + $this->assertTrue( + is_float($this->_type->convertToPHPValue('5.5', $this->_platform)) + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Types/IntegerTest.php b/tests/Doctrine/Tests/DBAL/Types/IntegerTest.php new file mode 100644 index 000000000..6d0efc5ed --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Types/IntegerTest.php @@ -0,0 +1,28 @@ +_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); + $this->_type = Type::getType('integer'); + } + + public function testDecimalConvertsToPHPValue() + { + $this->assertTrue( + is_integer($this->_type->convertToPHPValue('1', $this->_platform)) + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Types/ObjectTest.php b/tests/Doctrine/Tests/DBAL/Types/ObjectTest.php new file mode 100644 index 000000000..122844c22 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Types/ObjectTest.php @@ -0,0 +1,35 @@ +_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); + $this->_type = Type::getType('object'); + } + + public function testObjectConvertsToDatabaseValue() + { + $this->assertTrue( + is_string($this->_type->convertToDatabaseValue(new \stdClass(), $this->_platform)) + ); + } + + public function testObjectConvertsToPHPValue() + { + $this->assertTrue( + is_object($this->_type->convertToPHPValue(serialize(new \stdClass), $this->_platform)) + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Types/SmallIntTest.php b/tests/Doctrine/Tests/DBAL/Types/SmallIntTest.php new file mode 100644 index 000000000..8495d45f3 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Types/SmallIntTest.php @@ -0,0 +1,28 @@ +_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); + $this->_type = Type::getType('smallint'); + } + + public function testDecimalConvertsToPHPValue() + { + $this->assertTrue( + is_integer($this->_type->convertToPHPValue('1', $this->_platform)) + ); + } +} \ No newline at end of file