|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\FieldHolder\Place\Acceptance; |
| 4 | + |
| 5 | +use App\Field\Domain\Enum\FieldCommunity; |
| 6 | +use App\Field\Domain\Enum\FieldPlace; |
| 7 | +use App\Field\Domain\Model\Field; |
| 8 | +use App\FieldHolder\Community\Domain\Enum\CommunityType; |
| 9 | +use App\Tests\Field\DummyFactory\DummyFieldFactory; |
| 10 | +use App\Tests\FieldHolder\Community\DummyFactory\DummyCommunityFactory; |
| 11 | +use App\Tests\FieldHolder\Place\DummyFactory\DummyPlaceFactory; |
| 12 | +use App\Tests\Helper\AcceptanceTestHelper; |
| 13 | +use Doctrine\Common\Collections\ArrayCollection; |
| 14 | +use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; |
| 15 | +use Zenstruck\Foundry\Test\Factories; |
| 16 | + |
| 17 | +class GetPlacesTest extends AcceptanceTestHelper |
| 18 | +{ |
| 19 | + use Factories; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testFilterByParentCommunityId(): void |
| 27 | + { |
| 28 | + [$community1, $community2] = DummyCommunityFactory::createMany(2, |
| 29 | + [ |
| 30 | + 'fields' => [ |
| 31 | + DummyFieldFactory::createOne([ |
| 32 | + 'name' => FieldCommunity::TYPE->value, |
| 33 | + Field::getPropertyName(FieldCommunity::TYPE) => CommunityType::PARISH->value, |
| 34 | + ]), |
| 35 | + ], |
| 36 | + ] |
| 37 | + ); |
| 38 | + |
| 39 | + $church1 = DummyPlaceFactory::createOne([ |
| 40 | + 'fields' => [ |
| 41 | + DummyFieldFactory::createOne([ |
| 42 | + 'name' => FieldPlace::PARENT_COMMUNITIES->value, |
| 43 | + Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community1->_real()]), |
| 44 | + ]), |
| 45 | + ], |
| 46 | + ]); |
| 47 | + |
| 48 | + $church2 = DummyPlaceFactory::createOne([ |
| 49 | + 'fields' => [ |
| 50 | + DummyFieldFactory::createOne([ |
| 51 | + 'name' => FieldPlace::PARENT_COMMUNITIES->value, |
| 52 | + Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community1->_real()]), |
| 53 | + ]), |
| 54 | + ], |
| 55 | + ]); |
| 56 | + |
| 57 | + $church3 = DummyPlaceFactory::createOne([ |
| 58 | + 'fields' => [ |
| 59 | + DummyFieldFactory::createOne([ |
| 60 | + 'name' => FieldPlace::PARENT_COMMUNITIES->value, |
| 61 | + Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community2->_real()]), |
| 62 | + ]), |
| 63 | + ], |
| 64 | + ]); |
| 65 | + |
| 66 | + $church4 = DummyPlaceFactory::createOne([ |
| 67 | + 'fields' => [ |
| 68 | + DummyFieldFactory::createOne([ |
| 69 | + 'name' => FieldPlace::PARENT_COMMUNITIES->value, |
| 70 | + Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community2->_real()]), |
| 71 | + ]), |
| 72 | + ], |
| 73 | + ]); |
| 74 | + |
| 75 | + $response = self::assertResponse($this->get('/places', querystring: [ |
| 76 | + FieldCommunity::PARENT_COMMUNITY_ID->value => $community1->id->toString(), |
| 77 | + ]), HttpFoundationResponse::HTTP_OK); |
| 78 | + $churchIds = array_map(fn (array $church) => $church['id'], $response); |
| 79 | + self::assertCount(2, $churchIds); |
| 80 | + self::assertContains($church1->id->toString(), $churchIds); |
| 81 | + self::assertContains($church2->id->toString(), $churchIds); |
| 82 | + |
| 83 | + $response = self::assertResponse($this->get('/places', querystring: [ |
| 84 | + FieldCommunity::PARENT_COMMUNITY_ID->value => $community2->id->toString(), |
| 85 | + ]), HttpFoundationResponse::HTTP_OK); |
| 86 | + $churchIds = array_map(fn (array $church) => $church['id'], $response); |
| 87 | + self::assertCount(2, $churchIds); |
| 88 | + self::assertContains($church3->id->toString(), $churchIds); |
| 89 | + self::assertContains($church4->id->toString(), $churchIds); |
| 90 | + } |
| 91 | + |
| 92 | + public function testShouldErrorIfParentCommunityIdNotAUuid(): void |
| 93 | + { |
| 94 | + $response = self::assertErrorResponse( |
| 95 | + $this->get('/places', querystring: [ |
| 96 | + FieldCommunity::PARENT_COMMUNITY_ID->value => 123, |
| 97 | + ]), |
| 98 | + HttpFoundationResponse::HTTP_BAD_REQUEST, |
| 99 | + sprintf('provided parentCommunityId %s is not a valid uuid', 123) |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments