Skip to content

Commit fccfe52

Browse files
committed
Full test coverage for AssetApi
1 parent 9d6de86 commit fccfe52

File tree

5 files changed

+162
-6
lines changed

5 files changed

+162
-6
lines changed

src/Endpoints/AssetApi.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ public function get(string|int $assetId): StoryblokResponseInterface
5858
);
5959
}
6060

61-
public function upload(string $filename, string|int|null $parent_id = null): StoryblokResponseInterface
62-
{
63-
// =========== CREATE A SIGNED REQUEST
61+
/**
62+
* @return array<mixed>
63+
*/
64+
public function buildPayload(
65+
string $filename,
66+
string|int|null $parent_id = null,
67+
): array {
6468
$payload = [
6569
'filename' => $filename,
6670
//'size' => $width . 'x' . $height,
@@ -74,6 +78,14 @@ public function upload(string $filename, string|int|null $parent_id = null): Sto
7478
$payload['size'] = $width . 'x' . $height;
7579
}
7680

81+
return $payload;
82+
}
83+
84+
public function upload(string $filename, string|int|null $parent_id = null): StoryblokResponseInterface
85+
{
86+
// =========== CREATE A SIGNED REQUEST
87+
$payload = $this->buildPayload($filename, $parent_id);
88+
7789
$signedResponse = $this->makeRequest(
7890
"POST",
7991
'/v1/spaces/' . $this->spaceId . '/assets/',
@@ -99,14 +111,27 @@ public function upload(string $filename, string|int|null $parent_id = null): Sto
99111
$postFields['file'] = fopen($filename, 'r');
100112
$postUrl = $signedResponseData->getString('post_url');
101113

102-
$responseUpload = HttpClient::create()->request(
114+
/*
115+
$responseUpload = $this->makeHttpRequest(
116+
"POST",
117+
$postUrl,
118+
[
119+
"body" => $postFields,
120+
],
121+
122+
);
123+
*/
124+
125+
$responseUpload = $this->managementClient->httpAssetClient()->request(
103126
"POST",
104127
$postUrl,
105128
[
106129
"body" => $postFields,
107130
],
108131
);
132+
109133
if (!($responseUpload->getStatusCode() >= 200 && $responseUpload->getStatusCode() < 300)) {
134+
//var_dump($responseUpload->getInfo());
110135
throw new \Exception("Upload Asset, Upload call failed (Step 2) , " . $responseUpload->getStatusCode());
111136
}
112137

@@ -129,6 +154,7 @@ public function delete(string $assetId): StoryblokResponseInterface
129154
return $this->makeRequest(
130155
"DELETE",
131156
'/v1/spaces/' . $this->spaceId . '/assets/' . $assetId,
157+
dataClass: AssetData::class,
132158
);
133159
}
134160

src/ManagementApiClient.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Storyblok\ManagementApi\Endpoints\WorkflowApi;
1616
use Storyblok\ManagementApi\Endpoints\WorkflowStageApi;
1717
use Symfony\Component\HttpClient\HttpClient;
18+
use Symfony\Component\HttpClient\MockHttpClient;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
1920
use Psr\Log\LoggerInterface;
2021
use Psr\Log\NullLogger;
@@ -27,6 +28,8 @@ class ManagementApiClient
2728
{
2829
private HttpClientInterface $httpClient;
2930

31+
private HttpClientInterface $httpAssetClient;
32+
3033
/**
3134
* MapiClient constructor.
3235
*/
@@ -46,6 +49,7 @@ public function __construct(
4649
'Authorization' => $personalAccessToken,
4750
],
4851
]);
52+
$this->httpAssetClient = HttpClient::create();
4953
}
5054

5155
/**
@@ -61,12 +65,19 @@ public static function init(
6165

6266
public static function initTest(
6367
HttpClientInterface $httpClient,
68+
?HttpClientInterface $httpAssetClient = null,
6469
): self {
6570

6671
$client = new self("");
6772
//$baseUriMapi = $baseUri ?? StoryblokUtils::baseUriFromRegionForMapi($region);
6873

6974
$client->httpClient = $httpClient;
75+
if ($httpAssetClient instanceof \Symfony\Contracts\HttpClient\HttpClientInterface) {
76+
$client->httpAssetClient = $httpAssetClient;
77+
} else {
78+
$client->httpAssetClient = new MockHttpClient();
79+
}
80+
7081

7182

7283
return $client;
@@ -78,6 +89,11 @@ public function httpClient(): HttpClientInterface
7889
return $this->httpClient;
7990
}
8091

92+
public function httpAssetClient(): HttpClientInterface
93+
{
94+
return $this->httpAssetClient;
95+
}
96+
8197
public function spaceApi(): SpaceApi
8298
{
8399
return new SpaceApi($this);

tests/Feature/AssetTest.php

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
declare(strict_types=1);
44

5+
use Storyblok\ManagementApi\Endpoints\AssetApi;
56
use Storyblok\ManagementApi\ManagementApiClient;
67

78
use Storyblok\ManagementApi\QueryParameters\AssetsParams;
89
use Storyblok\ManagementApi\QueryParameters\PaginationParams;
910
use Symfony\Component\HttpClient\MockHttpClient;
11+
use Symfony\Component\HttpClient\Response\JsonMockResponse;
1012

1113

1214
test('Testing One asset, AssetData', function (): void {
@@ -17,10 +19,10 @@
1719

1820
$client = new MockHttpClient($responses);
1921
$mapiClient = ManagementApiClient::initTest($client);
20-
$assetApi = $mapiClient->assetApi("222");
22+
$assetApi = new AssetApi($mapiClient, "222");
2123

2224
$storyblokResponse = $assetApi->get("111");
23-
/** @var \Storyblok\ManagementApi\Data\AssetData $storyblokData */
25+
2426
$storyblokData = $storyblokResponse->data();
2527
expect($storyblokData->get("id"))
2628
->toBe(111)
@@ -107,4 +109,99 @@
107109

108110
});
109111

112+
test('testing asset payload', function (): void {
113+
$responses = [
114+
\mockResponse("list-assets", 200, ["total"=>2, "per-page" => 25 ]),
115+
\mockResponse("list-assets", 200, ["total"=>200, "per-page" => 25 ]),
116+
\mockResponse("list-assets", 200, ["total"=>200, "per-page" => 25 ]),
117+
\mockResponse("empty-asset", 404),
118+
];
119+
120+
$client = new MockHttpClient($responses);
121+
$mapiClient = ManagementApiClient::initTest($client);
122+
$assetApi = $mapiClient->assetApi("222");
123+
$filename = "./tests/Feature/Data/image-test.png";
124+
$parentId = "111";
125+
$payload = $assetApi->buildPayload($filename, $parentId);
126+
expect($payload)->toBeArray();
127+
expect($payload)->toHaveKey("filename");
128+
});
129+
130+
131+
test('delete one asset', function (): void {
132+
$responses = [
133+
\mockResponse("one-asset", 200, ["total"=>2, "per-page" => 25 ]),
134+
\mockResponse("empty-asset", 404),
135+
];
136+
137+
$client = new MockHttpClient($responses);
138+
$mapiClient = ManagementApiClient::initTest($client);
139+
$assetApi = new AssetApi($mapiClient, "222");
140+
$assetId = "12345";
141+
$response = $assetApi->delete($assetId);
142+
$data = $response->data();
143+
expect($data->id())->toBe("111");
144+
145+
});
146+
147+
test('upload one asset', function (): void {
148+
$responses = [
149+
\mockResponse('upload-asset-signed-response', 200),
150+
151+
\mockResponse('one-asset', 200),
152+
];
153+
$responsesAsset = [
154+
\mockResponse('one-asset', 200),
155+
];
156+
157+
158+
$httpClient = new MockHttpClient($responses);
159+
$httpAssetClient = new MockHttpClient($responsesAsset);
160+
$mapiClient = ManagementApiClient::initTest($httpClient, $httpAssetClient );
161+
$assetApi = new AssetApi($mapiClient, "222");
162+
163+
$response = $assetApi->upload("./tests/Feature/Data/image-test.png");
164+
$data = $response->data();
165+
expect($data->id())->toBe("111");
166+
167+
});
168+
169+
test('upload one asset - failing', function (): void {
170+
$responses = [
171+
\mockResponse('upload-asset-signed-response', 401),
172+
];
173+
$responsesAsset = [
174+
\mockResponse('one-asset', 200),
175+
];
176+
177+
178+
$httpClient = new MockHttpClient($responses);
179+
$httpAssetClient = new MockHttpClient($responsesAsset);
180+
$mapiClient = ManagementApiClient::initTest($httpClient, $httpAssetClient );
181+
$assetApi = new AssetApi($mapiClient, "222");
182+
183+
$response = $assetApi->upload("./tests/Feature/Data/image-test.png");
184+
$data = $response->data();
185+
expect($data->id())->toBe("111");
186+
187+
})->throws(Exception::class);
188+
189+
test('upload one asset - failing on second step', function (): void {
190+
$responses = [
191+
\mockResponse('upload-asset-signed-response', 200),
192+
];
193+
$responsesAsset = [
194+
\mockResponse('one-asset', 400),
195+
];
196+
197+
198+
$httpClient = new MockHttpClient($responses);
199+
$httpAssetClient = new MockHttpClient($responsesAsset);
200+
$mapiClient = ManagementApiClient::initTest($httpClient, $httpAssetClient );
201+
$assetApi = new AssetApi($mapiClient, "222");
202+
203+
$response = $assetApi->upload("./tests/Feature/Data/image-test.png");
204+
$data = $response->data();
205+
expect($data->id())->toBe("111");
110206

207+
})->throws(Exception::class);

tests/Feature/Data/image-test.png

492 Bytes
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"pretty_url": "//a-example.storyblok.com/f/606/e5990a3595/your_file.jpg",
3+
"public_url": "https://s3.amazonaws.com/a-example.storyblok.com/f/606/e5990a3595/your_file.jpg",
4+
"fields": {
5+
"key": "f/606/e5990a3595/your_file.jpg",
6+
"acl": "public-read",
7+
"Expires": "Sun, 10 Nov 2019 15:33:00 GMT",
8+
"Cache-Control": "public; max-age=31536000",
9+
"Content-Type": "image/jpeg",
10+
"policy": "eyJleHBpcmF0aW9uIjoiMjAxOC0xMS0xMFQxNTo...ei1hbGdvcml0aG0iOiJBV1M0LUhNQUM...LWFtei1kYXRlIjoiMjAxODExMTBUMTUzMzAwWiJ9XX0=",
11+
"x-amz-credential": "AKIAIU627EN23A/20181110/s3/aws4_request",
12+
"x-amz-algorithm": "AWS4-HMAC-SHA256",
13+
"x-amz-date": "20181110T153300Z",
14+
"x-amz-signature": "aaedd72b54636662b137b7648b54bdb47ee3b1dd28173313647930e625c8"
15+
},
16+
"post_url": "https://s3.amazonaws.com/a-example.storyblok.com"
17+
}

0 commit comments

Comments
 (0)