Skip to content

Commit

Permalink
Support doctrine/orm 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 1, 2023
1 parent 0398c6b commit 2a52c0d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ jobs:
setup: lowest

- php: 8.3
- php: 8.3
dbal: ^4.0.0
- php: 8.3
setup: lowest

Expand All @@ -97,7 +95,7 @@ jobs:
- php: 8.2
coverage: true

name: ${{ matrix.laravel && 'Laravel compatibility - ' || '' }}${{ matrix.classmap-authoritative && 'classmap-authoritative - ' || '' }}${{ matrix.coverage && 'Coverage - ' || '' }}PHP ${{ matrix.php }}${{ matrix.dbal && ' - DBAL ' || '' }}${{ matrix.dbal }} - ${{ matrix.setup || 'stable' }} - ubuntu
name: ${{ matrix.laravel && 'Laravel compatibility - ' || '' }}${{ matrix.classmap-authoritative && 'classmap-authoritative - ' || '' }}${{ matrix.coverage && 'Coverage - ' || '' }}PHP ${{ matrix.php }} - ${{ matrix.setup || 'stable' }} - ubuntu

steps:
- name: Checkout the code
Expand Down Expand Up @@ -136,8 +134,8 @@ jobs:
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache-ubuntu.outputs.dir }}
key: "php-${{ matrix.php }}-${{ matrix.setup }}-${{ matrix.dbal }}-ubuntu-${{ hashFiles('**/composer.json') }}"
restore-keys: "php-${{ matrix.php }}-${{ matrix.setup }}-${{ matrix.dbal }}-ubuntu-${{ hashFiles('**/composer.json') }}"
key: "php-${{ matrix.php }}-${{ matrix.setup }}-ubuntu-${{ hashFiles('**/composer.json') }}"
restore-keys: "php-${{ matrix.php }}-${{ matrix.setup }}-ubuntu-${{ hashFiles('**/composer.json') }}"

- name: Install dependencies
uses: nick-fields/retry@v2
Expand All @@ -150,9 +148,6 @@ jobs:
if [[ "${{ matrix.laravel }}" != 'true' ]]; then
composer remove --no-update kylekatarnls/multi-tester --no-interaction --dev;
fi;
if [[ "${{ matrix.dbal }}" != '' ]]; then
composer require --no-update doctrine/dbal:${{ matrix.dbal }} --no-interaction --dev;
fi;
${{ matrix.php >= 8.1 && 'composer require --no-update phpunit/phpunit:^9.5.20 --no-interaction --dev;' || '' }}
composer update --prefer-dist --no-progress --prefer-${{ matrix.setup || 'stable' }} ${{ matrix.classmap-authoritative && '--classmap-authoritative' || '' }}${{ matrix.php >= 8.2 && ' --ignore-platform-reqs' || '' }};
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"symfony/translation": "^3.4 || ^4.0 || ^5.0 || ^6.0"
},
"require-dev": {
"doctrine/dbal": "^2.0 || ^3.1.4",
"doctrine/orm": "^2.7",
"doctrine/dbal": "^2.0 || ^3.1.4 || ^4.0",
"doctrine/orm": "^2.7 || ^3.0",
"friendsofphp/php-cs-fixer": "^3.0",
"kylekatarnls/multi-tester": "^2.0",
"ondrejmirtes/better-reflection": "*",
Expand Down
70 changes: 45 additions & 25 deletions tests/Doctrine/CarbonTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use DateTimeImmutable;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\MySQL57Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Exception;
Expand Down Expand Up @@ -75,11 +76,11 @@ public function testGetSQLDeclaration(string $name)
] : [
'precision' => null,
'secondPrecision' => true,
], new MySQL57Platform()));
], $this->getMySQLPlatform()));

$this->assertSame('DATETIME(3)', $type->getSQLDeclaration([
'precision' => 3,
], new MySQL57Platform()));
], $this->getMySQLPlatform()));

$this->assertSame('TIMESTAMP(0)', $type->getSQLDeclaration($supportZeroPrecision ? [
'precision' => 0,
Expand All @@ -102,21 +103,21 @@ public function testGetSQLDeclaration(string $name)
'precision' => null,
] : [
'precision' => 0,
], new MySQL57Platform()));
], $this->getMySQLPlatform()));

$this->assertSame('DATETIME(6)', $type->getSQLDeclaration([
'precision' => null,
], new MySQL57Platform()));
], $this->getMySQLPlatform()));

DateTimeDefaultPrecision::set(9);
$this->assertSame('DATETIME(9)', $type->getSQLDeclaration([
'precision' => null,
], new MySQL57Platform()));
], $this->getMySQLPlatform()));

DateTimeDefaultPrecision::set(0);
$this->assertSame('DATETIME', $type->getSQLDeclaration([
'precision' => null,
], new MySQL57Platform()));
], $this->getMySQLPlatform()));

DateTimeDefaultPrecision::set($precision);
}
Expand All @@ -135,17 +136,17 @@ public function testConvertToPHPValue(string $name, string $class)
{
$type = Type::getType($name);

$this->assertNull($type->convertToPHPValue(null, new MySQL57Platform()));
$this->assertNull($type->convertToPHPValue(null, $this->getMySQLPlatform()));

$date = $type->convertToPHPValue(Carbon::parse('2020-06-23 18:47'), new MySQL57Platform());
$date = $type->convertToPHPValue(Carbon::parse('2020-06-23 18:47'), $this->getMySQLPlatform());
$this->assertInstanceOf($class, $date);
$this->assertSame('2020-06-23 18:47:00.000000', $date->format('Y-m-d H:i:s.u'));

$date = $type->convertToPHPValue(new DateTimeImmutable('2020-06-23 18:47'), new MySQL57Platform());
$date = $type->convertToPHPValue(new DateTimeImmutable('2020-06-23 18:47'), $this->getMySQLPlatform());
$this->assertInstanceOf($class, $date);
$this->assertSame('2020-06-23 18:47:00.000000', $date->format('Y-m-d H:i:s.u'));

$date = $type->convertToPHPValue('2020-06-23 18:47', new MySQL57Platform());
$date = $type->convertToPHPValue('2020-06-23 18:47', $this->getMySQLPlatform());
$this->assertInstanceOf($class, $date);
$this->assertSame('2020-06-23 18:47:00.000000', $date->format('Y-m-d H:i:s.u'));
}
Expand All @@ -160,14 +161,17 @@ public function testConvertToPHPValue(string $name, string $class)
*
* @throws Exception
*/
public function testConvertToPHPValueFailure(string $name, string $class)
public function testConvertToPHPValueFailure(string $name, string $class, string $typeClass)
{
$conversion = version_compare(self::getDbalVersion(), '4.0.0', '>=')
? "to \"$typeClass\" as an error was triggered by the unserialization: "
: "\"2020-0776-23 18:47\" to Doctrine Type $name. Expected format: ";
$this->expectExceptionObject(new ConversionException(
"Could not convert database value \"2020-0776-23 18:47\" to Doctrine Type $name. ".
"Expected format: Y-m-d H:i:s.u or any format supported by $class::parse()"
'Could not convert database value '.$conversion.
"Y-m-d H:i:s.u or any format supported by $class::parse()"
));

Type::getType($name)->convertToPHPValue('2020-0776-23 18:47', new MySQL57Platform());
Type::getType($name)->convertToPHPValue('2020-0776-23 18:47', $this->getMySQLPlatform());
}

/**
Expand All @@ -183,10 +187,10 @@ public function testConvertToDatabaseValue(string $name)
{
$type = Type::getType($name);

$this->assertNull($type->convertToDatabaseValue(null, new MySQL57Platform()));
$this->assertNull($type->convertToDatabaseValue(null, $this->getMySQLPlatform()));
$this->assertSame(
'2020-06-23 18:47:00.000000',
$type->convertToDatabaseValue(new DateTimeImmutable('2020-06-23 18:47'), new MySQL57Platform())
$type->convertToDatabaseValue(new DateTimeImmutable('2020-06-23 18:47'), $this->getMySQLPlatform())
);
}

Expand All @@ -199,15 +203,18 @@ public function testConvertToDatabaseValue(string $name)
*
* @throws Exception
*/
public function testConvertToDatabaseValueFailure(string $name)
public function testConvertToDatabaseValueFailure(string $name, string $class, string $typeClass)
{
$quote = class_exists('Doctrine\\DBAL\\Version') ? "'" : '';
$conversion = version_compare(self::getDbalVersion(), '4.0.0', '>=')
? "array to type $typeClass. "
: "{$quote}array{$quote} to type {$quote}$name{$quote}. ";
$this->expectExceptionObject(new ConversionException(
"Could not convert PHP value of type {$quote}array{$quote} to type {$quote}$name{$quote}. ".
'Could not convert PHP value of type '.$conversion.
'Expected one of the following types: null, DateTime, Carbon'
));

Type::getType($name)->convertToDatabaseValue([2020, 06, 23], new MySQL57Platform());
Type::getType($name)->convertToDatabaseValue([2020, 06, 23], $this->getMySQLPlatform());
}

/**
Expand All @@ -224,21 +231,34 @@ public function testConvertToDatabaseValueFailure(string $name)
*/
public function testRequiresSQLCommentHint(string $name, string $class, string $typeClass, bool $hintRequired)
{
if (version_compare(self::getDbalVersion(), '4.0.0', '>=')) {
$this->markTestSkipped('requiresSQLCommentHint dropped since DBAL 4');
}

$type = Type::getType($name);

$this->assertSame($hintRequired, $type->requiresSQLCommentHint(new MySQL57Platform()));
$this->assertSame($hintRequired, $type->requiresSQLCommentHint($this->getMySQLPlatform()));
}

private static function supportsZeroPrecision(): bool
private static function getDbalVersion(): string
{
static $support = null;
static $dbalVersion = null;

if ($support === null) {
if ($dbalVersion === null) {
$installed = require __DIR__.'/../../vendor/composer/installed.php';
$dbalVersion = $installed['versions']['doctrine/dbal']['version'] ?? '2.0.0';
$support = version_compare($dbalVersion, '3.7.0', '>=');
}

return $support;
return $dbalVersion;
}

private static function supportsZeroPrecision(): bool
{
return version_compare(self::getDbalVersion(), '3.7.0', '>=');
}

private function getMySQLPlatform()
{
return class_exists(MySQLPlatform::class) ? new MySQLPlatform() : new MySQL57Platform();
}
}

0 comments on commit 2a52c0d

Please sign in to comment.