Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add time-zone-aware doctrine types #10

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
40 changes: 40 additions & 0 deletions src/Carbon/Doctrine/CarbonTzImmutableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Carbon\Doctrine;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateTimeTzImmutableType;

class CarbonTzImmutableType extends DateTimeTzImmutableType implements CarbonDoctrineType
{
use CarbonTypeConverter
{
convertToDatabaseValue as convertCarbonToDatabaseValue;
}

public function getName(): string
{
return 'carbontz_immutable';
}

public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string

{
return $this->convertCarbonToDatabaseValue($value, $platform);
varshneydevansh marked this conversation as resolved.
Show resolved Hide resolved
}

public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?CarbonImmutable

{
return $this->convertToCarbon($value, $platform);
}

protected function getClassName(): string
{
return CarbonImmutable::class;
}
}
37 changes: 37 additions & 0 deletions src/Carbon/Doctrine/CarbonTzType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Carbon\Doctrine;

use Carbon\Carbon;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateTimeTzType;

class CarbonTzType extends DateTimeTzType implements CarbonDoctrineType
{
use CarbonTypeConverter
{
convertToDatabaseValue as convertCarbonToDatabaseValue;
}

public function getName(): string
{
return 'carbontz';
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
return $this->convertCarbonToDatabaseValue($value, $platform);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?DateTime
{
return $this->convertToCarbon($value, $platform);
}

protected function getClassName(): string
{
return Carbon::class;
}
}
55 changes: 55 additions & 0 deletions tests/Doctrine/CarbonTzImmutableTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Carbon\Tests\Doctrine;

use Carbon\CarbonImmutable;
use Carbon\Doctrine\CarbonTzImmutableType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;

class CarbonTzImmutableTypeTest extends TestCase
{
protected function setUp(): void
{
if (!Type::hasType('carbontz_immutable')) {
Type::addType('carbontz_immutable', CarbonTzImmutableType::class);
}
}

public function testGetName(): void
{
$type = Type::getType('carbontz_immutable');
$this->assertSame('carbontz_immutable', $type->getName());
}

public function testConvertToDatabaseValue(): void
{
$type = Type::getType('carbontz_immutable');
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getDateTimeTzFormatString')->willReturn('Y-m-d H:i:s.u P');

$carbon = CarbonImmutable::parse('2023-06-08 12:34:56.789000', 'Europe/Paris');
$expectedDatabaseValue = '2023-06-08 12:34:56.789000 +02:00';

$actualDatabaseValue = $type->convertToDatabaseValue($carbon, $platform);

$this->assertSame($expectedDatabaseValue, $actualDatabaseValue);
}

public function testConvertToPHPValue(): void
{
$type = Type::getType('carbontz_immutable');
$platform = $this->createMock(AbstractPlatform::class);

$databaseValue = '2023-06-08 12:34:56.789000 +02:00';
$expectedCarbon = CarbonImmutable::parse('2023-06-08 12:34:56.789000', 'Europe/Paris');

$actualCarbon = $type->convertToPHPValue($databaseValue, $platform);

$this->assertInstanceOf(CarbonImmutable::class, $actualCarbon);
$this->assertEquals($expectedCarbon, $actualCarbon);
}
}
55 changes: 55 additions & 0 deletions tests/Doctrine/CarbonTzTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Carbon\Tests\Doctrine;

use Carbon\Carbon;
use Carbon\Doctrine\CarbonTzType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;

class CarbonTzTypeTest extends TestCase
{
protected function setUp(): void
{
if (!Type::hasType('carbontz')) {
Type::addType('carbontz', CarbonTzType::class);
}
}

public function testGetName(): void
{
$type = Type::getType('carbontz');
$this->assertSame('carbontz', $type->getName());
}

public function testConvertToDatabaseValue(): void
{
$type = Type::getType('carbontz');
$platform = $this->createMock(AbstractPlatform::class);
$platform->method('getDateTimeTzFormatString')->willReturn('Y-m-d H:i:s.u P');

$carbon = Carbon::parse('2023-06-08 12:34:56.789000', 'Europe/Paris');
$expectedDatabaseValue = '2023-06-08 12:34:56.789000 +02:00';

$actualDatabaseValue = $type->convertToDatabaseValue($carbon, $platform);

$this->assertSame($expectedDatabaseValue, $actualDatabaseValue);
}

public function testConvertToPHPValue(): void
{
$type = Type::getType('carbontz');
$platform = $this->createMock(AbstractPlatform::class);

$databaseValue = '2023-06-08 12:34:56.789000 +02:00';
$expectedDateTime = new DateTime('2023-06-08 12:34:56.789000', new DateTimeZone('Europe/Paris'));

$actualCarbon = $type->convertToPHPValue($databaseValue, $platform);

$this->assertInstanceOf(Carbon::class, $actualCarbon);
$this->assertEquals($expectedCarbon, $actualCarbon);
varshneydevansh marked this conversation as resolved.
Show resolved Hide resolved
}
}