Skip to content

Commit

Permalink
deduplicate loading enum to entity test cases using data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
petrduda committed Feb 27, 2023
1 parent e6ae5bc commit 1dc8b81
Showing 1 changed file with 32 additions and 46 deletions.
78 changes: 32 additions & 46 deletions tests/Enum/LoadEnumToEntityIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,63 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Tools\Setup;
use Generator;
use PHPUnit\Framework\Assert;

class LoadEnumToEntityIntegrationTest extends \PHPUnit\Framework\TestCase
{

public function testLoadEnumToEntity(): void
/**
* @return mixed[][]|\Generator
*/
public function loadEnumToEntityDataProvider(): Generator
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
}
yield 'entity' => [
'foo' => new FooEntity(),
];

public function testLoadNullEnumToEntity(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);
yield 'unserialized entity' => (function (): array {
$fooBeforeSerialization = new FooEntity();
$fooBeforeSerialization->setEnum(FooEnum::get(FooEnum::ONE));

Assert::assertNull($foo->getNullableEnum());
return [
'foo' => unserialize(serialize($fooBeforeSerialization)),
];
})();
}

public function testLoadEnumToUnserializedEntity(): void
/**
* @dataProvider loadEnumToEntityDataProvider
*
* @param \Consistence\Doctrine\Enum\FooEntity $foo
*/
public function testLoadEnumToEntity(FooEntity $foo): void
{
$fooBeforeSerialization = new FooEntity();
$fooBeforeSerialization->setEnum(FooEnum::get(FooEnum::ONE));

$foo = unserialize(serialize($fooBeforeSerialization));
$this->callPostLoadEventOnEntity($foo);

Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getWithoutNamespace());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEnum());
Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEmbedded()->getEnum());
Assert::assertNull($foo->getNullableEnum());
Assert::assertNull($foo->getNotLoadedEmbedded());
}

public function testMultipleLoadEvents(): void
/**
* @dataProvider loadEnumToEntityDataProvider
*
* @param \Consistence\Doctrine\Enum\FooEntity $foo
*/
public function testMultipleLoadEvents(FooEntity $foo): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);
$this->callPostLoadEventOnEntity($foo);

Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEnum());
}

public function testLoadEnumClassWithoutNamespace(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getWithoutNamespace());
}

public function testLoadEnumInEmbeddable(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEnum());
}

public function testLoadEnumInEmbeddableWeNeedToGoDeeper(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

Assert::assertSame(FooEnum::get(FooEnum::ONE), $foo->getEmbedded()->getEmbedded()->getEnum());
}

public function testLoadEnumInNotLoadedEmbeddable(): void
{
$foo = new FooEntity();
$this->callPostLoadEventOnEntity($foo);

Assert::assertNull($foo->getNullableEnum());
Assert::assertNull($foo->getNotLoadedEmbedded());
}

Expand Down

0 comments on commit 1dc8b81

Please sign in to comment.