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
35 changes: 35 additions & 0 deletions src/Carbon/Doctrine/CarbonTzImmutableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
{
use CarbonTypeConverter;

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

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->convertCarbonToDatabaseValue($value, $platform);
varshneydevansh marked this conversation as resolved.
Show resolved Hide resolved
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?CarbonInterface
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->convertToCarbon($value, $platform);
}

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

declare(strict_types=1);

namespace Carbon\Doctrine;

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

class CarbonTzType extends DateTimeTzType
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
{
use CarbonTypeConverter;

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

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

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

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

declare(strict_types=1);

namespace Carbon\Tests\Doctrine;

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
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
{
Type::addType('carbontz_immutable', CarbonTzImmutableType::class);
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
}

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->assertEquals($expectedCarbon, $actualCarbon);
}

public function testGetClassName(): void
{
$type = Type::getType('carbontz_immutable');
$this->assertSame(CarbonImmutable::class, $type->getClassName());
}
}
59 changes: 59 additions & 0 deletions tests/Doctrine/CarbonTzTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Carbon\Tests\Doctrine;

use Carbon\Carbon;
use Carbon\CarbonInterface;
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
{
Type::addType('carbontz', CarbonTzType::class);
kylekatarnls marked this conversation as resolved.
Show resolved Hide resolved
}

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';
$expectedCarbon = Carbon::parse('2023-06-08 12:34:56.789000', 'Europe/Paris');

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

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

public function testGetClassName(): void
{
$type = Type::getType('carbontz');
$this->assertSame(Carbon::class, $type->getClassName());
}
}
Loading