From 3386b9daa0daf39c2042778f96a1aa88ce382d76 Mon Sep 17 00:00:00 2001 From: Petr Duda Date: Mon, 27 Feb 2023 10:33:27 +0100 Subject: [PATCH] deduplicate loading enum to entity test cases using data provider --- .../Enum/LoadEnumToEntityIntegrationTest.php | 78 ++++++++----------- 1 file changed, 32 insertions(+), 46 deletions(-) diff --git a/tests/Enum/LoadEnumToEntityIntegrationTest.php b/tests/Enum/LoadEnumToEntityIntegrationTest.php index 15b2b69..a7b6933 100644 --- a/tests/Enum/LoadEnumToEntityIntegrationTest.php +++ b/tests/Enum/LoadEnumToEntityIntegrationTest.php @@ -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()); }