Commit 1a6a796e authored by Marco Pivetta's avatar Marco Pivetta

#869 - DBAL-1293 - abstract test case for date-based types

parent bc0a4a47
<?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']],
];
}
}
......@@ -5,56 +5,16 @@ namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateTest extends \Doctrine\Tests\DbalTestCase
class DateTestCase extends BaseDateTypeTestCase
{
/**
* @var MockPlatform
*/
private $platform;
/**
* @var \Doctrine\DBAL\Types\DateType
*/
private $type;
/**
* @var string
*/
private $currentTimezone;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->platform = new MockPlatform();
$this->type = Type::getType('date');
$this->currentTimezone = date_default_timezone_get();
}
$this->type = Type::getType('date');
/**
* {@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);
parent::setUp();
}
public function testDateConvertsToPHPValue()
......@@ -91,37 +51,4 @@ class DateTest extends \Doctrine\Tests\DbalTestCase
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$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));
}
/**
* @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']],
];
}
}
......@@ -3,52 +3,33 @@
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateTimeTest extends \Doctrine\Tests\DbalTestCase
class DateTimeTestCase extends BaseDateTypeTestCase
{
/**
* @var MockPlatform
* {@inheritDoc}
*/
private $_platform;
/**
* @var \Doctrine\DBAL\Types\DateTimeType
*/
private $_type;
protected function setUp()
{
$this->_platform = new MockPlatform();
$this->_type = Type::getType('datetime');
$this->type = Type::getType('datetime');
parent::setUp();
}
public function testDateTimeConvertsToDatabaseValue()
{
$date = new \DateTime('1985-09-01 10:10:10');
$expected = $date->format($this->_platform->getDateTimeTzFormatString());
$actual = $this->_type->convertToDatabaseValue($date, $this->_platform);
$expected = $date->format($this->platform->getDateTimeTzFormatString());
$actual = $this->type->convertToDatabaseValue($date, $this->platform);
$this->assertEquals($expected, $actual);
}
/**
* @dataProvider invalidPHPValuesProvider
*
* @param mixed $value
*/
public function testInvalidTypeConversionToDatabaseValue($value)
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToDatabaseValue($value, $this->_platform);
}
public function testDateTimeConvertsToPHPValue()
{
// 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->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
}
......@@ -56,48 +37,15 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidDateTimeFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$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));
$this->type->convertToPHPValue('abcdefg', $this->platform);
}
public function testConvertsNonMatchingFormatToPhpValueWithParser()
{
$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'));
}
/**
* @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']],
];
}
}
......@@ -5,50 +5,32 @@ namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase
class DateTimeTzTestCase extends BaseDateTypeTestCase
{
/**
* @var MockPlatform
* {@inheritDoc}
*/
private $_platform;
/**
* @var \Doctrine\DBAL\Types\DateTimeTzType
*/
private $_type;
protected function setUp()
{
$this->_platform = new MockPlatform();
$this->_type = Type::getType('datetimetz');
$this->type = Type::getType('datetimetz');
parent::setUp();
}
public function testDateTimeConvertsToDatabaseValue()
{
$date = new \DateTime('1985-09-01 10:10:10');
$expected = $date->format($this->_platform->getDateTimeTzFormatString());
$actual = $this->_type->convertToDatabaseValue($date, $this->_platform);
$expected = $date->format($this->platform->getDateTimeTzFormatString());
$actual = $this->type->convertToDatabaseValue($date, $this->platform);
$this->assertEquals($expected, $actual);
}
/**
* @dataProvider invalidPHPValuesProvider
*
* @param mixed $value
*/
public function testInvalidTypeConversionToDatabaseValue($value)
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToDatabaseValue($value, $this->_platform);
}
public function testDateTimeConvertsToPHPValue()
{
// 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->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
}
......@@ -56,39 +38,6 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidDateFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$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));
}
/**
* @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']],
];
$this->type->convertToPHPValue('abcdefg', $this->platform);
}
}
......@@ -5,52 +5,27 @@ namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
class TimeTest extends \Doctrine\Tests\DbalTestCase
class TimeTestCase extends BaseDateTypeTestCase
{
/**
* @var MockPlatform
* {@inheritDoc}
*/
private $_platform;
/**
* @var \Doctrine\DBAL\Types\TimeType
*/
private $_type;
protected function setUp()
{
$this->_platform = new MockPlatform();
$this->_type = Type::getType('time');
}
public function testTimeConvertsToDatabaseValue()
{
$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 = Type::getType('time');
$this->_type->convertToDatabaseValue($value, $this->_platform);
parent::setUp();
}
public function testTimeConvertsToPHPValue()
{
$this->assertTrue(
$this->_type->convertToPHPValue('5:30:55', $this->_platform)
instanceof \DateTime
);
$this->assertInstanceOf('DateTime', $this->type->convertToPHPValue('5:30:55', $this->platform));
}
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('1970-01-01', $time->format('Y-m-d'));
}
......@@ -58,39 +33,6 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase
public function testInvalidTimeFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$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));
}
/**
* @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']],
];
$this->type->convertToPHPValue('abcdefg', $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