Skip to content

Commit 7c3f877

Browse files
authored
Merge branch 'master' into dependabot/composer/guzzlehttp/psr7-1.9.1
2 parents e4264d0 + 4e38b9a commit 7c3f877

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

spec/AkeneoPimClientSpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Akeneo\Pim\ApiClient\Api\MeasureFamilyApiInterface;
3030
use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApiInterface;
3131
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
32+
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
3233
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
3334
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
3435
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
@@ -50,6 +51,7 @@ function let(
5051
Authentication $authentication,
5152
ProductApiInterface $productApi,
5253
CategoryApiInterface $categoryApi,
54+
DownloadableResourceInterface $categoryMediaFileApi,
5355
AttributeApiInterface $attributeApi,
5456
AttributeOptionApiInterface $attributeOptionApi,
5557
AttributeGroupApiInterface $attributeGroupApi,
@@ -90,6 +92,7 @@ function let(
9092
$authentication,
9193
$productApi,
9294
$categoryApi,
95+
$categoryMediaFileApi,
9396
$attributeApi,
9497
$attributeOptionApi,
9598
$attributeGroupApi,
@@ -158,6 +161,11 @@ function it_gets_category_api($categoryApi)
158161
$this->getCategoryApi()->shouldReturn($categoryApi);
159162
}
160163

164+
function it_gets_category_media_file_api($categoryMediaFileApi)
165+
{
166+
$this->getCategoryMediaFileApi()->shouldReturn($categoryMediaFileApi);
167+
}
168+
161169
function it_gets_attribute_api($attributeApi)
162170
{
163171
$this->getAttributeApi()->shouldReturn($attributeApi);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace spec\Akeneo\Pim\ApiClient\Api;
4+
5+
use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi;
6+
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
7+
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
8+
use PhpSpec\ObjectBehavior;
9+
use Prophecy\Argument;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\StreamInterface;
12+
13+
class CategoryMediaFileApiSpec extends ObjectBehavior
14+
{
15+
function let(
16+
ResourceClientInterface $resourceClient,
17+
) {
18+
$this->beConstructedWith($resourceClient);
19+
}
20+
21+
function it_is_initializable()
22+
{
23+
$this->shouldHaveType(CategoryMediaFileApi::class);
24+
$this->shouldImplement(DownloadableResourceInterface::class);
25+
}
26+
27+
function it_downloads_a_media_file($resourceClient, ResponseInterface $response, StreamInterface $streamBody)
28+
{
29+
$resourceClient
30+
->getStreamedResource(CategoryMediaFileApi::MEDIA_FILE_DOWNLOAD_URI, ['42.jpg'])
31+
->willReturn($response);
32+
33+
$this->download('42.jpg')->shouldReturn($response);
34+
}
35+
}

src/AkeneoPimClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Akeneo\Pim\ApiClient\Api\AttributeGroupApiInterface;
2020
use Akeneo\Pim\ApiClient\Api\AttributeOptionApiInterface;
2121
use Akeneo\Pim\ApiClient\Api\CategoryApiInterface;
22+
use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi;
2223
use Akeneo\Pim\ApiClient\Api\ChannelApiInterface;
2324
use Akeneo\Pim\ApiClient\Api\CurrencyApiInterface;
2425
use Akeneo\Pim\ApiClient\Api\FamilyApiInterface;
@@ -27,6 +28,7 @@
2728
use Akeneo\Pim\ApiClient\Api\MeasureFamilyApiInterface;
2829
use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApiInterface;
2930
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
31+
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
3032
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
3133
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
3234
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
@@ -54,6 +56,7 @@ public function __construct(
5456
protected Authentication $authentication,
5557
protected ProductApiInterface $productApi,
5658
protected CategoryApiInterface $categoryApi,
59+
protected DownloadableResourceInterface $categoryMediaFileApi,
5760
protected AttributeApiInterface $attributeApi,
5861
protected AttributeOptionApiInterface $attributeOptionApi,
5962
protected AttributeGroupApiInterface $attributeGroupApi,
@@ -124,6 +127,14 @@ public function getCategoryApi(): CategoryApiInterface
124127
return $this->categoryApi;
125128
}
126129

130+
/**
131+
* {@inheritdoc}
132+
*/
133+
public function getCategoryMediaFileApi(): DownloadableResourceInterface
134+
{
135+
return $this->categoryMediaFileApi;
136+
}
137+
127138
/**
128139
* {@inheritdoc}
129140
*/

src/AkeneoPimClientBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Akeneo\Pim\ApiClient\Api\AttributeOptionApi;
2121
use Akeneo\Pim\ApiClient\Api\AuthenticationApi;
2222
use Akeneo\Pim\ApiClient\Api\CategoryApi;
23+
use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi;
2324
use Akeneo\Pim\ApiClient\Api\ChannelApi;
2425
use Akeneo\Pim\ApiClient\Api\CurrencyApi;
2526
use Akeneo\Pim\ApiClient\Api\FamilyApi;
@@ -213,6 +214,7 @@ protected function buildAuthenticatedClient(Authentication $authentication): Ake
213214
$authentication,
214215
new ProductApi($resourceClient, $pageFactory, $cursorFactory),
215216
new CategoryApi($resourceClientWithCache, $pageFactory, $cursorFactory),
217+
new CategoryMediaFileApi($resourceClientWithCache),
216218
new AttributeApi($resourceClientWithCache, $pageFactory, $cursorFactory),
217219
new AttributeOptionApi($resourceClientWithCache, $pageFactory, $cursorFactory),
218220
new AttributeGroupApi($resourceClientWithCache, $pageFactory, $cursorFactory),

src/AkeneoPimClientInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Akeneo\Pim\ApiClient\Api\MeasureFamilyApiInterface;
2828
use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApiInterface;
2929
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
30+
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
3031
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
3132
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
3233
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
@@ -57,6 +58,8 @@ public function getProductApi(): ProductApiInterface;
5758

5859
public function getCategoryApi(): CategoryApiInterface;
5960

61+
public function getCategoryMediaFileApi(): DownloadableResourceInterface;
62+
6063
public function getAttributeApi(): AttributeApiInterface;
6164

6265
public function getAttributeOptionApi(): AttributeOptionApiInterface;

src/Api/CategoryMediaFileApi.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\Api;
4+
5+
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface;
6+
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
/**
10+
* API implementation to manage enriched categories media files.
11+
*
12+
* @copyright 2023 Akeneo SAS (http://www.akeneo.com)
13+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14+
*/
15+
class CategoryMediaFileApi implements DownloadableResourceInterface
16+
{
17+
public const MEDIA_FILE_DOWNLOAD_URI = 'api/rest/v1/category-media-files/%s/download';
18+
19+
public function __construct(
20+
protected ResourceClientInterface $resourceClient,
21+
) {
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function download(string $code): ResponseInterface
28+
{
29+
return $this->resourceClient->getStreamedResource(static::MEDIA_FILE_DOWNLOAD_URI, [$code]);
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\tests\Api;
4+
5+
use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi;
6+
use donatj\MockWebServer\RequestInfo;
7+
use donatj\MockWebServer\Response;
8+
use donatj\MockWebServer\ResponseStack;
9+
use PHPUnit\Framework\Assert;
10+
use Psr\Http\Message\ResponseInterface;
11+
12+
class DownloadCategoryMediaFileTest extends ApiTestCase
13+
{
14+
public function test_download_media_file()
15+
{
16+
$expectedMediaFilePath = realpath(__DIR__ . '/../fixtures/akeneo.png');
17+
18+
$this->server->setResponseOfPath(
19+
'/' . sprintf(CategoryMediaFileApi::MEDIA_FILE_DOWNLOAD_URI, '/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png'),
20+
new ResponseStack(
21+
new Response(file_get_contents($expectedMediaFilePath), [], 201)
22+
)
23+
);
24+
25+
$api = $this->createClientByPassword()->getCategoryMediaFileApi();
26+
$mediaFile = $api->download('/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png');
27+
28+
Assert::assertSame('GET', $this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_METHOD]);
29+
Assert::assertInstanceOf(ResponseInterface::class, $mediaFile);
30+
Assert::assertSame(file_get_contents($expectedMediaFilePath), $mediaFile->getBody()->getContents());
31+
}
32+
}

0 commit comments

Comments
 (0)