Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bailletced committed Dec 12, 2024
1 parent 90f88b1 commit c02c65e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 79 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion src/Field/Application/FieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
]),
]]);
Expand All @@ -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,
]),
]]);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -69,50 +69,50 @@ 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());
}

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());
}

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',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use function Zenstruck\Foundry\Persistence\flush_after;

class PlaceTest extends KernelTestCase
class PlaceUnitTest extends KernelTestCase
{
use Factories;

Expand All @@ -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,
]),
Expand All @@ -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,
]),
Expand All @@ -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',
]),
Expand All @@ -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(),
]),
],
]),
Expand All @@ -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();
Expand All @@ -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,
Expand Down

0 comments on commit c02c65e

Please sign in to comment.