Skip to content

Commit 7d3aa97

Browse files
committed
Improving tests for creating story
1 parent 0f2312e commit 7d3aa97

File tree

3 files changed

+41
-40
lines changed

3 files changed

+41
-40
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ Now we want to upload a new image, and then create a new simple story that inclu
569569
use Storyblok\ManagementApi\Data\Story;
570570
use Storyblok\ManagementApi\ManagementApiClient;
571571
use Storyblok\ManagementApi\Data\StoryComponent;
572+
use Storyblok\ManagementApi\Endpoints\StoryApi;
573+
use Storyblok\ManagementApi\Endpoints\AssetApi;
572574

573575
$client = new ManagementApiClient($storyblokPersonalAccessToken);
574576

@@ -577,16 +579,16 @@ $storyApi = new StoryApi($client, $spaceId);
577579
$assetApi = new AssetApi($client, $spaceId);
578580

579581
echo "UPLOADING ASSET..." . PHP_EOL;
580-
$response = $assetApi->upload("image.png");
581-
$assetCreated = $response->data();
582+
$assetCreated = $assetApi->upload("image.png")->data();
582583
echo "Asset created, ID: " . $assetCreated->id() . PHP_EOL;
583584

584-
echo "PREPARING STORY DATA..." . PHP_EOL;
585+
echo "PREPARING STORY CONTENT DATA..." . PHP_EOL;
585586
$content = new StoryComponent("article-page");
586587
$content->set("title", "New Article");
587588
$content->set("body", "This is the content");
588589
$content->setAsset("image", $assetCreated);
589590

591+
echo "INITIALIZING STORY OBJECT..." . PHP_EOL;
590592
$story = new Story(
591593
"An Article",
592594
"an-article-" . random_int(10000, 99999),

src/Endpoints/StoryApi.php

+10-37
Original file line numberDiff line numberDiff line change
@@ -110,43 +110,16 @@ public function create(Story $storyData): StoryResponse
110110
$storyData->setContent(new StoryComponent($storyData->defaultContentType()));
111111
}
112112

113-
try {
114-
$httpResponse = $this->makeHttpRequest(
115-
"POST",
116-
$this->buildStoriesEndpoint(),
117-
[
118-
"body" => json_encode(["story" => $storyData->toArray()]),
119-
]
120-
);
121-
122-
return new StoryResponse($httpResponse);
123-
124-
} catch (\Exception $exception) {
125-
if ($exception instanceof StoryblokApiException) {
126-
127-
$this->logger->info('xxxFailed to create story', [
128-
'status_code' => $exception->getCode(),
129-
'error_message' => $exception->getMessage(),
130-
'story_name' => $storyData->name(),
131-
]);
132-
throw $exception;
133-
}
134-
135-
if ($exception instanceof ClientException) {
136-
$this->logger->info($exception->getResponse()->getContent(false), [
137-
'status_code' => $exception->getCode(),
138-
'error_message' => $exception->getMessage(),
139-
'story_name' => $storyData->name(),
140-
]);
141-
throw $exception;
142-
}
143-
144-
$this->logger->error($exception->getMessage(), [
145-
'error' => $exception->getMessage(),
146-
'story_name' => $storyData->name(),
147-
]);
148-
throw $exception;
149-
}
113+
$httpResponse = $this->makeHttpRequest(
114+
"POST",
115+
$this->buildStoriesEndpoint(),
116+
[
117+
"body" => json_encode(["story" => $storyData->toArray()]),
118+
]
119+
);
120+
121+
return new StoryResponse($httpResponse);
122+
150123
}
151124

152125
/**

tests/Feature/StoryTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use Storyblok\ManagementApi\Data\StoryComponent;
56
use Storyblok\ManagementApi\Endpoints\StoryApi;
67
use Storyblok\ManagementApi\ManagementApiClient;
78
use Storyblok\ManagementApi\Data\Story;
@@ -41,6 +42,31 @@
4142
//expect($storyblokResponse->getResponseStatusCode())->toBe(404) ;
4243
//expect($storyblokResponse->asJson())->toBe('["This record could not be found"]');
4344
});
45+
test('Testing Creating story with error', function (): void {
46+
$responses = [
47+
\mockResponse("empty-story", 401),
48+
];
49+
50+
$client = new MockHttpClient($responses);
51+
$mapiClient = ManagementApiClient::initTest($client);
52+
$storyApi = $mapiClient->storyApi("222");
53+
54+
$storyblokResponse = $storyApi->create(
55+
new Story(
56+
"aa",
57+
"aa",
58+
new StoryComponent("aa")
59+
)
60+
);
61+
expect($storyblokResponse->getResponseStatusCode())->toBe(401);
62+
expect(function () use ($storyblokResponse): void {
63+
$storyCreated = $storyblokResponse->data();
64+
})->toThrow(
65+
\Exception::class,
66+
'HTTP 401 returned for "https://example.com/v1/spaces/222/stories'
67+
);
68+
69+
});
4470

4571
test('Create story encodes data correctly as JSON', function (): void {
4672
$expectedStoryData = [

0 commit comments

Comments
 (0)