3 namespace Doctrine\Tests\DBAL\Functional;
5 use Doctrine\DBAL\Types\Type;
7 require_once __DIR__ . '/../../TestInit.php';
9 class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
11 static private $typeCounter = 0;
13 public function setUp()
17 /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
18 $sm = $this->_conn->getSchemaManager();
20 $table = new \Doctrine\DBAL\Schema\Table("type_conversion");
21 $table->addColumn('id', 'integer', array('notnull' => false));
22 $table->addColumn('test_string', 'string', array('notnull' => false));
23 $table->addColumn('test_boolean', 'boolean', array('notnull' => false));
24 $table->addColumn('test_bigint', 'bigint', array('notnull' => false));
25 $table->addColumn('test_smallint', 'bigint', array('notnull' => false));
26 $table->addColumn('test_datetime', 'datetime', array('notnull' => false));
27 $table->addColumn('test_datetimetz', 'datetimetz', array('notnull' => false));
28 $table->addColumn('test_date', 'date', array('notnull' => false));
29 $table->addColumn('test_time', 'time', array('notnull' => false));
30 $table->addColumn('test_text', 'text', array('notnull' => false));
31 $table->addColumn('test_array', 'array', array('notnull' => false));
32 $table->addColumn('test_object', 'object', array('notnull' => false));
33 $table->addColumn('test_float', 'float', array('notnull' => false));
34 $table->addColumn('test_decimal', 'decimal', array('notnull' => false, 'scale' => 2, 'precision' => 10));
35 $table->setPrimaryKey(array('id'));
38 foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
39 $this->_conn->executeQuery($sql);
41 } catch(\Exception $e) {
46 static public function dataIdempotentDataConversion()
48 $obj = new \stdClass();
53 array('string', 'ABCDEFGaaaBBB', 'string'),
54 array('boolean', true, 'bool'),
55 array('boolean', false, 'bool'),
56 array('bigint', 12345678, 'string'),
57 array('smallint', 123, 'int'),
58 array('datetime', new \DateTime('2010-04-05 10:10:10'), 'DateTime'),
59 array('datetimetz', new \DateTime('2010-04-05 10:10:10'), 'DateTime'),
60 array('date', new \DateTime('2010-04-05'), 'DateTime'),
61 array('time', new \DateTime('10:10:10'), 'DateTime'),
62 array('text', str_repeat('foo ', 1000), 'string'),
63 array('array', array('foo' => 'bar'), 'array'),
64 array('object', $obj, 'object'),
65 array('float', 1.5, 'float'),
66 array('decimal', 1.55, 'string'),
71 * @dataProvider dataIdempotentDataConversion
73 * @param mixed $originalValue
74 * @param string $expectedPhpType
76 public function testIdempotentDataConversion($type, $originalValue, $expectedPhpType)
78 $columnName = "test_" . $type;
79 $typeInstance = Type::getType($type);
80 $insertionValue = $typeInstance->convertToDatabaseValue($originalValue, $this->_conn->getDatabasePlatform());
82 $this->_conn->insert('type_conversion', array('id' => ++self::$typeCounter, $columnName => $insertionValue));
84 $sql = "SELECT " . $columnName . " FROM type_conversion WHERE id = " . self::$typeCounter;
85 $actualDbValue = $typeInstance->convertToPHPValue($this->_conn->fetchColumn($sql), $this->_conn->getDatabasePlatform());
87 if ($originalValue instanceof \DateTime) {
88 $this->assertInstanceOf($expectedPhpType, $actualDbValue, "The expected type from the conversion to and back from the database should be " . $expectedPhpType);
90 $this->assertInternalType($expectedPhpType, $actualDbValue, "The expected type from the conversion to and back from the database should be " . $expectedPhpType);
93 if ($type !== "datetimetz") {
94 $this->assertEquals($originalValue, $actualDbValue, "Conversion between values should produce the same out as in value, but doesnt!");
96 if ($originalValue instanceof \DateTime) {
97 $this->assertEquals($originalValue->getTimezone()->getName(), $actualDbValue->getTimezone()->getName(), "Timezones should be the same.");