-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from akeneo/GRF-862
GRF-862 : Add api/rest/v1/category-media-files/%s/download route to the client
- Loading branch information
Showing
7 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace spec\Akeneo\Pim\ApiClient\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi; | ||
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
class CategoryMediaFileApiSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
ResourceClientInterface $resourceClient, | ||
) { | ||
$this->beConstructedWith($resourceClient); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(CategoryMediaFileApi::class); | ||
$this->shouldImplement(DownloadableResourceInterface::class); | ||
} | ||
|
||
function it_downloads_a_media_file($resourceClient, ResponseInterface $response, StreamInterface $streamBody) | ||
{ | ||
$resourceClient | ||
->getStreamedResource(CategoryMediaFileApi::MEDIA_FILE_DOWNLOAD_URI, ['42.jpg']) | ||
->willReturn($response); | ||
|
||
$this->download('42.jpg')->shouldReturn($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Akeneo\Pim\ApiClient\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\Operation\DownloadableResourceInterface; | ||
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* API implementation to manage enriched categories media files. | ||
* | ||
* @copyright 2023 Akeneo SAS (http://www.akeneo.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
class CategoryMediaFileApi implements DownloadableResourceInterface | ||
{ | ||
public const MEDIA_FILE_DOWNLOAD_URI = 'api/rest/v1/category-media-files/%s/download'; | ||
|
||
public function __construct( | ||
protected ResourceClientInterface $resourceClient, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function download(string $code): ResponseInterface | ||
{ | ||
return $this->resourceClient->getStreamedResource(static::MEDIA_FILE_DOWNLOAD_URI, [$code]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Akeneo\Pim\ApiClient\tests\Api; | ||
|
||
use Akeneo\Pim\ApiClient\Api\CategoryMediaFileApi; | ||
use donatj\MockWebServer\RequestInfo; | ||
use donatj\MockWebServer\Response; | ||
use donatj\MockWebServer\ResponseStack; | ||
use PHPUnit\Framework\Assert; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class DownloadCategoryMediaFileTest extends ApiTestCase | ||
{ | ||
public function test_download_media_file() | ||
{ | ||
$expectedMediaFilePath = realpath(__DIR__ . '/../fixtures/akeneo.png'); | ||
|
||
$this->server->setResponseOfPath( | ||
'/' . sprintf(CategoryMediaFileApi::MEDIA_FILE_DOWNLOAD_URI, '/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png'), | ||
new ResponseStack( | ||
new Response(file_get_contents($expectedMediaFilePath), [], 201) | ||
) | ||
); | ||
|
||
$api = $this->createClientByPassword()->getCategoryMediaFileApi(); | ||
$mediaFile = $api->download('/f/b/0/6/fb068ccc9e3c5609d73c28d852812ba5faeeab28_akeneo.png'); | ||
|
||
Assert::assertSame('GET', $this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_METHOD]); | ||
Assert::assertInstanceOf(ResponseInterface::class, $mediaFile); | ||
Assert::assertSame(file_get_contents($expectedMediaFilePath), $mediaFile->getBody()->getContents()); | ||
} | ||
} |