Skip to content

Commit

Permalink
simplified code and PHP version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
driehle committed Jun 10, 2022
1 parent a22e2c8 commit 5f79685
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 120 deletions.
4 changes: 2 additions & 2 deletions src/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use function is_int;
use function is_object;
use function is_string;
use function interface_exists;
use function method_exists;
use function property_exists;
use function sprintf;
Expand Down Expand Up @@ -316,7 +315,8 @@ public function hydrateValue(string $name, $value, ?array $data = null)
return null;
}

if (PHP_VERSION_ID >= 80100 && interface_exists(BackedEnum::class) && $value instanceof BackedEnum) {
// BackedEnum is available from PHP 8.1 on
if ($value instanceof BackedEnum) {
return $value;
}

Expand Down
38 changes: 0 additions & 38 deletions tests/Assets/SimpleEntityWithEnum.php

This file was deleted.

32 changes: 32 additions & 0 deletions tests/Assets/SimpleEntityWithEnumPhp81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DoctrineTest\Laminas\Hydrator\Assets;

class SimpleEntityWithEnumPhp81
{
protected int $id;

protected ?SimpleEnumPhp81 $enum = null;

public function setId(int $id): void
{
$this->id = $id;
}

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

public function setEnum(?SimpleEnumPhp81 $enum = null): void
{
$this->enum = $enum;
}

public function getEnum(): ?SimpleEnumPhp81
{
return $this->enum;
}
}
15 changes: 0 additions & 15 deletions tests/Assets/SimpleEnum.php

This file was deleted.

11 changes: 11 additions & 0 deletions tests/Assets/SimpleEnumPhp81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace DoctrineTest\Laminas\Hydrator\Assets;

enum SimpleEnumPhp81: int
{
case One = 1;
case Two = 2;
}
41 changes: 0 additions & 41 deletions tests/Assets/SimpleEnumStrategy.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/Assets/SimpleEnumStrategyPhp81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineTest\Laminas\Hydrator\Assets;

use Laminas\Hydrator\Strategy\StrategyInterface;

class SimpleEnumStrategyPhp81 implements StrategyInterface
{
/**
* @param mixed $value
*/
public function extract($value, ?object $object = null): ?int
{
if ($value === null) {
return null;
}

return SimpleEnumPhp81::tryFrom($value)->value;
}

/**
* @param mixed $value
* @param array<array-key, mixed>|null $data
*/
public function hydrate($value, ?array $data): ?SimpleEnumPhp81
{
if ($value === null) {
return null;
}

return SimpleEnumPhp81::tryFrom($value);
}
}
43 changes: 19 additions & 24 deletions tests/DoctrineObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Doctrine\Laminas\Hydrator\Strategy;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use DoctrineTest\Laminas\Hydrator\Assets\SimpleEnum;
use DoctrineTest\Laminas\Hydrator\Assets\SimpleEnumPhp81;
use InvalidArgumentException;
use Laminas\Hydrator\NamingStrategy\UnderscoreNamingStrategy;
use Laminas\Hydrator\Strategy\StrategyInterface;
Expand All @@ -29,8 +29,6 @@
use function implode;
use function time;

use const PHP_VERSION_ID;

class DoctrineObjectTest extends TestCase
{
use ProphecyTrait;
Expand Down Expand Up @@ -854,7 +852,7 @@ static function ($arg) {

public function configureObjectManagerForSimpleEntityWithEnum(): void
{
$refl = new ReflectionClass(Assets\SimpleEntityWithEnum::class);
$refl = new ReflectionClass(Assets\SimpleEntityWithEnumPhp81::class);

$this
->metadata
Expand Down Expand Up @@ -2928,57 +2926,54 @@ public function testNestedHydrationByReference(): void
$this->assertSame('2019-01-24 12:00:00', $entity->getCreatedAt()->format('Y-m-d H:i:s'));
}

/**
* @requires PHP 8.1
*/
public function testHandleEnumConversionUsingByValue(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('PHP 8.1 required for enum compatibility');
}

// When using hydration by value, it will use the public API of the entity to set values (setters)
$entity = new Assets\SimpleEntityWithEnum();
$entity = new Assets\SimpleEntityWithEnumPhp81();
$this->configureObjectManagerForSimpleEntityWithEnum();

$value = 1;
$data = ['enum' => $value];

$this->hydratorByValue->addStrategy('enum', new Assets\SimpleEnumStrategy());
$this->hydratorByValue->addStrategy('enum', new Assets\SimpleEnumStrategyPhp81());
$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertInstanceOf(SimpleEnum::class, $entity->getEnum());
$this->assertEquals(SimpleEnum::tryFrom($value), $entity->getEnum());
$this->assertInstanceOf(SimpleEnumPhp81::class, $entity->getEnum());
$this->assertEquals(SimpleEnumPhp81::tryFrom($value), $entity->getEnum());
}

/**
* @requires PHP 8.1
*/
public function testNullValueIsNotConvertedToEnum(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('PHP 8.1 required for enum compatibility');
}

$entity = new Assets\SimpleEntityWithEnum();
$entity = new Assets\SimpleEntityWithEnumPhp81();
$this->configureObjectManagerForSimpleEntityWithEnum();

$data = ['enum' => null];

$this->hydratorByValue->addStrategy('enum', new Assets\SimpleEnumStrategy());
$this->hydratorByValue->addStrategy('enum', new Assets\SimpleEnumStrategyPhp81());
$entity = $this->hydratorByValue->hydrate($data, $entity);

$this->assertNull($entity->getEnum());
}

/**
* @requires PHP 8.1
*/
public function testWrongEnumBackedValueThrowsException(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('PHP 8.1 required for enum compatibility');
}

$entity = new Assets\SimpleEntityWithEnum();
$entity = new Assets\SimpleEntityWithEnumPhp81();
$this->configureObjectManagerForSimpleEntityWithEnum();

$data = ['enum' => 'string'];

$this->expectException(TypeError::class);

$this->hydratorByValue->addStrategy('enum', new Assets\SimpleEnumStrategy());
$this->hydratorByValue->addStrategy('enum', new Assets\SimpleEnumStrategyPhp81());
$this->hydratorByValue->hydrate($data, $entity);
}
}

0 comments on commit 5f79685

Please sign in to comment.