3 namespace Doctrine\Tests\DBAL\Types;
5 use Doctrine\DBAL\Types\Type;
6 use Doctrine\Tests\DBAL\Mocks;
8 require_once __DIR__ . '/../../TestInit.php';
10 class VarDateTimeTest extends \Doctrine\Tests\DbalTestCase
16 protected function setUp()
18 $this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
19 if (!Type::hasType('vardatetime')) {
20 Type::addType('vardatetime', 'Doctrine\DBAL\Types\VarDateTimeType');
22 $this->_type = Type::getType('vardatetime');
25 public function testDateTimeConvertsToDatabaseValue()
27 $date = new \DateTime('1985-09-01 10:10:10');
29 $expected = $date->format($this->_platform->getDateTimeTzFormatString());
30 $actual = $this->_type->convertToDatabaseValue($date, $this->_platform);
32 $this->assertEquals($expected, $actual);
35 public function testDateTimeConvertsToPHPValue()
37 // Birthday of jwage and also birthday of Doctrine. Send him a present ;)
38 $date = $this->_type->convertToPHPValue('1985-09-01 00:00:00', $this->_platform);
39 $this->assertInstanceOf('DateTime', $date);
40 $this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
41 $this->assertEquals('000000', $date->format('u'));
44 public function testInvalidDateTimeFormatConversion()
46 $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
47 $this->_type->convertToPHPValue('abcdefg', $this->_platform);
50 public function testConversionWithMicroseconds()
52 $date = $this->_type->convertToPHPValue('1985-09-01 00:00:00.123456', $this->_platform);
53 $this->assertInstanceOf('DateTime', $date);
54 $this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
55 $this->assertEquals('123456', $date->format('u'));
58 public function testNullConversion()
60 $this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
63 public function testConvertDateTimeToPHPValue()
65 $date = new \DateTime("now");
66 $this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform));