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
$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
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null)
? $value->format($platform->getDateTimeFormatString()) : null;
}
......
......@@ -67,6 +67,10 @@ class DateTimeTzType extends Type
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null)
? $value->format($platform->getDateTimeTzFormatString()) : null;
}
......
......@@ -49,6 +49,10 @@ class DateType extends Type
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null)
? $value->format($platform->getDateFormatString()) : null;
}
......
......@@ -49,6 +49,10 @@ class TimeType extends Type
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null && !$value instanceof \DateTime) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), "DateTime");
}
return ($value !== null)
? $value->format($platform->getTimeFormatString()) : null;
}
......
......@@ -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()
{
// Birthday of jwage and also birthday of Doctrine. Send him a present ;)
......
......@@ -2,6 +2,7 @@
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
......@@ -27,6 +28,24 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase
$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()
{
// Birthday of jwage and also birthday of Doctrine. Send him a present ;)
......
......@@ -27,6 +27,25 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase
$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()
{
// Birthday of jwage and also birthday of Doctrine. Send him a present ;)
......
......@@ -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()
{
$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