Skip to content

Commit c02c65e

Browse files
committed
improve tests
1 parent 90f88b1 commit c02c65e

File tree

5 files changed

+70
-79
lines changed

5 files changed

+70
-79
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
1. Attention à la création des communautés / places !
22
Les fields et leurs entités doivent être persistés ensemble avant de flush
3+
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

src/Field/Application/FieldService.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use App\Field\Domain\Repository\FieldRepositoryInterface;
1616
use App\Place\Domain\Model\Place;
1717
use App\Place\Domain\Repository\PlaceRepositoryInterface;
18-
use Doctrine\Common\Collections\Collection;
1918
use Symfony\Bundle\SecurityBundle\Security;
2019
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2120
use Symfony\Component\Uid\Uuid;

tests/Community/Unit/CommunityTest.php tests/Community/Unit/CommunityUnitTest.php

+26-35
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
use Symfony\Component\Validator\Validator\ValidatorInterface;
1313
use Zenstruck\Foundry\Test\Factories;
1414

15-
use function Zenstruck\Foundry\Persistence\flush_after;
16-
17-
class CommunityTest extends KernelTestCase
15+
class CommunityUnitTest extends KernelTestCase
1816
{
1917
use Factories;
2018

@@ -27,8 +25,8 @@ protected function setUp(): void
2725

2826
public function testStateDeletedWithoutReason(): void
2927
{
30-
$community = DummyCommunityFactory::createOne(['fields' => [
31-
DummyFieldFactory::createOne([
28+
$community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [
29+
DummyFieldFactory::new()->withoutPersisting()->create([
3230
'name' => FieldCommunity::STATE->value, 'stringVal' => CommunityState::DELETED->value,
3331
]),
3432
]]);
@@ -40,8 +38,8 @@ public function testStateDeletedWithoutReason(): void
4038

4139
public function testCountryCodeValidation(): void
4240
{
43-
$community = DummyCommunityFactory::createOne(['fields' => [
44-
DummyFieldFactory::createOne([
41+
$community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [
42+
DummyFieldFactory::new()->withoutPersisting()->create([
4543
'name' => FieldCommunity::CONTACT_COUNTRY_CODE->value, 'stringVal' => -1,
4644
]),
4745
]]);
@@ -53,57 +51,55 @@ public function testCountryCodeValidation(): void
5351

5452
public function testGetMostTrustableFieldByName(): void
5553
{
56-
$community = flush_after(fn () => DummyCommunityFactory::createOne(['fields' => [
57-
DummyFieldFactory::createOne([
54+
$community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [
55+
DummyFieldFactory::new()->withoutPersisting()->create([
5856
'name' => FieldCommunity::NAME->value, 'stringVal' => 'low reliability name',
5957
'reliability' => FieldReliability::LOW,
6058
]),
61-
DummyFieldFactory::createOne([
59+
DummyFieldFactory::new()->withoutPersisting()->create([
6260
'name' => FieldCommunity::NAME->value, 'stringVal' => 'high reliability name',
6361
'reliability' => FieldReliability::HIGH,
6462
]),
65-
DummyFieldFactory::createOne([
63+
DummyFieldFactory::new()->withoutPersisting()->create([
6664
'name' => FieldCommunity::NAME->value, 'stringVal' => 'medium reliability name',
6765
'reliability' => FieldReliability::MEDIUM,
6866
]),
6967
],
70-
])->_real()
71-
);
68+
])->_real();
7269

7370
$result = $community->getMostTrustableFieldByName(FieldCommunity::NAME);
7471
static::assertEquals('high reliability name', $result->getValue());
7572

76-
$community = DummyCommunityFactory::createOne();
73+
$community = DummyCommunityFactory::new()->withoutPersisting()->create()->_real();
7774
$result = $community->getMostTrustableFieldByName(FieldCommunity::NAME);
7875
static::assertEmpty($result);
7976
}
8077

8178
public function testGetFieldsByName(): void
8279
{
83-
$community = flush_after(fn () => DummyCommunityFactory::createOne(['fields' => [
84-
DummyFieldFactory::createOne([
80+
$community = DummyCommunityFactory::new()->withoutPersisting()->create(['fields' => [
81+
DummyFieldFactory::new()->withoutPersisting()->create([
8582
'name' => FieldCommunity::NAME->value, 'stringVal' => 'low reliability name',
8683
'reliability' => FieldReliability::LOW,
8784
]),
88-
DummyFieldFactory::createOne([
85+
DummyFieldFactory::new()->withoutPersisting()->create([
8986
'name' => FieldCommunity::NAME->value, 'stringVal' => 'high reliability name',
9087
'reliability' => FieldReliability::HIGH,
9188
]),
92-
DummyFieldFactory::createOne([
89+
DummyFieldFactory::new()->withoutPersisting()->create([
9390
'name' => FieldCommunity::NAME->value, 'stringVal' => 'medium reliability name',
9491
'reliability' => FieldReliability::MEDIUM,
9592
]),
96-
DummyFieldFactory::createOne([
93+
DummyFieldFactory::new()->withoutPersisting()->create([
9794
'name' => FieldCommunity::CONTACT_ADDRESS->value, 'stringVal' => 'adress...',
9895
'reliability' => FieldReliability::MEDIUM,
9996
]),
100-
DummyFieldFactory::createOne([
97+
DummyFieldFactory::new()->withoutPersisting()->create([
10198
'name' => FieldCommunity::CONTACT_CITY->value, 'stringVal' => 'city...',
10299
'reliability' => FieldReliability::MEDIUM,
103100
]),
104101
],
105-
])->_real()
106-
);
102+
])->_real();
107103

108104
$results = $community->getFieldsByName(FieldCommunity::NAME);
109105
static::assertCount(3, $results);
@@ -115,19 +111,14 @@ public function testGetFieldsByName(): void
115111

116112
public function testRemoveField(): void
117113
{
118-
[$community, $field] = flush_after(function () {
119-
$field = DummyFieldFactory::createOne([
120-
'name' => FieldCommunity::NAME->value,
121-
Field::getPropertyName(FieldCommunity::NAME) => 'mon nom',
122-
])->_real();
123-
124-
return [
125-
DummyCommunityFactory::createOne([
126-
'fields' => [$field],
127-
])->_real(),
128-
$field,
129-
];
130-
});
114+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
115+
'name' => FieldCommunity::NAME->value,
116+
Field::getPropertyName(FieldCommunity::NAME) => 'mon nom',
117+
])->_real();
118+
119+
$community = DummyCommunityFactory::new()->withoutPersisting()->create([
120+
'fields' => [$field],
121+
])->_real();
131122

132123
static::assertCount(1, $community->fields);
133124
$community->removeField($field);

tests/Field/Unit/FieldTest.php tests/Field/Unit/FieldUnitTest.php

+25-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Symfony\Component\Validator\Validator\ValidatorInterface;
1313
use Zenstruck\Foundry\Test\Factories;
1414

15-
class FieldTest extends KernelTestCase
15+
class FieldUnitTest extends KernelTestCase
1616
{
1717
use Factories;
1818

@@ -25,23 +25,23 @@ protected function setUp(): void
2525

2626
public function testDefineCommunityOrPlace(): void
2727
{
28-
$field = DummyFieldFactory::createOne([
29-
'community' => DummyCommunityFactory::createOne(),
28+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
29+
'community' => DummyCommunityFactory::new()->withoutPersisting()->create(),
3030
'name' => FieldCommunity::NAME->value,
3131
]);
3232
$violations = $this->validator->validate($field);
3333
static::assertCount(0, $violations);
3434

35-
$field = DummyFieldFactory::createOne([
36-
'community' => DummyCommunityFactory::createOne(),
37-
'place' => DummyPlaceFactory::createOne(),
35+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
36+
'community' => DummyCommunityFactory::new()->withoutPersisting()->create(),
37+
'place' => DummyPlaceFactory::new()->withoutPersisting()->create(),
3838
'name' => FieldCommunity::NAME->value,
3939
]);
4040
$violations = $this->validator->validate($field);
4141
static::assertCount(1, $violations);
4242
static::assertEquals('Field must be attached to a community or a place, not none, not both', $violations->get(0)->getMessage());
4343

44-
$field = DummyFieldFactory::createOne([
44+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
4545
'name' => FieldCommunity::NAME->value,
4646
]);
4747
static::assertCount(1, $violations);
@@ -50,16 +50,16 @@ public function testDefineCommunityOrPlace(): void
5050

5151
public function testDefineWrongType(): void
5252
{
53-
$field = DummyFieldFactory::createOne([
54-
'community' => DummyCommunityFactory::createOne(),
53+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
54+
'community' => DummyCommunityFactory::new()->withoutPersisting()->create(),
5555
'name' => 'toto',
5656
]);
5757
$violations = $this->validator->validate($field);
5858
static::assertCount(1, $violations);
5959
static::assertEquals('Field toto is not acceptable', $violations->get(0)->getMessage());
6060

61-
$field = DummyFieldFactory::createOne([
62-
'place' => DummyPlaceFactory::createOne(),
61+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
62+
'place' => DummyPlaceFactory::new()->withoutPersisting()->create(),
6363
'name' => 'toto',
6464
]);
6565
$violations = $this->validator->validate($field);
@@ -69,50 +69,50 @@ public function testDefineWrongType(): void
6969

7070
public function testNotInsertCommunitiesInReplacesField(): void
7171
{
72-
$field = DummyFieldFactory::createOne([
73-
'community' => DummyCommunityFactory::createOne(),
72+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
73+
'community' => DummyCommunityFactory::new()->withoutPersisting()->create(),
7474
'name' => FieldCommunity::REPLACES->value,
75-
'value' => DummyCommunityFactory::createOne()->_real(),
75+
'value' => DummyCommunityFactory::new()->withoutPersisting()->create()->_real(),
7676
]);
7777
$violations = $this->validator->validate($field);
7878

7979
static::assertCount(1, $violations);
8080
static::assertEquals('Field replaces expected value of type Community[]', $violations->get(0)->getMessage());
8181

82-
$field = DummyFieldFactory::createOne([
83-
'community' => DummyCommunityFactory::createOne(),
82+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
83+
'community' => DummyCommunityFactory::new()->withoutPersisting()->create(),
8484
'name' => FieldCommunity::REPLACES->value,
85-
'value' => [DummyPlaceFactory::createOne()->_real()],
85+
'value' => [DummyPlaceFactory::new()->withoutPersisting()->create()->_real()],
8686
]);
8787
$violations = $this->validator->validate($field);
8888
static::assertEquals('Field replaces expected value of type Community[]', $violations->get(0)->getMessage());
8989
}
9090

9191
public function testNotInsertPlacesInReplacesField(): void
9292
{
93-
$field = DummyFieldFactory::createOne([
94-
'place' => DummyPlaceFactory::createOne(),
93+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
94+
'place' => DummyPlaceFactory::new()->withoutPersisting()->create(),
9595
'name' => FieldPlace::REPLACES->value,
96-
'value' => DummyPlaceFactory::createOne()->_real(),
96+
'value' => DummyPlaceFactory::new()->withoutPersisting()->create()->_real(),
9797
]);
9898
$violations = $this->validator->validate($field);
9999

100100
static::assertCount(1, $violations);
101101
static::assertEquals('Field replaces expected value of type Place[]', $violations->get(0)->getMessage());
102102

103-
$field = DummyFieldFactory::createOne([
104-
'place' => DummyPlaceFactory::createOne(),
103+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
104+
'place' => DummyPlaceFactory::new()->withoutPersisting()->create(),
105105
'name' => FieldPlace::REPLACES->value,
106-
'value' => [DummyCommunityFactory::createOne()->_real()],
106+
'value' => [DummyCommunityFactory::new()->withoutPersisting()->create()->_real()],
107107
]);
108108
$violations = $this->validator->validate($field);
109109
static::assertEquals('Field replaces expected value of type Place[]', $violations->get(0)->getMessage());
110110
}
111111

112112
public function testShouldFailIfValueNotInArray(): void
113113
{
114-
$field = DummyFieldFactory::createOne([
115-
'place' => DummyPlaceFactory::createOne(),
114+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
115+
'place' => DummyPlaceFactory::new()->withoutPersisting()->create(),
116116
'name' => FieldPlace::TYPE->value,
117117
'value' => 'toto',
118118
]);

tests/Place/Unit/PlaceTest.php tests/Place/Unit/PlaceUnitTest.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use function Zenstruck\Foundry\Persistence\flush_after;
1717

18-
class PlaceTest extends KernelTestCase
18+
class PlaceUnitTest extends KernelTestCase
1919
{
2020
use Factories;
2121

@@ -28,9 +28,9 @@ protected function setUp(): void
2828

2929
public function testDeletionReasonNotSet(): void
3030
{
31-
$place = DummyPlaceFactory::createOne([
31+
$place = DummyPlaceFactory::new()->withoutPersisting()->create([
3232
'fields' => [
33-
DummyFieldFactory::createOne([
33+
DummyFieldFactory::new()->withoutPersisting()->create([
3434
'name' => FieldPlace::STATE->value,
3535
Field::getPropertyName(FieldPlace::STATE) => PlaceState::DELETED->value,
3636
]),
@@ -43,9 +43,9 @@ public function testDeletionReasonNotSet(): void
4343

4444
public function testWrongCountryCode(): void
4545
{
46-
$place = DummyPlaceFactory::createOne([
46+
$place = DummyPlaceFactory::new()->withoutPersisting()->create([
4747
'fields' => [
48-
DummyFieldFactory::createOne([
48+
DummyFieldFactory::new()->withoutPersisting()->create([
4949
'name' => FieldPlace::COUNTRY_CODE->value,
5050
Field::getPropertyName(FieldPlace::COUNTRY_CODE) => -1,
5151
]),
@@ -59,16 +59,16 @@ public function testWrongCountryCode(): void
5959
public function testGetFieldsByName(): void
6060
{
6161
[$place, $field] = flush_after(function () {
62-
$field = DummyFieldFactory::createOne([
62+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
6363
'name' => FieldPlace::MESSESINFO_ID->value,
6464
Field::getPropertyName(FieldPlace::MESSESINFO_ID) => 123456,
6565
]);
6666

6767
return [
68-
DummyPlaceFactory::createOne([
68+
DummyPlaceFactory::new()->withoutPersisting()->create([
6969
'fields' => [
7070
$field,
71-
DummyFieldFactory::createOne([
71+
DummyFieldFactory::new()->withoutPersisting()->create([
7272
'name' => FieldPlace::NAME->value,
7373
Field::getPropertyName(FieldPlace::NAME) => 'mon nom',
7474
]),
@@ -84,22 +84,22 @@ public function testGetFieldsByName(): void
8484

8585
public function testGetFieldByNameAndAgent(): void
8686
{
87-
$agent = DummyAgentFactory::createOne();
88-
$place = flush_after(fn () => DummyPlaceFactory::createOne([
87+
$agent = DummyAgentFactory::new()->withoutPersisting()->create();
88+
$place = flush_after(fn () => DummyPlaceFactory::new()->withoutPersisting()->create([
8989
'fields' => [
90-
DummyFieldFactory::createOne([
90+
DummyFieldFactory::new()->withoutPersisting()->create([
9191
'name' => FieldPlace::NAME->value,
9292
Field::getPropertyName(FieldPlace::NAME) => 'mon nom',
9393
]),
94-
DummyFieldFactory::createOne([
94+
DummyFieldFactory::new()->withoutPersisting()->create([
9595
'name' => FieldPlace::MESSESINFO_ID->value,
9696
Field::getPropertyName(FieldPlace::MESSESINFO_ID) => 789456,
9797
'agent' => $agent,
9898
]),
99-
DummyFieldFactory::createOne([
99+
DummyFieldFactory::new()->withoutPersisting()->create([
100100
'name' => FieldPlace::MESSESINFO_ID->value,
101101
Field::getPropertyName(FieldPlace::MESSESINFO_ID) => 123456,
102-
'agent' => DummyAgentFactory::createOne(),
102+
'agent' => DummyAgentFactory::new()->withoutPersisting()->create(),
103103
]),
104104
],
105105
]),
@@ -111,8 +111,8 @@ public function testGetFieldByNameAndAgent(): void
111111

112112
public function testAddField(): void
113113
{
114-
$place = DummyPlaceFactory::createOne()->_real();
115-
$field = DummyFieldFactory::createOne([
114+
$place = DummyPlaceFactory::new()->withoutPersisting()->create()->_real();
115+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
116116
'name' => FieldPlace::NAME->value,
117117
Field::getPropertyName(FieldPlace::NAME) => 'mon nom',
118118
])->_real();
@@ -128,13 +128,13 @@ public function testAddField(): void
128128
public function testRemoveField(): void
129129
{
130130
[$place, $field] = flush_after(function () {
131-
$field = DummyFieldFactory::createOne([
131+
$field = DummyFieldFactory::new()->withoutPersisting()->create([
132132
'name' => FieldPlace::NAME->value,
133133
Field::getPropertyName(FieldPlace::NAME) => 'mon nom',
134134
])->_real();
135135

136136
return [
137-
DummyPlaceFactory::createOne([
137+
DummyPlaceFactory::new()->withoutPersisting()->create([
138138
'fields' => [$field],
139139
])->_real(),
140140
$field,

0 commit comments

Comments
 (0)