diff --git a/composer.json b/composer.json index 9715ba6..e4d55d4 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/psalm-baseline.xml b/psalm-baseline.xml deleted file mode 100644 index 11aeca8..0000000 --- a/psalm-baseline.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - getVarcharTypeDeclarationSQL - - - diff --git a/psalm.xml b/psalm.xml index 8a88a25..6cd4585 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,11 +1,9 @@ diff --git a/src/Types/PhoneNumberType.php b/src/Types/PhoneNumberType.php index 45e7fac..160567c 100644 --- a/src/Types/PhoneNumberType.php +++ b/src/Types/PhoneNumberType.php @@ -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; @@ -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 @@ -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); } } diff --git a/tests/AbstractFunctionalTestCase.php b/tests/AbstractFunctionalTestCase.php index 55e47f0..c946a13 100644 --- a/tests/AbstractFunctionalTestCase.php +++ b/tests/AbstractFunctionalTestCase.php @@ -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 diff --git a/tests/Entity/User.php b/tests/Entity/User.php index 514dfec..9786970 100644 --- a/tests/Entity/User.php +++ b/tests/Entity/User.php @@ -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; } diff --git a/tests/Types/PhoneNumberTypeTest.php b/tests/Types/PhoneNumberTypeTest.php index f366a50..8d1da04 100644 --- a/tests/Types/PhoneNumberTypeTest.php +++ b/tests/Types/PhoneNumberTypeTest.php @@ -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; @@ -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); } @@ -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 @@ -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); @@ -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'], ]; } } diff --git a/tests/TypesFunctionalTest.php b/tests/TypesFunctionalTest.php index 1f32ca3..3f738ba 100644 --- a/tests/TypesFunctionalTest.php +++ b/tests/TypesFunctionalTest.php @@ -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; }