From c02c65ee7e843d10e415972a0ad9c281ec74f225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Baillet?= Date: Thu, 12 Dec 2024 14:11:07 +0100 Subject: [PATCH] improve tests --- README.md | 1 + src/Field/Application/FieldService.php | 1 - ...ommunityTest.php => CommunityUnitTest.php} | 61 ++++++++----------- .../Unit/{FieldTest.php => FieldUnitTest.php} | 50 +++++++-------- .../Unit/{PlaceTest.php => PlaceUnitTest.php} | 36 +++++------ 5 files changed, 70 insertions(+), 79 deletions(-) rename tests/Community/Unit/{CommunityTest.php => CommunityUnitTest.php} (70%) rename tests/Field/Unit/{FieldTest.php => FieldUnitTest.php} (64%) rename tests/Place/Unit/{PlaceTest.php => PlaceUnitTest.php} (76%) diff --git a/README.md b/README.md index 1b8530c1..bd58f04e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ 1. Attention à la création des communautés / places ! Les fields et leurs entités doivent être persistés ensemble avant de flush +2. Comme nous n'avons pas encore de moyens de supprimer des entités (depuis endpoint ou admin), la synchro elastic pour la suppression n'a pas encore été implémentée diff --git a/src/Field/Application/FieldService.php b/src/Field/Application/FieldService.php index e114f4bf..61288084 100644 --- a/src/Field/Application/FieldService.php +++ b/src/Field/Application/FieldService.php @@ -15,7 +15,6 @@ use App\Field\Domain\Repository\FieldRepositoryInterface; use App\Place\Domain\Model\Place; use App\Place\Domain\Repository\PlaceRepositoryInterface; -use Doctrine\Common\Collections\Collection; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Uid\Uuid; diff --git a/tests/Community/Unit/CommunityTest.php b/tests/Community/Unit/CommunityUnitTest.php similarity index 70% rename from tests/Community/Unit/CommunityTest.php rename to tests/Community/Unit/CommunityUnitTest.php index c63490b4..494b3a6a 100644 --- a/tests/Community/Unit/CommunityTest.php +++ b/tests/Community/Unit/CommunityUnitTest.php @@ -12,9 +12,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface; use Zenstruck\Foundry\Test\Factories; -use function Zenstruck\Foundry\Persistence\flush_after; - -class CommunityTest extends KernelTestCase +class CommunityUnitTest extends KernelTestCase { use Factories; @@ -27,8 +25,8 @@ protected function setUp(): void public function testStateDeletedWithoutReason(): void { - $community = DummyCommunityFactory::createOne(['fields' => [ - DummyFieldFactory::createOne([ + $community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::STATE->value, 'stringVal' => CommunityState::DELETED->value, ]), ]]); @@ -40,8 +38,8 @@ public function testStateDeletedWithoutReason(): void public function testCountryCodeValidation(): void { - $community = DummyCommunityFactory::createOne(['fields' => [ - DummyFieldFactory::createOne([ + $community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::CONTACT_COUNTRY_CODE->value, 'stringVal' => -1, ]), ]]); @@ -53,57 +51,55 @@ public function testCountryCodeValidation(): void public function testGetMostTrustableFieldByName(): void { - $community = flush_after(fn () => DummyCommunityFactory::createOne(['fields' => [ - DummyFieldFactory::createOne([ + $community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, 'stringVal' => 'low reliability name', 'reliability' => FieldReliability::LOW, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, 'stringVal' => 'high reliability name', 'reliability' => FieldReliability::HIGH, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, 'stringVal' => 'medium reliability name', 'reliability' => FieldReliability::MEDIUM, ]), ], - ])->_real() - ); + ])->_real(); $result = $community->getMostTrustableFieldByName(FieldCommunity::NAME); static::assertEquals('high reliability name', $result->getValue()); - $community = DummyCommunityFactory::createOne(); + $community = DummyCommunityFactory::new()->withoutPersisting()->create()->_real(); $result = $community->getMostTrustableFieldByName(FieldCommunity::NAME); static::assertEmpty($result); } public function testGetFieldsByName(): void { - $community = flush_after(fn () => DummyCommunityFactory::createOne(['fields' => [ - DummyFieldFactory::createOne([ + $community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, 'stringVal' => 'low reliability name', 'reliability' => FieldReliability::LOW, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, 'stringVal' => 'high reliability name', 'reliability' => FieldReliability::HIGH, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, 'stringVal' => 'medium reliability name', 'reliability' => FieldReliability::MEDIUM, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::CONTACT_ADDRESS->value, 'stringVal' => 'adress...', 'reliability' => FieldReliability::MEDIUM, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::CONTACT_CITY->value, 'stringVal' => 'city...', 'reliability' => FieldReliability::MEDIUM, ]), ], - ])->_real() - ); + ])->_real(); $results = $community->getFieldsByName(FieldCommunity::NAME); static::assertCount(3, $results); @@ -115,19 +111,14 @@ public function testGetFieldsByName(): void public function testRemoveField(): void { - [$community, $field] = flush_after(function () { - $field = DummyFieldFactory::createOne([ - 'name' => FieldCommunity::NAME->value, - Field::getPropertyName(FieldCommunity::NAME) => 'mon nom', - ])->_real(); - - return [ - DummyCommunityFactory::createOne([ - 'fields' => [$field], - ])->_real(), - $field, - ]; - }); + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'name' => FieldCommunity::NAME->value, + Field::getPropertyName(FieldCommunity::NAME) => 'mon nom', + ])->_real(); + + $community = DummyCommunityFactory::new()->withoutPersisting()->create([ + 'fields' => [$field], + ])->_real(); static::assertCount(1, $community->fields); $community->removeField($field); diff --git a/tests/Field/Unit/FieldTest.php b/tests/Field/Unit/FieldUnitTest.php similarity index 64% rename from tests/Field/Unit/FieldTest.php rename to tests/Field/Unit/FieldUnitTest.php index c8a13df4..894aa724 100644 --- a/tests/Field/Unit/FieldTest.php +++ b/tests/Field/Unit/FieldUnitTest.php @@ -12,7 +12,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface; use Zenstruck\Foundry\Test\Factories; -class FieldTest extends KernelTestCase +class FieldUnitTest extends KernelTestCase { use Factories; @@ -25,23 +25,23 @@ protected function setUp(): void public function testDefineCommunityOrPlace(): void { - $field = DummyFieldFactory::createOne([ - 'community' => DummyCommunityFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'community' => DummyCommunityFactory::new()->withoutPersisting()->create(), 'name' => FieldCommunity::NAME->value, ]); $violations = $this->validator->validate($field); static::assertCount(0, $violations); - $field = DummyFieldFactory::createOne([ - 'community' => DummyCommunityFactory::createOne(), - 'place' => DummyPlaceFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'community' => DummyCommunityFactory::new()->withoutPersisting()->create(), + 'place' => DummyPlaceFactory::new()->withoutPersisting()->create(), 'name' => FieldCommunity::NAME->value, ]); $violations = $this->validator->validate($field); static::assertCount(1, $violations); static::assertEquals('Field must be attached to a community or a place, not none, not both', $violations->get(0)->getMessage()); - $field = DummyFieldFactory::createOne([ + $field = DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldCommunity::NAME->value, ]); static::assertCount(1, $violations); @@ -50,16 +50,16 @@ public function testDefineCommunityOrPlace(): void public function testDefineWrongType(): void { - $field = DummyFieldFactory::createOne([ - 'community' => DummyCommunityFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'community' => DummyCommunityFactory::new()->withoutPersisting()->create(), 'name' => 'toto', ]); $violations = $this->validator->validate($field); static::assertCount(1, $violations); static::assertEquals('Field toto is not acceptable', $violations->get(0)->getMessage()); - $field = DummyFieldFactory::createOne([ - 'place' => DummyPlaceFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'place' => DummyPlaceFactory::new()->withoutPersisting()->create(), 'name' => 'toto', ]); $violations = $this->validator->validate($field); @@ -69,20 +69,20 @@ public function testDefineWrongType(): void public function testNotInsertCommunitiesInReplacesField(): void { - $field = DummyFieldFactory::createOne([ - 'community' => DummyCommunityFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'community' => DummyCommunityFactory::new()->withoutPersisting()->create(), 'name' => FieldCommunity::REPLACES->value, - 'value' => DummyCommunityFactory::createOne()->_real(), + 'value' => DummyCommunityFactory::new()->withoutPersisting()->create()->_real(), ]); $violations = $this->validator->validate($field); static::assertCount(1, $violations); static::assertEquals('Field replaces expected value of type Community[]', $violations->get(0)->getMessage()); - $field = DummyFieldFactory::createOne([ - 'community' => DummyCommunityFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'community' => DummyCommunityFactory::new()->withoutPersisting()->create(), 'name' => FieldCommunity::REPLACES->value, - 'value' => [DummyPlaceFactory::createOne()->_real()], + 'value' => [DummyPlaceFactory::new()->withoutPersisting()->create()->_real()], ]); $violations = $this->validator->validate($field); static::assertEquals('Field replaces expected value of type Community[]', $violations->get(0)->getMessage()); @@ -90,20 +90,20 @@ public function testNotInsertCommunitiesInReplacesField(): void public function testNotInsertPlacesInReplacesField(): void { - $field = DummyFieldFactory::createOne([ - 'place' => DummyPlaceFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'place' => DummyPlaceFactory::new()->withoutPersisting()->create(), 'name' => FieldPlace::REPLACES->value, - 'value' => DummyPlaceFactory::createOne()->_real(), + 'value' => DummyPlaceFactory::new()->withoutPersisting()->create()->_real(), ]); $violations = $this->validator->validate($field); static::assertCount(1, $violations); static::assertEquals('Field replaces expected value of type Place[]', $violations->get(0)->getMessage()); - $field = DummyFieldFactory::createOne([ - 'place' => DummyPlaceFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'place' => DummyPlaceFactory::new()->withoutPersisting()->create(), 'name' => FieldPlace::REPLACES->value, - 'value' => [DummyCommunityFactory::createOne()->_real()], + 'value' => [DummyCommunityFactory::new()->withoutPersisting()->create()->_real()], ]); $violations = $this->validator->validate($field); static::assertEquals('Field replaces expected value of type Place[]', $violations->get(0)->getMessage()); @@ -111,8 +111,8 @@ public function testNotInsertPlacesInReplacesField(): void public function testShouldFailIfValueNotInArray(): void { - $field = DummyFieldFactory::createOne([ - 'place' => DummyPlaceFactory::createOne(), + $field = DummyFieldFactory::new()->withoutPersisting()->create([ + 'place' => DummyPlaceFactory::new()->withoutPersisting()->create(), 'name' => FieldPlace::TYPE->value, 'value' => 'toto', ]); diff --git a/tests/Place/Unit/PlaceTest.php b/tests/Place/Unit/PlaceUnitTest.php similarity index 76% rename from tests/Place/Unit/PlaceTest.php rename to tests/Place/Unit/PlaceUnitTest.php index 21efb23d..081d5849 100644 --- a/tests/Place/Unit/PlaceTest.php +++ b/tests/Place/Unit/PlaceUnitTest.php @@ -15,7 +15,7 @@ use function Zenstruck\Foundry\Persistence\flush_after; -class PlaceTest extends KernelTestCase +class PlaceUnitTest extends KernelTestCase { use Factories; @@ -28,9 +28,9 @@ protected function setUp(): void public function testDeletionReasonNotSet(): void { - $place = DummyPlaceFactory::createOne([ + $place = DummyPlaceFactory::new()->withoutPersisting()->create([ 'fields' => [ - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::STATE->value, Field::getPropertyName(FieldPlace::STATE) => PlaceState::DELETED->value, ]), @@ -43,9 +43,9 @@ public function testDeletionReasonNotSet(): void public function testWrongCountryCode(): void { - $place = DummyPlaceFactory::createOne([ + $place = DummyPlaceFactory::new()->withoutPersisting()->create([ 'fields' => [ - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::COUNTRY_CODE->value, Field::getPropertyName(FieldPlace::COUNTRY_CODE) => -1, ]), @@ -59,16 +59,16 @@ public function testWrongCountryCode(): void public function testGetFieldsByName(): void { [$place, $field] = flush_after(function () { - $field = DummyFieldFactory::createOne([ + $field = DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::MESSESINFO_ID->value, Field::getPropertyName(FieldPlace::MESSESINFO_ID) => 123456, ]); return [ - DummyPlaceFactory::createOne([ + DummyPlaceFactory::new()->withoutPersisting()->create([ 'fields' => [ $field, - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::NAME->value, Field::getPropertyName(FieldPlace::NAME) => 'mon nom', ]), @@ -84,22 +84,22 @@ public function testGetFieldsByName(): void public function testGetFieldByNameAndAgent(): void { - $agent = DummyAgentFactory::createOne(); - $place = flush_after(fn () => DummyPlaceFactory::createOne([ + $agent = DummyAgentFactory::new()->withoutPersisting()->create(); + $place = flush_after(fn () => DummyPlaceFactory::new()->withoutPersisting()->create([ 'fields' => [ - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::NAME->value, Field::getPropertyName(FieldPlace::NAME) => 'mon nom', ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::MESSESINFO_ID->value, Field::getPropertyName(FieldPlace::MESSESINFO_ID) => 789456, 'agent' => $agent, ]), - DummyFieldFactory::createOne([ + DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::MESSESINFO_ID->value, Field::getPropertyName(FieldPlace::MESSESINFO_ID) => 123456, - 'agent' => DummyAgentFactory::createOne(), + 'agent' => DummyAgentFactory::new()->withoutPersisting()->create(), ]), ], ]), @@ -111,8 +111,8 @@ public function testGetFieldByNameAndAgent(): void public function testAddField(): void { - $place = DummyPlaceFactory::createOne()->_real(); - $field = DummyFieldFactory::createOne([ + $place = DummyPlaceFactory::new()->withoutPersisting()->create()->_real(); + $field = DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::NAME->value, Field::getPropertyName(FieldPlace::NAME) => 'mon nom', ])->_real(); @@ -128,13 +128,13 @@ public function testAddField(): void public function testRemoveField(): void { [$place, $field] = flush_after(function () { - $field = DummyFieldFactory::createOne([ + $field = DummyFieldFactory::new()->withoutPersisting()->create([ 'name' => FieldPlace::NAME->value, Field::getPropertyName(FieldPlace::NAME) => 'mon nom', ])->_real(); return [ - DummyPlaceFactory::createOne([ + DummyPlaceFactory::new()->withoutPersisting()->create([ 'fields' => [$field], ])->_real(), $field,