Skip to content

Commit b705cbb

Browse files
committed
Fix specifications & add integration test
1 parent 721f194 commit b705cbb

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

spec/Api/ProductMediaFileApiSpec.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ function it_creates_a_media_file_from_a_path($resourceClient, $fileSystem, Respo
129129
]
130130
];
131131

132-
$response->getHeaders()->willReturn(['Location' => [
132+
$response->hasHeader('location')->willReturn(true);
133+
134+
$response->getHeader('location')->willReturn([
133135
'http://localhost/api/rest/v1/media-files/1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png'
134-
]]);
136+
]);
135137

136138
$resourceClient
137139
->createMultipartResource(ProductMediaFileApi::MEDIA_FILES_URI, [], $requestParts)
@@ -164,9 +166,10 @@ function it_creates_a_media_file_from_a_resource($resourceClient, $fileSystem, R
164166
]
165167
];
166168

167-
$response->getHeaders()->willReturn(['Location' => [
169+
$response->hasHeader('location')->willReturn(true);
170+
$response->getHeader('location')->willReturn([
168171
'http://localhost/api/rest/v1/media-files/1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png'
169-
]]);
172+
]);
170173

171174
$resourceClient
172175
->createMultipartResource(ProductMediaFileApi::MEDIA_FILES_URI, [], $requestParts)
@@ -202,9 +205,10 @@ function it_creates_a_media_file_for_a_product_model($resourceClient, $fileSyste
202205
]
203206
];
204207

205-
$response->getHeaders()->willReturn(['Location' => [
208+
$response->hasHeader('location')->willReturn(true);
209+
$response->getHeader('location')->willReturn([
206210
'http://localhost/api/rest/v1/media-files/1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png'
207-
]]);
211+
]);
208212

209213
$resourceClient
210214
->createMultipartResource(ProductMediaFileApi::MEDIA_FILES_URI, [], $requestParts)
@@ -236,7 +240,7 @@ function it_throws_an_exception_if_the_response_does_not_contain_the_uri_of_the_
236240
]
237241
];
238242

239-
$response->getHeaders()->willReturn(['Location' => '']);
243+
$response->hasHeader('location')->willReturn(false);
240244

241245
$resourceClient
242246
->createMultipartResource(ProductMediaFileApi::MEDIA_FILES_URI, [], $requestParts)
@@ -268,7 +272,8 @@ function it_throws_an_exception_if_the_uri_of_the_created_media_file_is_invalid(
268272
]
269273
];
270274

271-
$response->getHeaders()->willReturn(['Location' => ['http://localhost/api/rest/v1/products/foo']]);
275+
$response->hasHeader('location')->willReturn(true);
276+
$response->getHeader('location')->willReturn(['http://localhost/api/rest/v1/products/foo']);
272277

273278
$resourceClient
274279
->createMultipartResource(ProductMediaFileApi::MEDIA_FILES_URI, [], $requestParts)

src/Api/ProductMediaFileApi.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,10 @@ public function download(string $code): ResponseInterface
130130
*/
131131
protected function extractCodeFromCreationResponse(ResponseInterface $response)
132132
{
133-
if ($response->hasHeader('Location')) {
134-
$locationHeader = $response->getHeader('Location')[0];
135-
}
136-
137-
if ($response->hasHeader('location')) {
138-
$locationHeader = $response->getHeader('location')[0];
139-
}
140-
141-
if (!isset($locationHeader)) {
133+
if (!$response->hasHeader('location')) {
142134
throw new RuntimeException('The response does not contain the URI of the created media-file.');
143135
}
136+
$locationHeader = $response->getHeader('location')[0];
144137

145138
$matches = [];
146139
if (1 !== preg_match(static::MEDIA_FILE_URI_CODE_REGEX, $locationHeader, $matches)) {

tests/Api/CreateProductMediaFileTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,29 @@ public function test_create_media_file()
4141

4242
Assert::assertSame('f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png', $response);
4343
}
44+
45+
public function test_get_created_media_file_location_regardless_of_the_header_case()
46+
{
47+
$mediaFileURI = $this->server->getServerRoot(
48+
) . '/' . ProductMediaFileApi::MEDIA_FILES_URI . '/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png';
49+
$this->server->setResponseOfPath(
50+
'/' . ProductMediaFileApi::MEDIA_FILES_URI,
51+
new ResponseStack(
52+
new Response('', ['LOcaTiON' => $mediaFileURI], 201)
53+
)
54+
);
55+
56+
$api = $this->createClient()->getProductMediaFileApi();
57+
$mediaFile = realpath(__DIR__ . '/../fixtures/akeneo.png');
58+
59+
$productInfos = [
60+
'identifier' => 'medium_boot',
61+
'attribute' => 'side_view',
62+
'scope' => null,
63+
'locale' => null,
64+
];
65+
66+
$response = $api->create($mediaFile, $productInfos);
67+
Assert::assertSame('f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png', $response);
68+
}
4469
}

0 commit comments

Comments
 (0)