Skip to content

Commit

Permalink
fix DateType timezone issus
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhard-efler committed Jul 30, 2021
1 parent da9bdc6 commit 50683f5
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ final class DateType extends AbstractType implements DatabaseTypeInterface, Elem
*/
protected function transform($value)
{
if ($value instanceof \DateTimeInterface) {
return new \DateTimeImmutable('@' . $value->getTimestamp());
if ($value instanceof \DateTime) {
return \DateTimeImmutable::createFromMutable($value);
}

if ($value instanceof \DateTimeImmutable) {
return $value;
}

if (\is_string($value)) {
Expand Down
136 changes: 136 additions & 0 deletions tests/src/Type/DateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* @link https://github.com/ixocreate
* @copyright IXOLIT GmbH
* @license MIT License
*/

declare(strict_types=1);

namespace Ixocreate\Test\Schema\Type;

use Doctrine\DBAL\Types\DateType as DBALDateType;
use Exception;
use Ixocreate\Schema\Builder\BuilderInterface;
use Ixocreate\Schema\Element\DateElement;
use Ixocreate\Schema\Type\DateType;
use PHPUnit\Framework\TestCase;

/**
* @covers \Ixocreate\Schema\Type\DateType
*/
final class DateTest extends TestCase
{
public function testCreate()
{
$dateTimeType = new DateType();

$newDateTimeType = $dateTimeType->create('2012-12-12 12:12:12');
$this->assertNotSame($dateTimeType, $newDateTimeType);
$this->assertSame('2012-12-12 12:12:12', $newDateTimeType->value()->format('Y-m-d H:i:s'));

$newDateTimeType = $dateTimeType->create(1355314332);
$this->assertNotSame($dateTimeType, $newDateTimeType);
$this->assertSame('2012-12-12 12:12:12', $newDateTimeType->value()->format('Y-m-d H:i:s'));

$newDateTimeType = $dateTimeType->create(new \DateTime('2012-12-12 12:12:12'));
$this->assertNotSame($dateTimeType, $newDateTimeType);
$this->assertSame('2012-12-12 12:12:12', $newDateTimeType->value()->format('Y-m-d H:i:s'));

$newDateTimeType = $dateTimeType->create([
'date' => '2012-12-12 12:12:12.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
]);
$this->assertNotSame($dateTimeType, $newDateTimeType);
$this->assertSame('2012-12-12 12:12:12', $newDateTimeType->value()->format('Y-m-d H:i:s'));
}

public function testInvalidValue()
{
$this->expectException(Exception::class);
$dateTimeType = new DateType();
$dateTimeType->create([]);
}

public function testToString()
{
$dateTimeType = new DateType();

$newDateTimeType = $dateTimeType->create('2012-12-12 12:12:12');

$this->assertSame((new \DateTime('2012-12-12 12:12:12'))->format('Y-m-d'), (string)$newDateTimeType);
}

/** @runInSeparateProcess */
public function testTimezone()
{
\date_default_timezone_set('UTC');

$dateTimeType = new DateType();

$newDateTimeType = $dateTimeType->create(new \DateTime('2012-12-12 00:12:12', new \DateTimeZone('Europe/Vienna')));

$this->assertSame('"' . (new \DateTime('2012-12-12 12:12:12'))->format('Y-m-d') . '"', \json_encode($newDateTimeType));
}

public function testJsonserialize()
{
$dateTimeType = new DateType();

$newDateTimeType = $dateTimeType->create('2012-12-12 12:12:12');

$this->assertSame('"' . (new \DateTime('2012-12-12 12:12:12'))->format('Y-m-d') . '"', \json_encode($newDateTimeType));
}

public function testConvertToDatabaseValue()
{
$dateTimeType = new DateType();

$newDateTimeType = $dateTimeType->create('2012-12-12 12:12:12');
$this->assertInstanceOf(\DateTimeImmutable::class, $newDateTimeType->convertToDatabaseValue());
$this->assertSame((new \DateTime('2012-12-12 12:12:12'))->format('c'), $newDateTimeType->convertToDatabaseValue()->format('c'));
}

public function testValue()
{
$dateTimeType = new DateType();

$newDateTimeType = $dateTimeType->create('2012-12-12 12:12:12');
$this->assertInstanceOf(\DateTimeImmutable::class, $newDateTimeType->value());
$this->assertSame((new \DateTime('2012-12-12 12:12:12'))->format('c'), $newDateTimeType->value()->format('c'));
}

public function testSerialize()
{
$dateTimeType = new DateType();

$dateTimeType = $dateTimeType->create('2012-12-12 12:12:12');
$newDateTimeType = \unserialize(\serialize($dateTimeType));
$this->assertInstanceOf(DateType::class, $newDateTimeType);
$this->assertSame((new \DateTime('2012-12-12 12:12:12'))->format('Y-m-d'), $newDateTimeType->__toString());
}

public function testProvideElement()
{
$mock = $this->createMock(BuilderInterface::class);
$mock->expects($this->any())
->method('get')
->with(DateElement::class);

$dateTimeType = new DateType();
$dateTimeType->provideElement($mock);

$this->addToAssertionCount(1);
}

public function testServiceName()
{
$this->assertSame('date', DateType::serviceName());
}

public function testBaseDatabaseType()
{
$this->assertSame(DBALDateType::class, DateType::baseDatabaseType());
}
}

0 comments on commit 50683f5

Please sign in to comment.