-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dc4921
commit 7171ba8
Showing
10 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/Field/Infrastructure/ApiPlatform/Filter/FieldParentCommunityIdFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Field\Infrastructure\ApiPlatform\Filter; | ||
|
||
use ApiPlatform\Metadata\FilterInterface; | ||
use App\Field\Domain\Enum\FieldCommunity; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final class FieldParentCommunityIdFilter implements FilterInterface | ||
{ | ||
public function getDescription(string $resourceClass): array | ||
{ | ||
return [ | ||
'parentCommunityId' => [ | ||
'property' => FieldCommunity::PARENT_COMMUNITY_ID->value, | ||
'type' => Type::BUILTIN_TYPE_STRING, | ||
'required' => false, | ||
], | ||
]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Field/Infrastructure/ApiPlatform/Filter/FieldParentWikidataIdFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Field\Infrastructure\ApiPlatform\Filter; | ||
|
||
use ApiPlatform\Metadata\FilterInterface; | ||
use App\Field\Domain\Enum\FieldCommunity; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final class FieldParentWikidataIdFilter implements FilterInterface | ||
{ | ||
public function getDescription(string $resourceClass): array | ||
{ | ||
return [ | ||
'parentWikidataId' => [ | ||
'property' => FieldCommunity::PARENT_WIKIDATA_ID->value, | ||
'type' => Type::BUILTIN_TYPE_INT, | ||
'required' => false, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/FieldHolder/Place/Infrastructure/ApiPlatform/State/Provider/PlaceCollectionProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\FieldHolder\Place\Infrastructure\ApiPlatform\State\Provider; | ||
|
||
use ApiPlatform\Metadata\Exception\InvalidArgumentException; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\Pagination\Pagination; | ||
use ApiPlatform\State\ProviderInterface; | ||
use App\Field\Domain\Enum\FieldCommunity; | ||
use App\FieldHolder\Place\Domain\Repository\PlaceRepositoryInterface; | ||
use App\FieldHolder\Place\Infrastructure\ApiPlatform\Resource\PlaceResource; | ||
use App\Shared\Infrastructure\ApiPlatform\State\Paginator; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
/** | ||
* @implements ProviderInterface<PlaceResource> | ||
*/ | ||
final class PlaceCollectionProvider implements ProviderInterface | ||
{ | ||
public function __construct( | ||
private Pagination $pagination, | ||
private PlaceRepositoryInterface $placeRepo, | ||
) { | ||
} | ||
|
||
/** | ||
* @return Paginator<PlaceResource>|list<PlaceResource> | ||
*/ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): Paginator|array | ||
{ | ||
$parentCommunityId = $context['filters'][FieldCommunity::PARENT_COMMUNITY_ID->value] ?? null; | ||
if ($parentCommunityId && !Uuid::isValid($parentCommunityId)) { | ||
throw new InvalidArgumentException(sprintf('provided parentCommunityId %s is not a valid uuid', $parentCommunityId)); | ||
} | ||
|
||
$page = $itemsPerPage = null; | ||
|
||
if ($this->pagination->isEnabled($operation, $context)) { | ||
$page = $this->pagination->getPage($context); | ||
$itemsPerPage = $this->pagination->getLimit($operation, $context); | ||
} | ||
|
||
$models = $this->placeRepo | ||
->withParentCommunityId(Uuid::fromString($parentCommunityId)) | ||
->withPagination($page, $itemsPerPage); | ||
|
||
$resources = []; | ||
foreach ($models as $model) { | ||
$resources[] = PlaceResource::fromModel($model); | ||
} | ||
|
||
if (null !== $paginator = $models->paginator()) { | ||
$resources = new Paginator( | ||
new \ArrayIterator($resources), | ||
(float) $paginator->getCurrentPage(), | ||
(float) $paginator->getItemsPerPage(), | ||
(float) $paginator->getLastPage(), | ||
(float) $paginator->getTotalItems(), | ||
); | ||
} | ||
|
||
return $resources; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace App\Tests\FieldHolder\Place\Acceptance; | ||
|
||
use App\Field\Domain\Enum\FieldCommunity; | ||
use App\Field\Domain\Enum\FieldPlace; | ||
use App\Field\Domain\Model\Field; | ||
use App\FieldHolder\Community\Domain\Enum\CommunityType; | ||
use App\Tests\Field\DummyFactory\DummyFieldFactory; | ||
use App\Tests\FieldHolder\Community\DummyFactory\DummyCommunityFactory; | ||
use App\Tests\FieldHolder\Place\DummyFactory\DummyPlaceFactory; | ||
use App\Tests\Helper\AcceptanceTestHelper; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
class GetPlacesTest extends AcceptanceTestHelper | ||
{ | ||
use Factories; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function testFilterByParentCommunityId(): void | ||
{ | ||
[$community1, $community2] = DummyCommunityFactory::createMany(2, | ||
[ | ||
'fields' => [ | ||
DummyFieldFactory::createOne([ | ||
'name' => FieldCommunity::TYPE->value, | ||
Field::getPropertyName(FieldCommunity::TYPE) => CommunityType::PARISH->value, | ||
]), | ||
], | ||
] | ||
); | ||
|
||
$church1 = DummyPlaceFactory::createOne([ | ||
'fields' => [ | ||
DummyFieldFactory::createOne([ | ||
'name' => FieldPlace::PARENT_COMMUNITIES->value, | ||
Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community1->_real()]), | ||
]), | ||
], | ||
]); | ||
|
||
$church2 = DummyPlaceFactory::createOne([ | ||
'fields' => [ | ||
DummyFieldFactory::createOne([ | ||
'name' => FieldPlace::PARENT_COMMUNITIES->value, | ||
Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community1->_real()]), | ||
]), | ||
], | ||
]); | ||
|
||
$church3 = DummyPlaceFactory::createOne([ | ||
'fields' => [ | ||
DummyFieldFactory::createOne([ | ||
'name' => FieldPlace::PARENT_COMMUNITIES->value, | ||
Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community2->_real()]), | ||
]), | ||
], | ||
]); | ||
|
||
$church4 = DummyPlaceFactory::createOne([ | ||
'fields' => [ | ||
DummyFieldFactory::createOne([ | ||
'name' => FieldPlace::PARENT_COMMUNITIES->value, | ||
Field::getPropertyName(FieldPlace::PARENT_COMMUNITIES) => new ArrayCollection([$community2->_real()]), | ||
]), | ||
], | ||
]); | ||
|
||
$response = self::assertResponse($this->get('/places', querystring: [ | ||
FieldCommunity::PARENT_COMMUNITY_ID->value => $community1->id->toString(), | ||
]), HttpFoundationResponse::HTTP_OK); | ||
$churchIds = array_map(fn (array $church) => $church['id'], $response); | ||
self::assertCount(2, $churchIds); | ||
self::assertContains($church1->id->toString(), $churchIds); | ||
self::assertContains($church2->id->toString(), $churchIds); | ||
|
||
$response = self::assertResponse($this->get('/places', querystring: [ | ||
FieldCommunity::PARENT_COMMUNITY_ID->value => $community2->id->toString(), | ||
]), HttpFoundationResponse::HTTP_OK); | ||
$churchIds = array_map(fn (array $church) => $church['id'], $response); | ||
self::assertCount(2, $churchIds); | ||
self::assertContains($church3->id->toString(), $churchIds); | ||
self::assertContains($church4->id->toString(), $churchIds); | ||
} | ||
|
||
public function testShouldErrorIfParentCommunityIdNotAUuid(): void | ||
{ | ||
$response = self::assertErrorResponse( | ||
$this->get('/places', querystring: [ | ||
FieldCommunity::PARENT_COMMUNITY_ID->value => 123, | ||
]), | ||
HttpFoundationResponse::HTTP_BAD_REQUEST, | ||
sprintf('provided parentCommunityId %s is not a valid uuid', 123) | ||
); | ||
} | ||
} |