Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Functional / TypeConversionTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Functional;
4
5 use Doctrine\DBAL\Types\Type;
6
7 require_once __DIR__ . '/../../TestInit.php';
8
9 class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
10 {
11     static private $typeCounter = 0;
12
13     public function setUp()
14     {
15         parent::setUp();
16
17         /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
18         $sm = $this->_conn->getSchemaManager();
19
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'));
36
37         try {
38             foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
39                 $this->_conn->executeQuery($sql);
40             }
41         } catch(\Exception $e) {
42
43         }
44     }
45
46     static public function dataIdempotentDataConversion()
47     {
48         $obj = new \stdClass();
49         $obj->foo = "bar";
50         $obj->bar = "baz";
51
52         return array(
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'),
67         );
68     }
69
70     /**
71      * @dataProvider dataIdempotentDataConversion
72      * @param string $type
73      * @param mixed $originalValue
74      * @param string $expectedPhpType
75      */
76     public function testIdempotentDataConversion($type, $originalValue, $expectedPhpType)
77     {
78         $columnName = "test_" . $type;
79         $typeInstance = Type::getType($type);
80         $insertionValue = $typeInstance->convertToDatabaseValue($originalValue, $this->_conn->getDatabasePlatform());
81
82         $this->_conn->insert('type_conversion', array('id' => ++self::$typeCounter, $columnName => $insertionValue));
83
84         $sql = "SELECT " . $columnName . " FROM type_conversion WHERE id = " . self::$typeCounter;
85         $actualDbValue = $typeInstance->convertToPHPValue($this->_conn->fetchColumn($sql), $this->_conn->getDatabasePlatform());
86
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);
89         } else {
90             $this->assertInternalType($expectedPhpType, $actualDbValue, "The expected type from the conversion to and back from the database should be " . $expectedPhpType);
91         }
92
93         if ($type !== "datetimetz") {
94             $this->assertEquals($originalValue, $actualDbValue, "Conversion between values should produce the same out as in value, but doesnt!");
95
96             if ($originalValue instanceof \DateTime) {
97                 $this->assertEquals($originalValue->getTimezone()->getName(), $actualDbValue->getTimezone()->getName(), "Timezones should be the same.");
98             }
99         }
100     }
101 }