Skip to content

Commit 0f4c94c

Browse files
authored
feat(response): Add support for rel_uuids (#67)
1 parent 975d4f1 commit 0f4c94c

File tree

6 files changed

+92
-13
lines changed

6 files changed

+92
-13
lines changed

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ parameters:
7373
path: src/Bridge/Faker/Provider/StoryblokProvider.php
7474

7575
-
76-
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\.$#'
76+
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\.$#'
7777
identifier: return.type
7878
count: 1
7979
path: src/Bridge/Faker/Provider/StoryblokProvider.php
@@ -85,7 +85,7 @@ parameters:
8585
path: src/Bridge/Faker/Provider/StoryblokProvider.php
8686

8787
-
88-
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\.$#'
88+
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\.$#'
8989
identifier: return.type
9090
count: 1
9191
path: src/Bridge/Faker/Provider/StoryblokProvider.php

src/Bridge/Faker/Provider/StoryblokProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ final class StoryblokProvider extends BaseProvider
3131
* stories?: list<array<string, mixed>>,
3232
* links?: string[],
3333
* rels?: string[],
34+
* rel_uuids?: string[],
3435
* } $overrides
3536
*
3637
* @return array{
3738
* cv: integer,
3839
* stories: list<array<string, mixed>>,
3940
* links: string[],
41+
* rel_uuids: string[],
4042
* rels: string[],
4143
* }
4244
*/
@@ -46,6 +48,7 @@ public function storiesResponse(array $overrides = []): array
4648
'stories' => [],
4749
'cv' => $this->generator->randomNumber(),
4850
'rels' => [],
51+
'rel_uuids' => [],
4952
'links' => [],
5053
];
5154

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

65+
for ($i = 0; $this->generator->numberBetween(1, 5) > $i; ++$i) {
66+
$response['rel_uuids'][] = $this->generator->uuid();
67+
}
68+
6269
for ($i = 0; $this->generator->numberBetween(1, 5) > $i; ++$i) {
6370
$response['links'][] = $this->generator->url();
6471
}
@@ -75,13 +82,15 @@ public function storiesResponse(array $overrides = []): array
7582
* story?: array<string, mixed>,
7683
* links?: string[],
7784
* rels?: string[],
85+
* rel_uuids?: string[],
7886
* } $overrides
7987
*
8088
* @return array{
8189
* cv: integer,
8290
* story: array<string, mixed>,
8391
* links: string[],
8492
* rels: string[],
93+
* rel_uuids: string[],
8594
* }
8695
*/
8796
public function storyResponse(array $overrides = []): array
@@ -92,6 +101,7 @@ public function storyResponse(array $overrides = []): array
92101
],
93102
'cv' => $this->generator->randomNumber(),
94103
'rels' => [],
104+
'rel_uuids' => [],
95105
'links' => [],
96106
];
97107

src/Response/StoriesResponse.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
*/
3535
public array $rels;
3636

37+
/**
38+
* @var list<string>
39+
*/
40+
public array $relUuids;
41+
3742
/**
3843
* @var list<array<string, mixed>>
3944
*/
@@ -54,8 +59,21 @@ public function __construct(
5459
Assert::integer($values['cv']);
5560
$this->cv = $values['cv'];
5661

57-
Assert::keyExists($values, 'rels');
58-
$this->rels = $values['rels'];
62+
$rels = [];
63+
64+
if (\array_key_exists('rels', $values)) {
65+
$rels = $values['rels'];
66+
}
67+
68+
$this->rels = $rels;
69+
70+
$relUuids = [];
71+
72+
if (\array_key_exists('rel_uuids', $values)) {
73+
$relUuids = $values['rel_uuids'];
74+
}
75+
76+
$this->relUuids = $relUuids;
5977

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

src/Response/StoryResponse.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
*/
3333
public array $rels;
3434

35+
/**
36+
* @var list<string>
37+
*/
38+
public array $relUuids;
39+
3540
/**
3641
* @var list<array<string, mixed>>
3742
*/
@@ -49,8 +54,21 @@ public function __construct(array $values)
4954
Assert::integer($values['cv']);
5055
$this->cv = $values['cv'];
5156

52-
Assert::keyExists($values, 'rels');
53-
$this->rels = $values['rels'];
57+
$rels = [];
58+
59+
if (\array_key_exists('rels', $values)) {
60+
$rels = $values['rels'];
61+
}
62+
63+
$this->rels = $rels;
64+
65+
$relUuids = [];
66+
67+
if (\array_key_exists('rel_uuids', $values)) {
68+
$relUuids = $values['rel_uuids'];
69+
}
70+
71+
$this->relUuids = $relUuids;
5472

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

tests/Unit/Response/StoriesResponseTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,12 @@ public function rels(): void
9898
}
9999

100100
#[Test]
101-
public function relsKeyMustExist(): void
101+
public function relsKeyIsOptional(): void
102102
{
103103
$values = self::faker()->storiesResponse();
104104
unset($values['rels']);
105105

106-
self::expectException(\InvalidArgumentException::class);
107-
108-
new StoriesResponse(new Total(1), new Pagination(), $values);
106+
self::assertEmpty((new StoriesResponse(new Total(1), new Pagination(), $values))->rels);
109107
}
110108

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

130128
new StoriesResponse(new Total(1), new Pagination(), $values);
131129
}
130+
131+
#[Test]
132+
public function relUuids(): void
133+
{
134+
$values = self::faker()->storiesResponse();
135+
136+
self::assertCount(
137+
\count($values['rel_uuids']),
138+
(new StoriesResponse(new Total(1), new Pagination(), $values))->relUuids,
139+
);
140+
}
141+
142+
#[Test]
143+
public function relUuidsKeyIsOptional(): void
144+
{
145+
$values = self::faker()->storiesResponse();
146+
unset($values['rel_uuids']);
147+
148+
self::assertEmpty((new StoriesResponse(new Total(1), new Pagination(), $values))->relUuids);
149+
}
132150
}

tests/Unit/Response/StoryResponseTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,29 @@ public function rels(): void
7979
}
8080

8181
#[Test]
82-
public function relsKeyMustExist(): void
82+
public function relsIsOptional(): void
8383
{
8484
$values = self::faker()->storyResponse();
8585
unset($values['rels']);
8686

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

89-
new StoryResponse($values);
90+
#[Test]
91+
public function relUuids(): void
92+
{
93+
$values = self::faker()->storyResponse();
94+
95+
self::assertCount(\count($values['rel_uuids']), (new StoryResponse($values))->relUuids);
96+
}
97+
98+
#[Test]
99+
public function relsUuidsIsOptional(): void
100+
{
101+
$values = self::faker()->storyResponse();
102+
unset($values['rel_uuids']);
103+
104+
self::assertEmpty((new StoryResponse($values))->relUuids);
90105
}
91106

92107
#[Test]

0 commit comments

Comments
 (0)