Skip to content

Commit

Permalink
[2.0] Testing all dbal types and making sure they are fully implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Jun 20, 2009
1 parent ab2b399 commit 78d4309
Show file tree
Hide file tree
Showing 22 changed files with 295 additions and 53 deletions.
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,11 @@ public function getTimeFormatString()
*/
abstract public function getVarcharTypeDeclarationSql(array $field);

public function getBooleanTypeDeclarationSql(array $field)
{
return $this->getIntegerTypeDeclarationSql($field);
}

/**
* Get the platform name for this instance
*
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,14 @@ public function getDateTimeTypeDeclarationSql(array $fieldDeclaration)
return 'CHAR(' . strlen('YYYY-MM-DD HH:MM:SS') . ')';
}

/**
* @override
*/
public function getBooleanTypeDeclarationSql(array $field)
{
return 'BIT';
}

/**
* Get the platform name for this instance
*
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ public function getDateTimeTypeDeclarationSql(array $fieldDeclaration)
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.
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ public function getDateTimeTypeDeclarationSql(array $fieldDeclaration)
return 'DATE';
}

/**
* @override
*/
public function getBooleanTypeDeclarationSql(array $field)
{
return 'NUMBER(1)';
}

/**
* @override
*/
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ public function getDateTimeTypeDeclarationSql(array $fieldDeclaration)
return 'TIMESTAMP without time zone';
}

/**
* @override
*/
public function getBooleanTypeDeclarationSql(array $field)
{
return 'BOOLEAN';
}

/**
* @override
*/
Expand Down
17 changes: 16 additions & 1 deletion lib/Doctrine/DBAL/Types/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Types/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions lib/Doctrine/DBAL/Types/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
*/
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';
}
}
13 changes: 0 additions & 13 deletions lib/Doctrine/DBAL/Types/CharType.php

This file was deleted.

13 changes: 0 additions & 13 deletions lib/Doctrine/DBAL/Types/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,16 @@ public function getName()
return 'DateTime';
}

/**
* {@inheritdoc}
*/
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return $platform->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);
Expand Down
13 changes: 0 additions & 13 deletions lib/Doctrine/DBAL/Types/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,16 @@ public function getName()
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);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Types/DecimalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Types/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions lib/Doctrine/DBAL/Types/ObjectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Doctrine\DBAL\Types;

/**
* Type that maps a PHP object to a clob SQL type.
*
* @since 2.0
*/
class ObjectType 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 'Object';
}
}
3 changes: 3 additions & 0 deletions lib/Doctrine/DBAL/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions tests/Doctrine/Tests/DBAL/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ public static function suite()
$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());

Expand Down
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/DBAL/Types/ArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Doctrine\Tests\DBAL\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;

require_once __DIR__ . '/../../TestInit.php';

class ArrayTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;

protected function setUp()
{
$this->_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))
);
}
}
35 changes: 35 additions & 0 deletions tests/Doctrine/Tests/DBAL/Types/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Doctrine\Tests\DBAL\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;

require_once __DIR__ . '/../../TestInit.php';

class BooleanTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;

protected function setUp()
{
$this->_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))
);
}
}
Loading

0 comments on commit 78d4309

Please sign in to comment.