/tests/Doctrine/Tests/DBAL/Functional/TypeConversionTest.php

https://github.com/mrtorrent/dbal · PHP · 101 lines · 77 code · 17 blank · 7 comment · 4 complexity · 37b2f214deb630e8b89f52cb9e99f318 MD5 · raw file

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