[Behat] Refactor subresources removal

This commit is contained in:
Rafikooo 2024-08-13 15:42:46 +02:00 committed by Grzegorz Sadowski
parent a6b8c46d0a
commit 9bd483ff25
6 changed files with 39 additions and 8 deletions

View file

@ -73,7 +73,9 @@ interface ApiClientInterface
public function addSubResourceData(string $key, array $data): void;
public function removeSubResource(string $subResource, string $id): void;
public function removeSubResourceIri(string $subResourceKey, string $iri): void;
public function removeSubResourceObject(string $subResourceKey, string $value, string $key = '@id'): void;
public function updateRequestData(array $data): void;

View file

@ -231,9 +231,14 @@ final class ApiPlatformClient implements ApiClientInterface
$this->request->addSubResource($key, $data);
}
public function removeSubResource(string $subResource, string $id): void
public function removeSubResourceIri(string $subResourceKey, string $iri): void
{
$this->request->removeSubResource($subResource, $id);
$this->request->removeSubResource($subResourceKey, $iri);
}
public function removeSubResourceObject(string $subResourceKey, string $value, string $key = '@id'): void
{
$this->request->removeSubResource($subResourceKey, $value, $key);
}
public function getContent(): array

View file

@ -95,11 +95,19 @@ final class Request implements RequestInterface
$this->content[$key][] = $subResource;
}
public function removeSubResource(string $subResource, string $id): void
public function removeSubResource(string $subResourceKey, string $value, string $key = '@id'): void
{
foreach ($this->content[$subResource] as $key => $resource) {
if ($resource === $id) {
unset($this->content[$subResource][$key]);
foreach ($this->content[$subResourceKey] as $index => $objectOrIri) {
if (is_array($objectOrIri)) {
if (isset($objectOrIri[$key]) && $objectOrIri[$key] === $value) {
unset($this->content[$subResourceKey][$index]);
}
continue;
}
if ($objectOrIri === $value) {
unset($this->content[$subResourceKey][$index]);
}
}
}

View file

@ -43,7 +43,7 @@ interface RequestInterface
public function addSubResource(string $key, array $subResource): void;
public function removeSubResource(string $subResource, string $id): void;
public function removeSubResource(string $subResourceKey, string $value, string $key = '@id'): void;
public function authorize(?string $token, string $authorizationHeader): void;
}

View file

@ -208,6 +208,17 @@ final class ResponseChecker implements ResponseCheckerInterface
return false;
}
public function hasItemWithTranslationInCollection(array $items, string $locale, string $key, string $translation): bool
{
foreach ($items as $item) {
if (isset($item['translations'][$locale]) && $item['translations'][$locale][$key] === $translation) {
return true;
}
}
return false;
}
public function hasKey(Response $response, string $key): bool
{
$content = json_decode($response->getContent(), true);

View file

@ -76,6 +76,11 @@ interface ResponseCheckerInterface
public function hasItemWithTranslation(Response $response, string $locale, string $key, string $translation): bool;
/**
* @param array<array-key, array> $items
*/
public function hasItemWithTranslationInCollection(array $items, string $locale, string $key, string $translation): bool;
public function hasKey(Response $response, string $key): bool;
public function hasTranslation(Response $response, string $locale, string $key, string $translation): bool;