Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Sep 2, 2019
2 parents 5a72378 + 081e477 commit 3d9ddd5
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,12 @@ protected function camelize($property)
*/
private function getValueFromType($value, Type $type, string $fieldType, AbstractPlatform $platform): string
{
if ('binary' === $platform->getDoctrineTypeMapping($fieldType)) {
if ($platform->hasDoctrineTypeMappingFor($fieldType) &&
'binary' === $platform->getDoctrineTypeMapping($fieldType)
) {
return (string) $type->convertToPHPValue($value, $platform);
}

return $type->convertToDatabaseValue($value, $platform);
return (string) $type->convertToDatabaseValue($value, $platform);
}
}
64 changes: 64 additions & 0 deletions tests/Fixtures/DoctrineType/ProductIdType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ProductId;

final class ProductIdType extends Type
{
const NAME = 'ProductId';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
return $platform->getIntegerTypeDeclarationSQL($fieldDeclaration);
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/**
* @param mixed $value
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?ProductId
{
if ($value === null || $value instanceof ProductId) {
return $value;
}

try {
return new ProductId((int) $value);
} catch (\Throwable $e) {
throw ConversionException::conversionFailed($value, $this->getName());
}
}

/**
* @param mixed $value
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
{
$value = $this->convertToPHPValue($value, $platform);

return $value !== null ? $value->getId() : null;
}

public function getName(): string
{
return self::NAME;
}
}
36 changes: 36 additions & 0 deletions tests/Fixtures/Entity/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity;

final class Product
{
private $id;

private $name;

public function __construct(ProductId $id, string $name)
{
$this->id = $id;
$this->name = $name;
}

public function getId(): ProductId
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}
}
28 changes: 28 additions & 0 deletions tests/Fixtures/Entity/ProductId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity;

final class ProductId
{
private $id;

public function __construct(int $id)
{
$this->id = $id;
}

public function getId(): int
{
return $this->id;
}
}
72 changes: 68 additions & 4 deletions tests/Model/ModelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
use Sonata\DoctrineORMAdminBundle\Datagrid\OrderByToSelectWalker;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineORMAdminBundle\Model\ModelManager;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\ProductIdType;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidBinaryType;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AbstractEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\SubEmbeddedEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Product;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ProductId;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ProtectedEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\SimpleEntity;
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\UuidEntity;
Expand All @@ -55,11 +58,14 @@ class ModelManagerTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!Type::hasType('uuid')) {
Type::addType('uuid', UuidType::class);
if (!Type::hasType(UuidType::NAME)) {
Type::addType(UuidType::NAME, UuidType::class);
}
if (!Type::hasType('uuid_binary')) {
Type::addType('uuid_binary', UuidBinaryType::class);
if (!Type::hasType(UuidBinaryType::NAME)) {
Type::addType(UuidBinaryType::NAME, UuidBinaryType::class);
}
if (!Type::hasType(ProductIdType::NAME)) {
Type::addType(ProductIdType::NAME, ProductIdType::class);
}
}

Expand Down Expand Up @@ -390,6 +396,12 @@ public function testNonIntegerIdentifierType(): void
->willReturn($meta);

$platform = $this->createMock(PostgreSqlPlatform::class);
$platform->expects($this->any())
->method('hasDoctrineTypeMappingFor')
->with(UuidType::NAME)
->willReturn(false);
$platform->expects($this->never())
->method('getDoctrineTypeMapping');

$conn = $this->createMock(Connection::class);
$conn->expects($this->any())
Expand All @@ -415,6 +427,56 @@ public function testNonIntegerIdentifierType(): void
$this->assertSame($entity->getId()->toString(), $result[0]);
}

public function testIntegerIdentifierType(): void
{
$id = new ProductId(12345);
$entity = new Product($id, 'Some product');

$meta = $this->createMock(ClassMetadata::class);
$meta->expects($this->any())
->method('getIdentifierValues')
->willReturn([$entity->getId()]);
$meta->expects($this->any())
->method('getTypeOfField')
->willReturn(ProductIdType::NAME);

$mf = $this->createMock(ClassMetadataFactory::class);
$mf->expects($this->any())
->method('getMetadataFor')
->willReturn($meta);

$platform = $this->createMock(PostgreSqlPlatform::class);
$platform->expects($this->any())
->method('hasDoctrineTypeMappingFor')
->with(ProductIdType::NAME)
->willReturn(false);
$platform->expects($this->never())
->method('getDoctrineTypeMapping');

$conn = $this->createMock(Connection::class);
$conn->expects($this->any())
->method('getDatabasePlatform')
->willReturn($platform);

$em = $this->createMock(EntityManager::class);
$em->expects($this->any())
->method('getMetadataFactory')
->willReturn($mf);
$em->expects($this->any())
->method('getConnection')
->willReturn($conn);

$registry = $this->createMock(RegistryInterface::class);
$registry->expects($this->any())
->method('getManagerForClass')
->willReturn($em);

$manager = new ModelManager($registry);
$result = $manager->getIdentifierValues($entity);

$this->assertSame((string) $entity->getId()->getId(), $result[0]);
}

public function testAssociationIdentifierType(): void
{
$entity = new ContainerEntity(new AssociatedEntity(42, new EmbeddedEntity()), new EmbeddedEntity());
Expand All @@ -433,6 +495,8 @@ public function testAssociationIdentifierType(): void
->willReturn($meta);

$platform = $this->createMock(PostgreSqlPlatform::class);
$platform->expects($this->never())
->method('hasDoctrineTypeMappingFor');

$conn = $this->createMock(Connection::class);
$conn->expects($this->any())
Expand Down

0 comments on commit 3d9ddd5

Please sign in to comment.