Skip to content

Commit 6832c33

Browse files
committed
Improving StoryApi and StoryBulkApi test coverage
1 parent 4182990 commit 6832c33

File tree

5 files changed

+147
-18
lines changed

5 files changed

+147
-18
lines changed

src/Data/TagData.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function name(): string
2424
{
2525
return $this->getString('name');
2626
}
27+
2728
public function id(): string
2829
{
2930
return $this->getString('id');

src/Endpoints/StoryBulkApi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ private function handleSuccessfulResponse(
145145
int $itemsPerPage,
146146
): int {
147147
if ($totalPages === null) {
148+
148149
$totalPages = (int) ceil($response->total() / $itemsPerPage);
149150
$this->logger->info('Total stories found: ' . $response->total());
150151
}

tests/Feature/StoryBulkTest.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use Storyblok\ManagementApi\Endpoints\StoryBulkApi;
56
use Storyblok\ManagementApi\ManagementApiClient;
67
use Storyblok\ManagementApi\Data\StoryData;
78
use Storyblok\ManagementApi\QueryParameters\StoriesParams;
@@ -50,7 +51,7 @@ public function error(string|\Stringable $message, array $context = []): void
5051

5152
$client = new MockHttpClient($responses);
5253
$mapiClient = ManagementApiClient::initTest($client);
53-
$storyBulkApi = $mapiClient->storyBulkApi('222', $mockLogger);
54+
$storyBulkApi = new TestStoryBulkApi($mapiClient, '222', $mockLogger);
5455

5556
// Use the all() method which we know triggers logging
5657
try {
@@ -67,31 +68,61 @@ public function error(string|\Stringable $message, array $context = []): void
6768

6869
test('Testing list of stories, Params', function (): void {
6970
$responses = [
70-
\mockResponse("list-stories", 200, ["total"=>2, "per-page" => 25 ]),
71-
\mockResponse("list-stories", 200, ["total"=>2, "per-page" => 25 ]),
72-
\mockResponse("list-stories", 200, ["total"=>2, "per-page" => 25 ]),
73-
\mockResponse("list-stories", 200, ["total"=>2, "per-page" => 25 ]),
74-
\mockResponse("list-stories", 200, ["total"=>2, "per-page" => 25 ]),
75-
\mockResponse("list-stories", 200, ["total"=>2, "per-page" => 25 ]),
71+
\mockResponse("list-stories", 200, ["total"=>10, "per-page" => 2, "page" => 1 ]),
72+
\mockResponse("list-stories", 429, ["total"=>10, "per-page" => 2, "page" => 2 ]),
73+
\mockResponse("list-stories", 200, ["total"=>10, "per-page" => 2, "page" => 2 ]),
74+
\mockResponse("list-stories", 200, ["total"=>10, "per-page" => 2, "page" => 3 ]),
75+
\mockResponse("list-stories", 200, ["total"=>10, "per-page" => 2, "page" => 4 ]),
76+
\mockResponse("list-stories", 200, ["total"=>10, "per-page" => 2, "page" => 5 ]),
7677
//\mockResponse("empty-asset", 404),
7778
];
7879

7980
$client = new MockHttpClient($responses);
8081
$mapiClient = ManagementApiClient::initTest($client);
81-
$storyBulkApi = $mapiClient->storyBulkApi("222");
82+
$storyBulkApi = new TestStoryBulkApi($mapiClient, "222");
8283

83-
$storyblokResponse = $storyBulkApi->all(
84+
foreach ($storyBulkApi->all(
8485
params: new StoriesParams(
8586
withTag: "aaa",
8687
search: "something"
87-
)
88-
);
89-
expect($storyblokResponse)->toBeInstanceOf(Generator::class);
90-
foreach ($storyblokResponse as $story) {
88+
),
89+
itemsPerPage: 2,
90+
) as $story) {
9191
expect($story->name())->toBe("My third post");
9292
}
9393
});
9494

95+
test('Testing list of stories max retry, Params', function (): void {
96+
$responses = [
97+
\mockResponse("list-stories", 429, ["total"=>10, "per-page" => 2, "page" => 1 ]),
98+
\mockResponse("list-stories", 429, ["total"=>10, "per-page" => 2, "page" => 1 ]),
99+
\mockResponse("list-stories", 429, ["total"=>10, "per-page" => 2, "page" => 1 ]),
100+
\mockResponse("list-stories", 429, ["total"=>10, "per-page" => 2, "page" => 1 ]),
101+
\mockResponse("list-stories", 429, ["total"=>10, "per-page" => 2, "page" => 1 ]),
102+
\mockResponse("list-stories", 200, ["total"=>10, "per-page" => 2, "page" => 1 ]),
103+
//\mockResponse("empty-asset", 404),
104+
];
105+
106+
$client = new MockHttpClient($responses);
107+
$mapiClient = ManagementApiClient::initTest($client);
108+
$storyBulkApi = new TestStoryBulkApi($mapiClient, "222");
109+
110+
$i = 0;
111+
foreach ($storyBulkApi->all(
112+
params: new StoriesParams(
113+
withTag: "aaa",
114+
search: "something"
115+
),
116+
itemsPerPage: 2
117+
) as $story) {
118+
++$i;
119+
expect($story->name())->toBe("My third post");
120+
}
121+
122+
expect($i)->toBe(0);
123+
124+
});
125+
95126
test('createBulk handles rate limiting and creates multiple stories', function (): void {
96127
$mockLogger = new class extends NullLogger {
97128
public array $logs = [];

tests/Feature/StoryTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use Storyblok\ManagementApi\Endpoints\StoryApi;
56
use Storyblok\ManagementApi\ManagementApiClient;
67
use Storyblok\ManagementApi\Data\StoryData;
78
use Storyblok\ManagementApi\QueryParameters\Filters\Filter;
@@ -178,3 +179,103 @@
178179
expect($string)->toMatch('/.*filter_query\[subheadline\]\[like\]=somethingelse.*$/');
179180

180181
});
182+
183+
test('Testing edit Story, StoryData', function (): void {
184+
$responses = [
185+
\mockResponse("one-story", 200),
186+
\mockResponse("one-story", 200),
187+
\mockResponse("empty-story", 404),
188+
];
189+
190+
$client = new MockHttpClient($responses);
191+
$mapiClient = ManagementApiClient::initTest($client);
192+
$storyApi = new StoryApi($mapiClient, "222");
193+
194+
$storyblokResponse = $storyApi->get("111");
195+
/** @var \Storyblok\ManagementApi\Data\StoryData $storyblokData */
196+
$storyblokData = $storyblokResponse->data();
197+
expect($storyblokData->get("name"))
198+
->toBe("My third post")
199+
->and($storyblokData->name())->toBe("My third post")
200+
->and($storyblokData->createdAt())->toBe("2024-02-08")
201+
->and($storyblokResponse->getResponseStatusCode())->toBe(200);
202+
$storyblokData->setName("AAA");
203+
$storyblokResponse = $storyApi->update("111", $storyblokData);
204+
$storyblokData = $storyblokResponse->data();
205+
expect($storyblokData->id())->toBe("440448565");
206+
207+
});
208+
209+
test('Testing publishing Story, StoryData', function (): void {
210+
$responses = [
211+
\mockResponse("one-story", 200),
212+
\mockResponse("one-story", 200),
213+
\mockResponse("empty-story", 404),
214+
];
215+
216+
$client = new MockHttpClient($responses);
217+
$mapiClient = ManagementApiClient::initTest($client);
218+
$storyApi = new StoryApi($mapiClient, "222");
219+
220+
$storyblokResponse = $storyApi->publish("111", "1112", "en");
221+
/** @var \Storyblok\ManagementApi\Data\StoryData $storyblokData */
222+
$storyblokData = $storyblokResponse->data();
223+
expect($storyblokData->get("name"))
224+
->toBe("My third post")
225+
->and($storyblokData->name())->toBe("My third post")
226+
->and($storyblokData->createdAt())->toBe("2024-02-08")
227+
->and($storyblokResponse->getResponseStatusCode())->toBe(200);
228+
229+
});
230+
231+
test('Testing unpublishing Story, StoryData', function (): void {
232+
$responses = [
233+
\mockResponse("one-story", 200),
234+
\mockResponse("one-story", 200),
235+
\mockResponse("empty-story", 404),
236+
];
237+
238+
$client = new MockHttpClient($responses);
239+
$mapiClient = ManagementApiClient::initTest($client);
240+
$storyApi = new StoryApi($mapiClient, "222");
241+
242+
$storyblokResponse = $storyApi->unpublish("111", "en");
243+
/** @var \Storyblok\ManagementApi\Data\StoryData $storyblokData */
244+
$storyblokData = $storyblokResponse->data();
245+
expect($storyblokData->get("name"))
246+
->toBe("My third post")
247+
->and($storyblokData->name())->toBe("My third post")
248+
->and($storyblokData->createdAt())->toBe("2024-02-08")
249+
->and($storyblokResponse->getResponseStatusCode())->toBe(200);
250+
251+
});
252+
253+
test('Testing validation input, StoryData', function (): void {
254+
$responses = [
255+
\mockResponse("list-stories", 200),
256+
\mockResponse("list-stories", 200),
257+
\mockResponse("empty-story", 404),
258+
];
259+
260+
$client = new MockHttpClient($responses);
261+
$mapiClient = ManagementApiClient::initTest($client);
262+
$storyApi = new StoryApi($mapiClient, "222");
263+
264+
$storyblokResponse = $storyApi->page(page: new PaginationParams(-1, -20));
265+
266+
})->throws(\InvalidArgumentException::class);
267+
268+
test('Testing validation input 2, StoryData', function (): void {
269+
$responses = [
270+
\mockResponse("list-stories", 200),
271+
\mockResponse("list-stories", 200),
272+
\mockResponse("empty-story", 404),
273+
];
274+
275+
$client = new MockHttpClient($responses);
276+
$mapiClient = ManagementApiClient::initTest($client);
277+
$storyApi = new StoryApi($mapiClient, "222");
278+
279+
$storyblokResponse = $storyApi->page(page: new PaginationParams(1, -20));
280+
281+
})->throws(\InvalidArgumentException::class);

tests/Feature/TagTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
$string = $storyblokResponse->getLastCalledUrl();
2424
expect($string)->toMatch('/.*tags.*$/');
2525

26-
2726
});
2827

2928
test('Testing one tag', function (): void {
@@ -46,7 +45,6 @@
4645
expect($data->id())->toBe("56932");
4746
expect($data->name())->toBe("some");
4847

49-
5048
});
5149

5250
test('Testing deleting tag', function (): void {
@@ -81,8 +79,6 @@
8179
$mapiClient = ManagementApiClient::initTest($client);
8280
$tagApi = new TagApi($mapiClient, "222");
8381

84-
85-
8682
$storyblokResponse = $tagApi->create("name");
8783
$string = $storyblokResponse->getLastCalledUrl();
8884
expect($string)->toMatch('/.*tags.*$/');
@@ -101,7 +97,6 @@
10197
$mapiClient = ManagementApiClient::initTest($client);
10298
$tagApi = new TagApi($mapiClient, "222");
10399

104-
105100
$storyblokResponse = $tagApi->update("56932", "some");
106101

107102
$string = $storyblokResponse->getLastCalledUrl();

0 commit comments

Comments
 (0)