Skip to content

Commit 6a128b2

Browse files
committed
API-543: Create a new media file and associate it to a product model
Signed-off-by: Klein Florian <[email protected]>
1 parent 8c68def commit 6a128b2

File tree

3 files changed

+143
-3
lines changed

3 files changed

+143
-3
lines changed

spec/Api/ProductMediaFileApiSpec.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,45 @@ function it_creates_a_media_file_from_a_resource($resourceClient, $fileSystem, R
176176
->shouldReturn('1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png');
177177
}
178178

179+
function it_creates_a_media_file_for_a_product_model($resourceClient, $fileSystem, ResponseInterface $response)
180+
{
181+
$fileResource = fopen('php://memory', 'r');
182+
$fileSystem->getResourceFromPath(Argument::any())->shouldNotBeCalled();
183+
184+
$productModel = [
185+
'code' => 'foo',
186+
'attribute' => 'picture',
187+
'scope' => 'e-commerce',
188+
'locale' => 'en_US',
189+
'type' => 'product_model',
190+
];
191+
192+
$requestProductModel = $productModel;
193+
unset($requestProductModel['type']);
194+
$requestParts = [
195+
[
196+
'name' => 'product_model',
197+
'contents' => json_encode($requestProductModel),
198+
],
199+
[
200+
'name' => 'file',
201+
'contents' => $fileResource,
202+
]
203+
];
204+
205+
$response->getHeaders()->willReturn(['Location' => [
206+
'http://localhost/api/rest/v1/media-files/1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png'
207+
]]);
208+
209+
$resourceClient
210+
->createMultipartResource(ProductMediaFileApi::MEDIA_FILES_URI, [], $requestParts)
211+
->shouldBeCalled()
212+
->willReturn($response);
213+
214+
$this->create($fileResource, $productModel)
215+
->shouldReturn('1/e/e/d/1eed10f108bde68b279d6f903f17b4b053e9d89d_akeneo.png');
216+
}
217+
179218
function it_throws_an_exception_if_the_response_does_not_contain_the_uri_of_the_created_media_file($resourceClient, ResponseInterface $response)
180219
{
181220
$fileResource = fopen('php://stdin', 'r');

src/Api/ProductMediaFileApi.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,18 @@ public function all($pageSize = 10, array $queryParameters = [])
8484
/**
8585
* {@inheritdoc}
8686
*/
87-
public function create($mediaFile, array $productData)
87+
public function create($mediaFile, array $data)
8888
{
8989
if (is_string($mediaFile)) {
9090
$mediaFile = $this->fileSystem->getResourceFromPath($mediaFile);
9191
}
9292

93+
$name = isset($data['type']) ? $data['type'] : 'product';
94+
unset($data['type']);
9395
$requestParts = [
9496
[
95-
'name' => 'product',
96-
'contents' => json_encode($productData),
97+
'name' => $name,
98+
'contents' => json_encode($data),
9799
],
98100
[
99101
'name' => 'file',
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\tests\Common\Api\ProductMediaFile;
4+
5+
use Akeneo\Pim\ApiClient\tests\Common\Api\ApiTestCase;
6+
use Akeneo\Pim\ApiClient\tests\MediaSanitizer;
7+
8+
class CreateMediaFileApiIntegration extends ApiTestCase
9+
{
10+
public function testCreateSuccessful()
11+
{
12+
$client = $this->createClient();
13+
$api = $client->getProductMediaFileApi();
14+
$baseUri = $this->getConfiguration()['pim']['base_uri'];
15+
$mediaFile = realpath(__DIR__ . '/../../../fixtures/akeneo.png');
16+
17+
$response = $client->getAttributeApi()->create('product_model_media', [
18+
'type' => 'pim_catalog_image',
19+
'group' => 'media',
20+
'unique' => false,
21+
'useable_as_grid_filter' => false,
22+
'max_characters' => null,
23+
'validation_rule' => null,
24+
'validation_regexp' => null,
25+
'localizable' => true,
26+
'scopable' => false,
27+
'labels' => [
28+
'en_US' => 'product model media',
29+
],
30+
]);
31+
32+
$response = $client->getFamilyApi()->upsert('boots', [
33+
'attributes' => [
34+
'product_model_media',
35+
'color',
36+
'description',
37+
'manufacturer',
38+
'name',
39+
'price',
40+
'side_view',
41+
'size',
42+
'sku',
43+
'heel_color',
44+
],
45+
]);
46+
47+
$initialCount = count($api->listPerPage(10)->getItems());
48+
$response = $api->create($mediaFile, [
49+
'code' => 'rain_boots',
50+
'attribute' => 'product_model_media',
51+
'scope' => null,
52+
'locale' => 'en_US',
53+
'type' => 'product_model',
54+
]);
55+
56+
$this->assertSame(MediaSanitizer::MEDIA_ATTRIBUTE_DATA_COMPARISON, MediaSanitizer::sanitize($response));
57+
58+
$mediaFiles = $api->listPerPage(10)->getItems();
59+
60+
$this->assertCount($initialCount + 1, $mediaFiles);
61+
62+
$expectedMediaFile = [
63+
'_links' => [
64+
'self' => [
65+
'href' => $baseUri . '/api/rest/v1/media-files/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png',
66+
],
67+
'download' => [
68+
'href' => $baseUri . '/api/rest/v1/media-files/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png/download',
69+
],
70+
],
71+
'code' => 'f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png',
72+
'original_filename' => 'akeneo.png',
73+
'mime_type' => 'image/png',
74+
'size' => 8073,
75+
'extension' => 'png',
76+
];
77+
78+
$expectedMediaFile = $this->sanitizeMediaFile($expectedMediaFile);
79+
$mediaFiles[4] = $this->sanitizeMediaFile($mediaFiles[4]);
80+
81+
$this->assertSameContent($expectedMediaFile, $mediaFiles[4]);
82+
}
83+
84+
/**
85+
* Sanitize the code and links of a media file, because the code is generated randomly.
86+
*
87+
* @param array $mediaFile
88+
*
89+
* @return array
90+
*/
91+
protected function sanitizeMediaFile(array $mediaFile)
92+
{
93+
$mediaFile['code'] = MediaSanitizer::sanitize($mediaFile['code']);
94+
$mediaFile['_links']['self']['href'] = MediaSanitizer::sanitize($mediaFile['_links']['self']['href']);
95+
$mediaFile['_links']['download']['href'] = MediaSanitizer::sanitize($mediaFile['_links']['download']['href']);
96+
97+
return $mediaFile;
98+
}
99+
}

0 commit comments

Comments
 (0)