Commit 2161ffff authored by Bill Schaller's avatar Bill Schaller

Merge pull request #902 from Ocramius/hotfix/#869-fixes-for-type-conversion-excessive-smartness

Hotfix - #869 - fixes for type conversion logic
parents bd142a58 155f5d75
...@@ -41,7 +41,7 @@ class ConversionException extends \Doctrine\DBAL\DBALException ...@@ -41,7 +41,7 @@ class ConversionException extends \Doctrine\DBAL\DBALException
*/ */
static public function conversionFailed($value, $toType) static public function conversionFailed($value, $toType)
{ {
$value = (strlen($value) > 32) ? substr($value, 0, 20) . "..." : $value; $value = (strlen($value) > 32) ? substr($value, 0, 20) . '...' : $value;
return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType); return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType);
} }
...@@ -53,16 +53,19 @@ class ConversionException extends \Doctrine\DBAL\DBALException ...@@ -53,16 +53,19 @@ class ConversionException extends \Doctrine\DBAL\DBALException
* @param string $value * @param string $value
* @param string $toType * @param string $toType
* @param string $expectedFormat * @param string $expectedFormat
* @param \Exception|null $previous
* *
* @return \Doctrine\DBAL\Types\ConversionException * @return \Doctrine\DBAL\Types\ConversionException
*/ */
static public function conversionFailedFormat($value, $toType, $expectedFormat) static public function conversionFailedFormat($value, $toType, $expectedFormat, \Exception $previous = null)
{ {
$value = (strlen($value) > 32) ? substr($value, 0, 20) . "..." : $value; $value = (strlen($value) > 32) ? substr($value, 0, 20) . '...' : $value;
return new self( return new self(
'Could not convert database value "' . $value . '" to Doctrine Type ' . 'Could not convert database value "' . $value . '" to Doctrine Type ' .
$toType . '. Expected format: ' . $expectedFormat $toType . '. Expected format: ' . $expectedFormat,
0,
$previous
); );
} }
...@@ -70,24 +73,30 @@ class ConversionException extends \Doctrine\DBAL\DBALException ...@@ -70,24 +73,30 @@ class ConversionException extends \Doctrine\DBAL\DBALException
* Thrown when the PHP value passed to the converter was not of the expected type. * Thrown when the PHP value passed to the converter was not of the expected type.
* *
* @param mixed $value * @param mixed $value
* @param string $fromType * @param string $toType
* @param string[] $possibleTypes
* *
* @return \Doctrine\DBAL\Types\ConversionException * @return \Doctrine\DBAL\Types\ConversionException
*/ */
static public function conversionFailedInvalidType($value, $toType, $fromType) static public function conversionFailedInvalidType($value, $toType, array $possibleTypes)
{ {
$actualType = gettype($value); $actualType = is_object($value) ? get_class($value) : gettype($value);
if ($actualType === 'object') {
$actualType .= " (" . get_class($value) . ")"; if (is_scalar($value)) {
if (!method_exists($value, '__toString')) { return new self(sprintf(
$value = 'object'; "Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
} $value,
$actualType,
$toType,
implode(', ', $possibleTypes)
));
} }
$value = (string)$value;
$value = (strlen($value) > 32) ? substr($value, 0, 20) . "..." : $value;
return new self( return new self(sprintf(
"Could not convert PHP value '$value' of type '$actualType' to type $toType. Expected type: $fromType" "Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
); $actualType,
$toType,
implode(', ', $possibleTypes)
));
} }
} }
...@@ -33,20 +33,17 @@ class DateIntervalType extends Type ...@@ -33,20 +33,17 @@ class DateIntervalType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
$spec = null; if (null === $value) {
if ($value !== null) { return null;
/** @var \DateInterval $value */ }
$spec = 'P'
if ($value instanceof \DateInterval) {
return 'P'
. str_pad($value->y, 4, '0', STR_PAD_LEFT) . '-' . str_pad($value->y, 4, '0', STR_PAD_LEFT) . '-'
. $value->format('%M') . '-' . $value->format('%M-%DT%H:%I:%S');
. $value->format('%D') . 'T'
. $value->format('%H') . ':'
. $value->format('%I') . ':'
. $value->format('%S')
;
} }
return $spec; throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']);
} }
/** /**
...@@ -59,13 +56,10 @@ class DateIntervalType extends Type ...@@ -59,13 +56,10 @@ class DateIntervalType extends Type
} }
try { try {
$interval = new \DateInterval($value); return new \DateInterval($value);
} catch (\Exception $e) { } catch (\Exception $exception) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s'); throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s', $exception);
} }
return $interval;
} }
/** /**
......
...@@ -49,12 +49,15 @@ class DateTimeType extends Type ...@@ -49,12 +49,15 @@ class DateTimeType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getDateTimeFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
? $value->format($platform->getDateTimeFormatString()) : null;
} }
/** /**
......
...@@ -67,12 +67,15 @@ class DateTimeTzType extends Type ...@@ -67,12 +67,15 @@ class DateTimeTzType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getDateTimeTzFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
? $value->format($platform->getDateTimeTzFormatString()) : null;
} }
/** /**
......
...@@ -49,12 +49,15 @@ class DateType extends Type ...@@ -49,12 +49,15 @@ class DateType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getDateFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
? $value->format($platform->getDateFormatString()) : null;
} }
/** /**
......
...@@ -49,12 +49,15 @@ class TimeType extends Type ...@@ -49,12 +49,15 @@ class TimeType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) { if (null === $value) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime"); return $value;
}
if ($value instanceof \DateTime) {
return $value->format($platform->getTimeFormatString());
} }
return ($value !== null) throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateTime']);
? $value->format($platform->getTimeFormatString()) : null;
} }
/** /**
......
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
use PHPUnit_Framework_TestCase;
abstract class BaseDateTypeTestCase extends PHPUnit_Framework_TestCase
{
/**
* @var MockPlatform
*/
protected $platform;
/**
* @var \Doctrine\DBAL\Types\Type
*/
protected $type;
/**
* @var string
*/
private $currentTimezone;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->platform = new MockPlatform();
$this->currentTimezone = date_default_timezone_get();
$this->assertInstanceOf('Doctrine\DBAL\Types\Type', $this->type);
}
/**
* {@inheritDoc}
*/
protected function tearDown()
{
date_default_timezone_set($this->currentTimezone);
}
public function testDateConvertsToDatabaseValue()
{
$this->assertInternalType('string', $this->type->convertToDatabaseValue(new \DateTime(), $this->platform));
}
/**
* @dataProvider invalidPHPValuesProvider
*
* @param mixed $value
*/
public function testInvalidTypeConversionToDatabaseValue($value)
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->type->convertToDatabaseValue($value, $this->platform);
}
public function testNullConversion()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}
public function testConvertDateTimeToPHPValue()
{
$date = new \DateTime('now');
$this->assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
}
/**
* @return mixed[][]
*/
public function invalidPHPValuesProvider()
{
return [
[0],
[''],
['foo'],
['10:11:12'],
['2015-01-31'],
['2015-01-31 10:11:12'],
[new \stdClass()],
[$this],
[27],
[-1],
[1.2],
[[]],
[['an array']],
];
}
}
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\ConversionException;
use PHPUnit_Framework_TestCase;
class ConversionExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider scalarsProvider
*
* @param mixed $scalarValue
*/
public function testConversionFailedInvalidTypeWithScalar($scalarValue)
{
$exception = ConversionException::conversionFailedInvalidType($scalarValue, 'foo', ['bar', 'baz']);
$this->assertInstanceOf('Doctrine\DBAL\Types\ConversionException', $exception);
$this->assertRegExp(
'/^Could not convert PHP value \'.*\' of type \'(string|boolean|float|double|integer)\' to type \'foo\'. '
. 'Expected one of the following types: bar, baz$/',
$exception->getMessage()
);
}
/**
* @dataProvider nonScalarsProvider
*
* @param mixed $nonScalar
*/
public function testConversionFailedInvalidTypeWithNonScalar($nonScalar)
{
$exception = ConversionException::conversionFailedInvalidType($nonScalar, 'foo', ['bar', 'baz']);
$this->assertInstanceOf('Doctrine\DBAL\Types\ConversionException', $exception);
$this->assertRegExp(
'/^Could not convert PHP value of type \'(.*)\' to type \'foo\'. '
. 'Expected one of the following types: bar, baz$/',
$exception->getMessage()
);
}
public function testConversionFailedFormatPreservesPreviousException()
{
$previous = new \Exception();
$exception = ConversionException::conversionFailedFormat('foo', 'bar', 'baz', $previous);
$this->assertInstanceOf('Doctrine\DBAL\Types\ConversionException', $exception);
$this->assertSame($previous, $exception->getPrevious());
}
/**
* @return mixed[][]
*/
public function nonScalarsProvider()
{
return [
[[]],
[['foo']],
[null],
[$this],
[new \stdClass()],
[tmpfile()],
];
}
/**
* @return mixed[][]
*/
public function scalarsProvider()
{
return [
[''],
['foo'],
[123],
[-123],
[12.34],
[true],
[false],
];
}
}
...@@ -7,14 +7,25 @@ use Doctrine\Tests\DBAL\Mocks\MockPlatform; ...@@ -7,14 +7,25 @@ use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateIntervalTest extends \Doctrine\Tests\DbalTestCase class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
{ {
protected /**
$_platform, * @var MockPlatform
$_type; */
private $platform;
/**
* @var \Doctrine\DBAL\Types\DateIntervalType
*/
private $type;
/**
* {@inheritDoc}
*/
protected function setUp() protected function setUp()
{ {
$this->_platform = new MockPlatform(); $this->platform = new MockPlatform();
$this->_type = Type::getType('dateinterval'); $this->type = Type::getType('dateinterval');
$this->assertInstanceOf('Doctrine\DBAL\Types\DateIntervalType', $this->type);
} }
public function testDateIntervalConvertsToDatabaseValue() public function testDateIntervalConvertsToDatabaseValue()
...@@ -22,14 +33,14 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase ...@@ -22,14 +33,14 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
$interval = new \DateInterval('P2Y1DT1H2M3S'); $interval = new \DateInterval('P2Y1DT1H2M3S');
$expected = 'P0002-00-01T01:02:03'; $expected = 'P0002-00-01T01:02:03';
$actual = $this->_type->convertToDatabaseValue($interval, $this->_platform); $actual = $this->type->convertToDatabaseValue($interval, $this->platform);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testDateIntervalConvertsToPHPValue() public function testDateIntervalConvertsToPHPValue()
{ {
$date = $this->_type->convertToPHPValue('P0002-00-01T01:02:03', $this->_platform); $date = $this->type->convertToPHPValue('P0002-00-01T01:02:03', $this->platform);
$this->assertInstanceOf('DateInterval', $date); $this->assertInstanceOf('DateInterval', $date);
$this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS')); $this->assertEquals('P2Y0M1DT1H2M3S', $date->format('P%yY%mM%dDT%hH%iM%sS'));
} }
...@@ -37,12 +48,12 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase ...@@ -37,12 +48,12 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidDateIntervalFormatConversion() public function testInvalidDateIntervalFormatConversion()
{ {
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->type->convertToPHPValue('abcdefg', $this->platform);
} }
public function testDateIntervalNullConversion() public function testDateIntervalNullConversion()
{ {
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform)); $this->assertNull($this->type->convertToPHPValue(null, $this->platform));
} }
/** /**
...@@ -50,6 +61,41 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase ...@@ -50,6 +61,41 @@ class DateIntervalTest extends \Doctrine\Tests\DbalTestCase
*/ */
public function testRequiresSQLCommentHint() public function testRequiresSQLCommentHint()
{ {
$this->assertTrue($this->_type->requiresSQLCommentHint($this->_platform)); $this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
/**
* @dataProvider invalidPHPValuesProvider
*
* @param mixed $value
*/
public function testInvalidTypeConversionToDatabaseValue($value)
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->type->convertToDatabaseValue($value, $this->platform);
}
/**
* @return mixed[][]
*/
public function invalidPHPValuesProvider()
{
return [
[0],
[''],
['foo'],
['10:11:12'],
['2015-01-31'],
['2015-01-31 10:11:12'],
[new \stdClass()],
[$this],
[27],
[-1],
[1.2],
[[]],
[['an array']],
[new \DateTime()],
];
} }
} }
...@@ -3,64 +3,31 @@ ...@@ -3,64 +3,31 @@
namespace Doctrine\Tests\DBAL\Types; namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateTest extends \Doctrine\Tests\DbalTestCase class DateTest extends BaseDateTypeTestCase
{ {
protected
$_platform,
$_type,
$_tz;
protected function setUp()
{
$this->_platform = new MockPlatform();
$this->_type = Type::getType('date');
$this->_tz = date_default_timezone_get();
}
public function tearDown()
{
date_default_timezone_set($this->_tz);
}
public function testDateConvertsToDatabaseValue()
{
$this->assertTrue(
is_string($this->_type->convertToDatabaseValue(new \DateTime(), $this->_platform))
);
}
/** /**
* @expectedException \Doctrine\DBAL\Types\ConversionException * {@inheritDoc}
*/ */
public function testInvalidDateTimeValueInteger() protected function setUp()
{ {
$date = 27; $this->type = Type::getType('date');
$this->_type->convertToDatabaseValue($date, $this->_platform);
}
/** parent::setUp();
* @expectedException \Doctrine\DBAL\Types\ConversionException
*/
public function testInvalidDateTimeValueStdClass()
{
$date = new \stdClass();
$this->_type->convertToDatabaseValue($date, $this->_platform);
} }
public function testDateConvertsToPHPValue() public function testDateConvertsToPHPValue()
{ {
// Birthday of jwage and also birthday of Doctrine. Send him a present ;) // Birthday of jwage and also birthday of Doctrine. Send him a present ;)
$this->assertTrue( $this->assertTrue(
$this->_type->convertToPHPValue('1985-09-01', $this->_platform) $this->type->convertToPHPValue('1985-09-01', $this->platform)
instanceof \DateTime instanceof \DateTime
); );
} }
public function testDateResetsNonDatePartsToZeroUnixTimeValues() public function testDateResetsNonDatePartsToZeroUnixTimeValues()
{ {
$date = $this->_type->convertToPHPValue('1985-09-01', $this->_platform); $date = $this->type->convertToPHPValue('1985-09-01', $this->platform);
$this->assertEquals('00:00:00', $date->format('H:i:s')); $this->assertEquals('00:00:00', $date->format('H:i:s'));
} }
...@@ -69,11 +36,11 @@ class DateTest extends \Doctrine\Tests\DbalTestCase ...@@ -69,11 +36,11 @@ class DateTest extends \Doctrine\Tests\DbalTestCase
{ {
date_default_timezone_set('Europe/Berlin'); date_default_timezone_set('Europe/Berlin');
$date = $this->_type->convertToPHPValue('2009-08-01', $this->_platform); $date = $this->type->convertToPHPValue('2009-08-01', $this->platform);
$this->assertEquals('00:00:00', $date->format('H:i:s')); $this->assertEquals('00:00:00', $date->format('H:i:s'));
$this->assertEquals('2009-08-01', $date->format('Y-m-d')); $this->assertEquals('2009-08-01', $date->format('Y-m-d'));
$date = $this->_type->convertToPHPValue('2009-11-01', $this->_platform); $date = $this->type->convertToPHPValue('2009-11-01', $this->platform);
$this->assertEquals('00:00:00', $date->format('H:i:s')); $this->assertEquals('00:00:00', $date->format('H:i:s'));
$this->assertEquals('2009-11-01', $date->format('Y-m-d')); $this->assertEquals('2009-11-01', $date->format('Y-m-d'));
} }
...@@ -81,17 +48,6 @@ class DateTest extends \Doctrine\Tests\DbalTestCase ...@@ -81,17 +48,6 @@ class DateTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidDateFormatConversion() public function testInvalidDateFormatConversion()
{ {
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->type->convertToPHPValue('abcdefg', $this->platform);
}
public function testNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
public function testConvertDateTimeToPHPValue()
{
$date = new \DateTime("now");
$this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform));
} }
} }
...@@ -2,54 +2,34 @@ ...@@ -2,54 +2,34 @@
namespace Doctrine\Tests\DBAL\Types; namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateTimeTest extends \Doctrine\Tests\DbalTestCase class DateTimeTest extends BaseDateTypeTestCase
{ {
protected /**
$_platform, * {@inheritDoc}
$_type; */
protected function setUp() protected function setUp()
{ {
$this->_platform = new MockPlatform(); $this->type = Type::getType('datetime');
$this->_type = Type::getType('datetime');
parent::setUp();
} }
public function testDateTimeConvertsToDatabaseValue() public function testDateTimeConvertsToDatabaseValue()
{ {
$date = new \DateTime('1985-09-01 10:10:10'); $date = new \DateTime('1985-09-01 10:10:10');
$expected = $date->format($this->_platform->getDateTimeTzFormatString()); $expected = $date->format($this->platform->getDateTimeTzFormatString());
$actual = $this->_type->convertToDatabaseValue($date, $this->_platform); $actual = $this->type->convertToDatabaseValue($date, $this->platform);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/**
* @expectedException \Doctrine\DBAL\Types\ConversionException
*/
public function testInvalidDateTimeValueInteger()
{
$date = 27;
$this->_type->convertToDatabaseValue($date, $this->_platform);
}
/**
* @expectedException \Doctrine\DBAL\Types\ConversionException
*/
public function testInvalidDateTimeValueStdClass()
{
$date = new \stdClass();
$this->_type->convertToDatabaseValue($date, $this->_platform);
}
public function testDateTimeConvertsToPHPValue() public function testDateTimeConvertsToPHPValue()
{ {
// Birthday of jwage and also birthday of Doctrine. Send him a present ;) // Birthday of jwage and also birthday of Doctrine. Send him a present ;)
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00', $this->_platform); $date = $this->type->convertToPHPValue('1985-09-01 00:00:00', $this->platform);
$this->assertInstanceOf('DateTime', $date); $this->assertInstanceOf('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s')); $this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
} }
...@@ -57,25 +37,14 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -57,25 +37,14 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidDateTimeFormatConversion() public function testInvalidDateTimeFormatConversion()
{ {
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->type->convertToPHPValue('abcdefg', $this->platform);
}
public function testNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
public function testConvertDateTimeToPHPValue()
{
$date = new \DateTime("now");
$this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform));
} }
public function testConvertsNonMatchingFormatToPhpValueWithParser() public function testConvertsNonMatchingFormatToPhpValueWithParser()
{ {
$date = '1985/09/01 10:10:10.12345'; $date = '1985/09/01 10:10:10.12345';
$actual = $this->_type->convertToPHPValue($date, $this->_platform); $actual = $this->type->convertToPHPValue($date, $this->platform);
$this->assertEquals('1985-09-01 10:10:10', $actual->format('Y-m-d H:i:s')); $this->assertEquals('1985-09-01 10:10:10', $actual->format('Y-m-d H:i:s'));
} }
......
...@@ -3,53 +3,33 @@ ...@@ -3,53 +3,33 @@
namespace Doctrine\Tests\DBAL\Types; namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase class DateTimeTzTest extends BaseDateTypeTestCase
{ {
protected /**
$_platform, * {@inheritDoc}
$_type; */
protected function setUp() protected function setUp()
{ {
$this->_platform = new MockPlatform(); $this->type = Type::getType('datetimetz');
$this->_type = Type::getType('datetimetz');
parent::setUp();
} }
public function testDateTimeConvertsToDatabaseValue() public function testDateTimeConvertsToDatabaseValue()
{ {
$date = new \DateTime('1985-09-01 10:10:10'); $date = new \DateTime('1985-09-01 10:10:10');
$expected = $date->format($this->_platform->getDateTimeTzFormatString()); $expected = $date->format($this->platform->getDateTimeTzFormatString());
$actual = $this->_type->convertToDatabaseValue($date, $this->_platform); $actual = $this->type->convertToDatabaseValue($date, $this->platform);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
/**
* @expectedException \Doctrine\DBAL\Types\ConversionException
*/
public function testInvalidDateTimeValueInteger()
{
$date = 27;
$this->_type->convertToDatabaseValue($date, $this->_platform);
}
/**
* @expectedException \Doctrine\DBAL\Types\ConversionException
*/
public function testInvalidDateTimeValueStdClass()
{
$date = new \stdClass();
$this->_type->convertToDatabaseValue($date, $this->_platform);
}
public function testDateTimeConvertsToPHPValue() public function testDateTimeConvertsToPHPValue()
{ {
// Birthday of jwage and also birthday of Doctrine. Send him a present ;) // Birthday of jwage and also birthday of Doctrine. Send him a present ;)
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00', $this->_platform); $date = $this->type->convertToPHPValue('1985-09-01 00:00:00', $this->platform);
$this->assertInstanceOf('DateTime', $date); $this->assertInstanceOf('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s')); $this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
} }
...@@ -57,17 +37,6 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase ...@@ -57,17 +37,6 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidDateFormatConversion() public function testInvalidDateFormatConversion()
{ {
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->type->convertToPHPValue('abcdefg', $this->platform);
}
public function testNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
public function testConvertDateTimeToPHPValue()
{
$date = new \DateTime("now");
$this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform));
} }
} }
...@@ -3,56 +3,28 @@ ...@@ -3,56 +3,28 @@
namespace Doctrine\Tests\DBAL\Types; namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class TimeTest extends \Doctrine\Tests\DbalTestCase class TimeTest extends BaseDateTypeTestCase
{ {
protected
$_platform,
$_type;
protected function setUp()
{
$this->_platform = new MockPlatform();
$this->_type = Type::getType('time');
}
public function testTimeConvertsToDatabaseValue()
{
$this->assertTrue(
is_string($this->_type->convertToDatabaseValue(new \DateTime(), $this->_platform))
);
}
/** /**
* @expectedException \Doctrine\DBAL\Types\ConversionException * {@inheritDoc}
*/ */
public function testInvalidDateTimeValueInteger() protected function setUp()
{ {
$date = 27; $this->type = Type::getType('time');
$this->_type->convertToDatabaseValue($date, $this->_platform);
}
/** parent::setUp();
* @expectedException \Doctrine\DBAL\Types\ConversionException
*/
public function testInvalidDateTimeValueStdClass()
{
$date = new \stdClass();
$this->_type->convertToDatabaseValue($date, $this->_platform);
} }
public function testTimeConvertsToPHPValue() public function testTimeConvertsToPHPValue()
{ {
$this->assertTrue( $this->assertInstanceOf('DateTime', $this->type->convertToPHPValue('5:30:55', $this->platform));
$this->_type->convertToPHPValue('5:30:55', $this->_platform)
instanceof \DateTime
);
} }
public function testDateFieldResetInPHPValue() public function testDateFieldResetInPHPValue()
{ {
$time = $this->_type->convertToPHPValue('01:23:34', $this->_platform); $time = $this->type->convertToPHPValue('01:23:34', $this->platform);
$this->assertEquals('01:23:34', $time->format('H:i:s')); $this->assertEquals('01:23:34', $time->format('H:i:s'));
$this->assertEquals('1970-01-01', $time->format('Y-m-d')); $this->assertEquals('1970-01-01', $time->format('Y-m-d'));
} }
...@@ -60,17 +32,6 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -60,17 +32,6 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidTimeFormatConversion() public function testInvalidTimeFormatConversion()
{ {
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); $this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform); $this->type->convertToPHPValue('abcdefg', $this->platform);
}
public function testNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
public function testConvertDateTimeToPHPValue()
{
$date = new \DateTime("now");
$this->assertSame($date, $this->_type->convertToPHPValue($date, $this->_platform));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment