Commit 3b50eb21 authored by Bill Schaller's avatar Bill Schaller

Add conversion exception when date and time type convertToDatabaseValue...

Add conversion exception when date and time type convertToDatabaseValue methods are passed non-null values that are not DateTime instances.
parent a0a43c0e
...@@ -65,4 +65,29 @@ class ConversionException extends \Doctrine\DBAL\DBALException ...@@ -65,4 +65,29 @@ class ConversionException extends \Doctrine\DBAL\DBALException
$toType . '. Expected format: ' . $expectedFormat $toType . '. Expected format: ' . $expectedFormat
); );
} }
/**
* Thrown when the PHP value passed to the converter was not of the expected type.
*
* @param mixed $value
* @param string $fromType
*
* @return \Doctrine\DBAL\Types\ConversionException
*/
static public function conversionFailedInvalidType($value, $toType, $fromType)
{
$actualType = gettype($value);
if ($actualType === 'object') {
$actualType .= " (" . get_class($value) . ")";
if (!method_exists($value, '__toString')) {
$value = 'object';
}
}
$value = (string)$value;
$value = (strlen($value) > 32) ? substr($value, 0, 20) . "..." : $value;
return new self(
"Could not convert PHP value '$value' of type '$actualType' to type $toType. Expected type: $fromType"
);
}
} }
...@@ -49,6 +49,10 @@ class DateTimeType extends Type ...@@ -49,6 +49,10 @@ class DateTimeType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null) return ($value !== null)
? $value->format($platform->getDateTimeFormatString()) : null; ? $value->format($platform->getDateTimeFormatString()) : null;
} }
......
...@@ -67,6 +67,10 @@ class DateTimeTzType extends Type ...@@ -67,6 +67,10 @@ class DateTimeTzType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null) return ($value !== null)
? $value->format($platform->getDateTimeTzFormatString()) : null; ? $value->format($platform->getDateTimeTzFormatString()) : null;
} }
......
...@@ -49,6 +49,10 @@ class DateType extends Type ...@@ -49,6 +49,10 @@ class DateType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null) return ($value !== null)
? $value->format($platform->getDateFormatString()) : null; ? $value->format($platform->getDateFormatString()) : null;
} }
......
...@@ -49,6 +49,10 @@ class TimeType extends Type ...@@ -49,6 +49,10 @@ class TimeType extends Type
*/ */
public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToDatabaseValue($value, AbstractPlatform $platform)
{ {
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null) return ($value !== null)
? $value->format($platform->getTimeFormatString()) : null; ? $value->format($platform->getTimeFormatString()) : null;
} }
......
...@@ -31,6 +31,24 @@ class DateTest extends \Doctrine\Tests\DbalTestCase ...@@ -31,6 +31,24 @@ class DateTest extends \Doctrine\Tests\DbalTestCase
); );
} }
/**
* @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 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 ;)
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
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; use Doctrine\Tests\DBAL\Mocks\MockPlatform;
...@@ -27,6 +28,24 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -27,6 +28,24 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase
$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 ;)
......
...@@ -27,6 +27,25 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase ...@@ -27,6 +27,25 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase
$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 ;)
......
...@@ -24,6 +24,24 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -24,6 +24,24 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase
); );
} }
/**
* @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 testTimeConvertsToPHPValue() public function testTimeConvertsToPHPValue()
{ {
$this->assertTrue( $this->assertTrue(
......
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