Skip to content

Commit

Permalink
Upgrade to doctrine/orm v3 & doctrine/dbal v4
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Feb 6, 2024
1 parent 5086161 commit ecf7e9a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 75 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
"license": "MIT",
"require": {
"brick/phonenumber": "~0.6.0",
"doctrine/dbal": "^2.7.0 || ^3.0.0",
"doctrine/orm": "^2.7.0",
"doctrine/dbal": "^4.0",
"doctrine/orm": "^3.0",
"php": "^8.1"
},
"require-dev": {
"ext-pdo": "*",
"ext-pdo_sqlite": "*",
"doctrine/annotations": "^1.11",
"phpunit/phpunit": "^10.5",
"php-coveralls/php-coveralls": "^2.4",
"vimeo/psalm": "^5.21.1"
"vimeo/psalm": "^5.21.1",
"symfony/cache": "^5.0 || ^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 0 additions & 8 deletions psalm-baseline.xml

This file was deleted.

2 changes: 0 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
Expand Down
39 changes: 20 additions & 19 deletions src/Types/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
namespace Brick\PhoneNumber\Doctrine\Types;

use Brick\PhoneNumber\PhoneNumber;
use Brick\PhoneNumber\PhoneNumberParseException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;

final class PhoneNumberType extends Type
{
public function getName() : string
{
return 'PhoneNumber';
}

public function convertToDatabaseValue($value, AbstractPlatform $platform) : ?string
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform) : ?string
{
if ($value === null) {
return null;
Expand All @@ -26,20 +23,29 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) : ?st
return (string) $value;
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), [PhoneNumber::class, 'null']);
throw InvalidType::new(
$value,
static::class,
[PhoneNumber::class, 'null'],
);
}

public function convertToPHPValue($value, AbstractPlatform $platform) : ?PhoneNumber
public function convertToPHPValue(mixed $value, AbstractPlatform $platform) : ?PhoneNumber
{
if ($value === null) {
return null;
}

if (is_string($value)) {
return PhoneNumber::parse($value);
try {
return PhoneNumber::parse((string) $value);
} catch (PhoneNumberParseException $e) {
throw ValueNotConvertible::new(
$value,
PhoneNumber::class,
$e->getMessage(),
$e,
);
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['string', 'null']);
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform) : string
Expand All @@ -49,11 +55,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) : s
$column['length'] = 16;
}

return $platform->getVarcharTypeDeclarationSQL($column);
}

public function requiresSQLCommentHint(AbstractPlatform $platform) : bool
{
return true;
return $platform->getStringTypeDeclarationSQL($column);
}
}
20 changes: 9 additions & 11 deletions tests/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@
use Brick\PhoneNumber\Doctrine\Tests\Entity\User;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;
use PHPUnit\Framework\TestCase;

abstract class AbstractFunctionalTestCase extends TestCase
{
final protected static function createConnection(): Connection
{
return DriverManager::getConnection(['url' => 'sqlite:///:memory:']);
$dsnParser = new DsnParser(['sqlite' => 'pdo_sqlite']);

return DriverManager::getConnection(
$dsnParser->parse('sqlite:///:memory:'),
);
}

final protected static function createEntityManager(Connection $connection): EntityManager
{
return EntityManager::create($connection, self::createConfiguration());
return new EntityManager($connection, self::createConfiguration());
}

private static function createConfiguration(): Configuration
{
$config = new Configuration();

$driverImpl = $config->newDefaultAnnotationDriver([__DIR__ . '/tests/Entity'], false);
$config->setMetadataDriverImpl($driverImpl);

$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Brick\PhoneNumber\Doctrine\Tests\Proxy');

return $config;
return ORMSetup::createAttributeMetadataConfiguration([__DIR__ . '/tests/Entity']);
}

final protected static function truncateEntityTable(EntityManager $em): void
Expand Down
24 changes: 7 additions & 17 deletions tests/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,14 @@
use Brick\PhoneNumber\PhoneNumber;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
#[ORM\Entity]
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @var int
*/
public $id;
#[ORM\Id]
#[ORM\Column(type: "integer")]
#[ORM\GeneratedValue]
public int $id;

/**
* @ORM\Column(type="PhoneNumber", nullable=true)
*
* @var PhoneNumber|null
*/
public $phoneNumber = null;
#[ORM\Column(type: 'PhoneNumber', nullable: true)]
public ?PhoneNumber $phoneNumber = null;
}
23 changes: 11 additions & 12 deletions tests/Types/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use Brick\PhoneNumber\PhoneNumber;
use Brick\PhoneNumber\Doctrine\Types\PhoneNumberType;
use Brick\PhoneNumber\PhoneNumberParseException;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
Expand All @@ -26,7 +25,7 @@ private function getPhoneNumberType(): PhoneNumberType
public function testConvertToDatabaseValue(?PhoneNumber $value, ?string $expectedValue): void
{
$type = $this->getPhoneNumberType();
$actualValue = $type->convertToDatabaseValue($value, new SqlitePlatform());
$actualValue = $type->convertToDatabaseValue($value, new SQLitePlatform());

self::assertSame($expectedValue, $actualValue);
}
Expand All @@ -43,12 +42,12 @@ public static function providerConvertToDatabaseValue(): array
/**
* @dataProvider providerConvertToDatabaseValueWithInvalidValue
*/
public function testConvertToDatabaseValueWithInvalidValue($value): void
public function testConvertToDatabaseValueWithInvalidValue(mixed $value): void
{
$type = $this->getPhoneNumberType();

$this->expectException(ConversionException::class);
$type->convertToDatabaseValue($value, new SqlitePlatform());
$type->convertToDatabaseValue($value, new SQLitePlatform());
}

public static function providerConvertToDatabaseValueWithInvalidValue(): array
Expand All @@ -68,7 +67,7 @@ public static function providerConvertToDatabaseValueWithInvalidValue(): array
public function testConvertToPHPValue(?string $value): void
{
$type = $this->getPhoneNumberType();
$convertedValue = $type->convertToPHPValue($value, new SqlitePlatform());
$convertedValue = $type->convertToPHPValue($value, new SQLitePlatform());

if ($value === null) {
self::assertNull($convertedValue);
Expand All @@ -90,20 +89,20 @@ public static function providerConvertToPHPValue(): array
/**
* @dataProvider providerConvertToPHPValueWithInvalidValue
*/
public function testConvertToPHPValueWithInvalidValue($value, string $expectedExceptionClass): void
public function testConvertToPHPValueWithInvalidValue(mixed $value): void
{
$type = $this->getPhoneNumberType();

$this->expectException($expectedExceptionClass);
$type->convertToPHPValue($value, new SqlitePlatform());
$this->expectException(ConversionException::class);
$type->convertToPHPValue($value, new SQLitePlatform());
}

public static function providerConvertToPHPValueWithInvalidValue(): array
{
return [
[123, ConversionException::class],
['', PhoneNumberParseException::class],
['+33', PhoneNumberParseException::class],
[123],
[''],
['+33'],
];
}
}
4 changes: 2 additions & 2 deletions tests/TypesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function testCreateSchema(): Connection
self::assertCount(1, $sql);
$sql = $sql[0];

self::assertStringContainsString('phoneNumber VARCHAR(16) DEFAULT NULL --(DC2Type:PhoneNumber)', $sql);
self::assertStringContainsString('phoneNumber VARCHAR(16) DEFAULT NULL', $sql);

$connection->exec($sql);
$connection->executeStatement($sql);

return $connection;
}
Expand Down

0 comments on commit ecf7e9a

Please sign in to comment.