Skip to content

feat(response): Add support for rel_uuids #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ parameters:
path: src/Bridge/Faker/Provider/StoryblokProvider.php

-
message: '#^Method Storyblok\\Api\\Bridge\\Faker\\Provider\\StoryblokProvider\:\:storiesResponse\(\) should return array\{cv\: int, stories\: list\<array\<string, mixed\>\>, links\: array\<string\>, rels\: array\<string\>\} but returns array\.$#'
message: '#^Method Storyblok\\Api\\Bridge\\Faker\\Provider\\StoryblokProvider\:\:storiesResponse\(\) should return array\{cv\: int, stories\: list\<array\<string, mixed\>\>, links\: array\<string\>, rel_uuids\: array\<string\>, rels\: array\<string\>\} but returns array\.$#'
identifier: return.type
count: 1
path: src/Bridge/Faker/Provider/StoryblokProvider.php
Expand All @@ -85,7 +85,7 @@ parameters:
path: src/Bridge/Faker/Provider/StoryblokProvider.php

-
message: '#^Method Storyblok\\Api\\Bridge\\Faker\\Provider\\StoryblokProvider\:\:storyResponse\(\) should return array\{cv\: int, story\: array\<string, mixed\>, links\: array\<string\>, rels\: array\<string\>\} but returns array\.$#'
message: '#^Method Storyblok\\Api\\Bridge\\Faker\\Provider\\StoryblokProvider\:\:storyResponse\(\) should return array\{cv\: int, story\: array\<string, mixed\>, links\: array\<string\>, rels\: array\<string\>, rel_uuids\: array\<string\>\} but returns array\.$#'
identifier: return.type
count: 1
path: src/Bridge/Faker/Provider/StoryblokProvider.php
Expand Down
10 changes: 10 additions & 0 deletions src/Bridge/Faker/Provider/StoryblokProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ final class StoryblokProvider extends BaseProvider
* stories?: list<array<string, mixed>>,
* links?: string[],
* rels?: string[],
* rel_uuids?: string[],
* } $overrides
*
* @return array{
* cv: integer,
* stories: list<array<string, mixed>>,
* links: string[],
* rel_uuids: string[],
* rels: string[],
* }
*/
Expand All @@ -46,6 +48,7 @@ public function storiesResponse(array $overrides = []): array
'stories' => [],
'cv' => $this->generator->randomNumber(),
'rels' => [],
'rel_uuids' => [],
'links' => [],
];

Expand All @@ -59,6 +62,10 @@ public function storiesResponse(array $overrides = []): array
$response['rels'][] = $this->generator->url();
}

for ($i = 0; $this->generator->numberBetween(1, 5) > $i; ++$i) {
$response['rel_uuids'][] = $this->generator->uuid();
}

for ($i = 0; $this->generator->numberBetween(1, 5) > $i; ++$i) {
$response['links'][] = $this->generator->url();
}
Expand All @@ -75,13 +82,15 @@ public function storiesResponse(array $overrides = []): array
* story?: array<string, mixed>,
* links?: string[],
* rels?: string[],
* rel_uuids?: string[],
* } $overrides
*
* @return array{
* cv: integer,
* story: array<string, mixed>,
* links: string[],
* rels: string[],
* rel_uuids: string[],
* }
*/
public function storyResponse(array $overrides = []): array
Expand All @@ -92,6 +101,7 @@ public function storyResponse(array $overrides = []): array
],
'cv' => $this->generator->randomNumber(),
'rels' => [],
'rel_uuids' => [],
'links' => [],
];

Expand Down
22 changes: 20 additions & 2 deletions src/Response/StoriesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
*/
public array $rels;

/**
* @var list<string>
*/
public array $relUuids;

/**
* @var list<array<string, mixed>>
*/
Expand All @@ -54,8 +59,21 @@ public function __construct(
Assert::integer($values['cv']);
$this->cv = $values['cv'];

Assert::keyExists($values, 'rels');
$this->rels = $values['rels'];
$rels = [];

if (\array_key_exists('rels', $values)) {
$rels = $values['rels'];
}

$this->rels = $rels;

$relUuids = [];

if (\array_key_exists('rel_uuids', $values)) {
$relUuids = $values['rel_uuids'];
}

$this->relUuids = $relUuids;

Assert::keyExists($values, 'links');
$this->links = $values['links'];
Expand Down
22 changes: 20 additions & 2 deletions src/Response/StoryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
*/
public array $rels;

/**
* @var list<string>
*/
public array $relUuids;

/**
* @var list<array<string, mixed>>
*/
Expand All @@ -49,8 +54,21 @@ public function __construct(array $values)
Assert::integer($values['cv']);
$this->cv = $values['cv'];

Assert::keyExists($values, 'rels');
$this->rels = $values['rels'];
$rels = [];

if (\array_key_exists('rels', $values)) {
$rels = $values['rels'];
}

$this->rels = $rels;

$relUuids = [];

if (\array_key_exists('rel_uuids', $values)) {
$relUuids = $values['rel_uuids'];
}

$this->relUuids = $relUuids;

Assert::keyExists($values, 'links');
$this->links = $values['links'];
Expand Down
26 changes: 22 additions & 4 deletions tests/Unit/Response/StoriesResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ public function rels(): void
}

#[Test]
public function relsKeyMustExist(): void
public function relsKeyIsOptional(): void
{
$values = self::faker()->storiesResponse();
unset($values['rels']);

self::expectException(\InvalidArgumentException::class);

new StoriesResponse(new Total(1), new Pagination(), $values);
self::assertEmpty((new StoriesResponse(new Total(1), new Pagination(), $values))->rels);
}

#[Test]
Expand All @@ -129,4 +127,24 @@ public function linksKeyMustExist(): void

new StoriesResponse(new Total(1), new Pagination(), $values);
}

#[Test]
public function relUuids(): void
{
$values = self::faker()->storiesResponse();

self::assertCount(
\count($values['rel_uuids']),
(new StoriesResponse(new Total(1), new Pagination(), $values))->relUuids,
);
}

#[Test]
public function relUuidsKeyIsOptional(): void
{
$values = self::faker()->storiesResponse();
unset($values['rel_uuids']);

self::assertEmpty((new StoriesResponse(new Total(1), new Pagination(), $values))->relUuids);
}
}
21 changes: 18 additions & 3 deletions tests/Unit/Response/StoryResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,29 @@ public function rels(): void
}

#[Test]
public function relsKeyMustExist(): void
public function relsIsOptional(): void
{
$values = self::faker()->storyResponse();
unset($values['rels']);

self::expectException(\InvalidArgumentException::class);
self::assertEmpty((new StoryResponse($values))->rels);
}

new StoryResponse($values);
#[Test]
public function relUuids(): void
{
$values = self::faker()->storyResponse();

self::assertCount(\count($values['rel_uuids']), (new StoryResponse($values))->relUuids);
}

#[Test]
public function relsUuidsIsOptional(): void
{
$values = self::faker()->storyResponse();
unset($values['rel_uuids']);

self::assertEmpty((new StoryResponse($values))->relUuids);
}

#[Test]
Expand Down