-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChronosDate does not implement DateTimeInterface #9967
For coherence, we also update Chronos and cover everything with tests since we have more code than before.
- Loading branch information
Showing
4 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EcodevTests\Felix\DBAL\Types; | ||
|
||
use Cake\Chronos\Chronos; | ||
use DateTimeImmutable; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Ecodev\Felix\DBAL\Types\ChronosType; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ChronosTypeTest extends TestCase | ||
{ | ||
private ChronosType $type; | ||
|
||
private AbstractPlatform $platform; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->type = new ChronosType(); | ||
$this->platform = new MySQLPlatform(); | ||
} | ||
|
||
public function testConvertToDatabaseValue(): void | ||
{ | ||
self::assertSame('DATETIME', $this->type->getSqlDeclaration(['foo'], $this->platform)); | ||
self::assertFalse($this->type->requiresSQLCommentHint($this->platform)); | ||
|
||
$actual = $this->type->convertToDatabaseValue(new Chronos('2016-01-01 15:58:59'), $this->platform); | ||
self::assertSame('2016-01-01 15:58:59', $actual, 'support Chronos'); | ||
|
||
$actual = $this->type->convertToDatabaseValue(new DateTimeImmutable('2016-01-01 15:58:59'), $this->platform); | ||
self::assertSame('2016-01-01 15:58:59', $actual, 'support DateTimeImmutable'); | ||
|
||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform), 'support null values'); | ||
} | ||
|
||
public function testConvertToPHPValue(): void | ||
{ | ||
$actualPhp = $this->type->convertToPHPValue('2016-01-01 15:58:59', $this->platform); | ||
self::assertInstanceOf(Chronos::class, $actualPhp); | ||
self::assertSame('2016-01-01 15:58:59', $actualPhp->__toString(), 'support string'); | ||
|
||
$actualPhp = $this->type->convertToPHPValue(new Chronos('2016-01-01 15:58:59'), $this->platform); | ||
self::assertInstanceOf(Chronos::class, $actualPhp); | ||
self::assertSame('2016-01-01 15:58:59', $actualPhp->__toString(), 'support Chronos'); | ||
|
||
$actualPhp = $this->type->convertToPHPValue(new DateTimeImmutable('2016-01-01 15:58:59'), $this->platform); | ||
self::assertInstanceOf(Chronos::class, $actualPhp); | ||
self::assertSame('2016-01-01 15:58:59', $actualPhp->__toString(), 'support DateTimeImmutable'); | ||
|
||
self::assertNull($this->type->convertToPHPValue(null, $this->platform), 'support null values'); | ||
} | ||
|
||
public function testConvertToPHPValueThrowsWithInvalidValue(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToPHPValue(123, $this->platform); | ||
} | ||
|
||
public function testConvertToDatabaseValueThrowsWithInvalidValue(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue(123, $this->platform); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EcodevTests\Felix\DBAL\Types; | ||
|
||
use Cake\Chronos\ChronosDate; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Ecodev\Felix\DBAL\Types\DateType; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DateTypeTest extends TestCase | ||
{ | ||
private DateType $type; | ||
|
||
private AbstractPlatform $platform; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->type = new DateType(); | ||
$this->platform = new MySQLPlatform(); | ||
} | ||
|
||
public function testConvertToDatabaseValue(): void | ||
{ | ||
self::assertSame('DATE', $this->type->getSqlDeclaration(['foo'], $this->platform)); | ||
self::assertFalse($this->type->requiresSQLCommentHint($this->platform)); | ||
|
||
$actual = $this->type->convertToDatabaseValue(new ChronosDate('2016-01-01'), $this->platform); | ||
self::assertSame('2016-01-01', $actual, 'support Chronos'); | ||
|
||
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform), 'support null values'); | ||
} | ||
|
||
public function testConvertToPHPValue(): void | ||
{ | ||
$actualPhp = $this->type->convertToPHPValue('2022-12-31', $this->platform); | ||
self::assertInstanceOf(ChronosDate::class, $actualPhp); | ||
self::assertSame('2022-12-31', $actualPhp->__toString(), 'support string'); | ||
|
||
$actualPhp = $this->type->convertToPHPValue(new ChronosDate('2022-12-31'), $this->platform); | ||
self::assertInstanceOf(ChronosDate::class, $actualPhp); | ||
self::assertSame('2022-12-31', $actualPhp->__toString(), 'support ChronosDate'); | ||
|
||
self::assertNull($this->type->convertToPHPValue(null, $this->platform), 'support null values'); | ||
} | ||
|
||
public function testConvertToPHPValueThrowsWithInvalidValue(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToPHPValue(123, $this->platform); | ||
} | ||
|
||
public function testConvertToDatabaseValueThrowsWithInvalidValue(): void | ||
{ | ||
$this->expectException(ConversionException::class); | ||
|
||
$this->type->convertToDatabaseValue(123, $this->platform); | ||
} | ||
} |