Skip to content

Commit

Permalink
Merge pull request #2 from brick/orm3
Browse files Browse the repository at this point in the history
Upgrade to doctrine/orm v3 & doctrine/dbal v4
  • Loading branch information
BenMorel authored Dec 19, 2024
2 parents 6fde9c2 + fcb58c3 commit 2514dda
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 99 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.15.0"
"vimeo/psalm": "^5.26.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;
}
40 changes: 16 additions & 24 deletions tests/Types/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

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\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;

Expand All @@ -20,13 +20,11 @@ private function getPhoneNumberType(): PhoneNumberType
return Type::getType('PhoneNumber');
}

/**
* @dataProvider providerConvertToDatabaseValue
*/
#[DataProvider('providerConvertToDatabaseValue')]
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 @@ -40,15 +38,13 @@ public static function providerConvertToDatabaseValue(): array
];
}

/**
* @dataProvider providerConvertToDatabaseValueWithInvalidValue
*/
public function testConvertToDatabaseValueWithInvalidValue($value): void
#[DataProvider('providerConvertToDatabaseValueWithInvalidValue')]
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 @@ -62,13 +58,11 @@ public static function providerConvertToDatabaseValueWithInvalidValue(): array
];
}

/**
* @dataProvider providerConvertToPHPValue
*/
#[DataProvider('providerConvertToPHPValue')]
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 @@ -87,23 +81,21 @@ public static function providerConvertToPHPValue(): array
];
}

/**
* @dataProvider providerConvertToPHPValueWithInvalidValue
*/
public function testConvertToPHPValueWithInvalidValue($value, string $expectedExceptionClass): void
#[DataProvider('providerConvertToPHPValueWithInvalidValue')]
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'],
];
}
}
21 changes: 7 additions & 14 deletions tests/TypesFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Brick\PhoneNumber\PhoneNumber;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\Attributes\Depends;

class TypesFunctionalTest extends AbstractFunctionalTestCase
{
Expand All @@ -23,16 +24,14 @@ 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;
}

/**
* @depends testCreateSchema
*/
#[Depends('testCreateSchema')]
public function testSaveNull(Connection $connection): Connection
{
$em = self::createEntityManager($connection);
Expand All @@ -49,9 +48,7 @@ public function testSaveNull(Connection $connection): Connection
return $connection;
}

/**
* @depends testSaveNull
*/
#[Depends('testSaveNull')]
public function testLoadNull(Connection $connection): void
{
$em = self::createEntityManager($connection);
Expand All @@ -62,9 +59,7 @@ public function testLoadNull(Connection $connection): void
self::assertNull($entity->phoneNumber);
}

/**
* @depends testCreateSchema
*/
#[Depends('testCreateSchema')]
public function testSaveValues(Connection $connection): Connection
{
$em = self::createEntityManager($connection);
Expand All @@ -83,9 +78,7 @@ public function testSaveValues(Connection $connection): Connection
return $connection;
}

/**
* @depends testSaveValues
*/
#[Depends('testSaveValues')]
public function testLoadValues(Connection $connection): void
{
$em = self::createEntityManager($connection);
Expand Down

0 comments on commit 2514dda

Please sign in to comment.