Skip to content

Commit 8619fbd

Browse files
authored
Merge pull request #248 from akeneo/API-1835-bis
Add Product Draft UUID routes
2 parents fb4deb7 + 0f1f12e commit 8619fbd

14 files changed

+219
-0
lines changed

spec/AkeneoPimClientSpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
3232
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
3333
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
34+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
3435
use Akeneo\Pim\ApiClient\Api\ProductModelApiInterface;
3536
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApiInterface;
3637
use Akeneo\Pim\ApiClient\Api\ProductUuidApiInterface;
@@ -81,6 +82,7 @@ function let(
8182
AssetAttributeOptionApiInterface $assetAttributeOptionApi,
8283
AssetMediaFileApiInterface $assetMediaFileApi,
8384
ProductUuidApiInterface $productUuidApi,
85+
ProductDraftUuidApiInterface $productDraftUuidApi,
8486
AppCatalogApiInterface $appCatalogApi,
8587
AppCatalogProductApiInterface $appCatalogProductApi
8688
) {
@@ -120,6 +122,7 @@ function let(
120122
$assetAttributeOptionApi,
121123
$assetMediaFileApi,
122124
$productUuidApi,
125+
$productDraftUuidApi,
123126
$appCatalogApi,
124127
$appCatalogProductApi
125128
);
@@ -300,6 +303,11 @@ function it_gets_product_uuid_api(ProductUuidApiInterface $productUuidApi)
300303
$this->getProductUuidApi()->shouldReturn($productUuidApi);
301304
}
302305

306+
function it_gets_product_draft_uuid_api(ProductDraftUuidApiInterface $productDraftUuidApi)
307+
{
308+
$this->getProductDraftUuidApi()->shouldReturn($productDraftUuidApi);
309+
}
310+
303311
function it_gets_app_catalog_api(AppCatalogApiInterface $appCatalogApi)
304312
{
305313
$this->getAppCatalogApi()->shouldReturn($appCatalogApi);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace spec\Akeneo\Pim\ApiClient\Api;
4+
5+
use Akeneo\Pim\ApiClient\Api\Operation\GettableResourceInterface;
6+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApi;
7+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
8+
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
9+
use Akeneo\Pim\ApiClient\Pagination\PageFactoryInterface;
10+
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorFactoryInterface;
11+
use PhpSpec\ObjectBehavior;
12+
13+
class ProductDraftUuidApiSpec extends ObjectBehavior
14+
{
15+
function let(
16+
ResourceClientInterface $resourceClient,
17+
PageFactoryInterface $pageFactory,
18+
ResourceCursorFactoryInterface $cursorFactory
19+
) {
20+
$this->beConstructedWith($resourceClient, $pageFactory, $cursorFactory);
21+
}
22+
23+
function it_is_initializable()
24+
{
25+
$this->shouldHaveType(ProductDraftUuidApi::class);
26+
$this->shouldImplement(ProductDraftUuidApiInterface::class);
27+
$this->shouldImplement(GettableResourceInterface::class);
28+
}
29+
30+
function it_gets_a_product_draft($resourceClient)
31+
{
32+
$draft = [
33+
'uuid' => '944ca210-d8e0-4c57-9529-741e17e95c8d',
34+
'family' => 'bar',
35+
'parent' => null,
36+
'groups' => [],
37+
'categories' => [],
38+
'enabled' => true,
39+
'values' => [],
40+
'created' => 'this is a date formatted to ISO-8601',
41+
'updated' => 'this is a date formatted to ISO-8601',
42+
'associations' => [],
43+
'metadata' => [
44+
'workflow_status' => 'draft_in_progress',
45+
],
46+
];
47+
48+
$resourceClient->getResource(ProductDraftUuidApi::PRODUCT_DRAFT_UUID_URI, ['944ca210-d8e0-4c57-9529-741e17e95c8d'])->willReturn($draft);
49+
50+
$this->get('944ca210-d8e0-4c57-9529-741e17e95c8d')->shouldReturn($draft);
51+
}
52+
53+
function it_submits_a_product_draft_for_approval($resourceClient)
54+
{
55+
$resourceClient->createResource(ProductDraftUuidApi::PRODUCT_PROPOSAL_UUID_URI, ['944ca210-d8e0-4c57-9529-741e17e95c8d'])->willReturn(201);
56+
57+
$this->submitForApproval('944ca210-d8e0-4c57-9529-741e17e95c8d')->shouldReturn(201);
58+
}
59+
}

src/AkeneoPimClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
3030
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
3131
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
32+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
3233
use Akeneo\Pim\ApiClient\Api\ProductModelApiInterface;
3334
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApiInterface;
3435
use Akeneo\Pim\ApiClient\Api\ProductUuidApiInterface;
@@ -87,6 +88,7 @@ class AkeneoPimClient implements AkeneoPimClientInterface
8788
private AppCatalogProductApiInterface $appCatalogProductApi;
8889

8990
private ProductUuidApiInterface $productUuidApi;
91+
private ProductDraftUuidApiInterface $productDraftUuidApi;
9092

9193
public function __construct(
9294
Authentication $authentication,
@@ -124,6 +126,7 @@ public function __construct(
124126
AssetAttributeOptionApiInterface $assetAttributeOptionApi,
125127
AssetMediaFileApiInterface $assetMediaFileApi,
126128
ProductUuidApiInterface $productUuidApi,
129+
ProductDraftUuidApiInterface $productDraftUuidApi,
127130
AppCatalogApiInterface $appCatalogApi,
128131
AppCatalogProductApiInterface $appCatalogProductApi
129132
) {
@@ -162,6 +165,7 @@ public function __construct(
162165
$this->assetAttributeOptionApi = $assetAttributeOptionApi;
163166
$this->assetMediaFileApi = $assetMediaFileApi;
164167
$this->productUuidApi = $productUuidApi;
168+
$this->productDraftUuidApi = $productDraftUuidApi;
165169
$this->appCatalogApi = $appCatalogApi;
166170
$this->appCatalogProductApi = $appCatalogProductApi;
167171
}
@@ -454,6 +458,14 @@ public function getProductUuidApi(): ProductUuidApiInterface
454458
return $this->productUuidApi;
455459
}
456460

461+
/**
462+
* {@inheritDoc}
463+
*/
464+
public function getProductDraftUuidApi(): ProductDraftUuidApiInterface
465+
{
466+
return $this->productDraftUuidApi;
467+
}
468+
457469
/**
458470
* {@inheritDoc}
459471
*/

src/AkeneoPimClientBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApi;
3030
use Akeneo\Pim\ApiClient\Api\ProductApi;
3131
use Akeneo\Pim\ApiClient\Api\ProductDraftApi;
32+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApi;
3233
use Akeneo\Pim\ApiClient\Api\ProductMediaFileApi;
3334
use Akeneo\Pim\ApiClient\Api\ProductModelApi;
3435
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApi;
@@ -251,6 +252,7 @@ protected function buildAuthenticatedClient(Authentication $authentication): Ake
251252
new AssetAttributeOptionApi($resourceClient),
252253
new AssetMediaFileApi($resourceClient, $fileSystem),
253254
new ProductUuidApi($resourceClient, $pageFactory, $cursorFactory),
255+
new ProductDraftUuidApi($resourceClient, $pageFactory, $cursorFactory),
254256
new AppCatalogApi($resourceClient, $pageFactory, $cursorFactory),
255257
new AppCatalogProductApi($resourceClient, $pageFactory, $cursorFactory)
256258
);

src/AkeneoPimClientInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
3030
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
3131
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
32+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
3233
use Akeneo\Pim\ApiClient\Api\ProductModelApiInterface;
3334
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApiInterface;
3435
use Akeneo\Pim\ApiClient\Api\ProductUuidApiInterface;
@@ -120,6 +121,8 @@ public function getAssetMediaFileApi(): AssetMediaFileApiInterface;
120121

121122
public function getProductUuidApi() : ProductUuidApiInterface;
122123

124+
public function getProductDraftUuidApi() : ProductDraftUuidApiInterface;
125+
123126
public function getAppCatalogApi(): AppCatalogApiInterface;
124127

125128
public function getAppCatalogProductApi(): AppCatalogProductApiInterface;

src/Api/ProductDraftUuidApi.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akeneo\Pim\ApiClient\Api;
6+
7+
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
8+
use Akeneo\Pim\ApiClient\Pagination\PageFactoryInterface;
9+
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorFactoryInterface;
10+
11+
/**
12+
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
13+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
14+
*/
15+
class ProductDraftUuidApi implements ProductDraftUuidApiInterface
16+
{
17+
public const PRODUCT_DRAFT_UUID_URI = '/api/rest/v1/products-uuid/%s/draft';
18+
public const PRODUCT_PROPOSAL_UUID_URI = '/api/rest/v1/products-uuid/%s/proposal';
19+
20+
protected ResourceClientInterface $resourceClient;
21+
protected PageFactoryInterface $pageFactory;
22+
protected ResourceCursorFactoryInterface $cursorFactory;
23+
24+
public function __construct(
25+
ResourceClientInterface $resourceClient,
26+
PageFactoryInterface $pageFactory,
27+
ResourceCursorFactoryInterface $cursorFactory
28+
) {
29+
$this->resourceClient = $resourceClient;
30+
$this->pageFactory = $pageFactory;
31+
$this->cursorFactory = $cursorFactory;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function get(string $uuid): array
38+
{
39+
return $this->resourceClient->getResource(static::PRODUCT_DRAFT_UUID_URI, [$uuid]);
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function submitForApproval(string $uuid): int
46+
{
47+
return $this->resourceClient->createResource(static::PRODUCT_PROPOSAL_UUID_URI, [$uuid]);
48+
}
49+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Akeneo\Pim\ApiClient\Api;
6+
7+
use Akeneo\Pim\ApiClient\Api\Operation\GettableResourceInterface;
8+
9+
/**
10+
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
11+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
12+
*/
13+
interface ProductDraftUuidApiInterface extends GettableResourceInterface
14+
{
15+
/**
16+
* Submits a product draft for approval, by its uuid.
17+
*/
18+
public function submitForApproval(string $uuid): int;
19+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\tests\Api;
4+
5+
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApi;
6+
use Akeneo\Pim\ApiClient\Client\HttpClient;
7+
use donatj\MockWebServer\RequestInfo;
8+
use donatj\MockWebServer\Response;
9+
use donatj\MockWebServer\ResponseStack;
10+
use PHPUnit\Framework\Assert;
11+
12+
/**
13+
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
14+
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
15+
*/
16+
class GetProductDraftUuidIntegration extends ApiTestCase
17+
{
18+
public function test_get_product()
19+
{
20+
$this->server->setResponseOfPath(
21+
'/'.sprintf(ProductDraftUuidApi::PRODUCT_DRAFT_UUID_URI, '12951d98-210e-4bRC-ab18-7fdgf1bd14f3'),
22+
new ResponseStack(
23+
new Response($this->getProductDraft(), [], HttpClient::HTTP_OK)
24+
)
25+
);
26+
27+
$api = $this->createClientByPassword()->getProductDraftUuidApi();
28+
$product = $api->get('12951d98-210e-4bRC-ab18-7fdgf1bd14f3');
29+
30+
Assert::assertSame('GET', $this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_METHOD]);
31+
Assert::assertEquals($product, json_decode($this->getProductDraft(), true));
32+
}
33+
34+
public function test_get_unknown_product()
35+
{
36+
$this->server->setResponseOfPath(
37+
'/'.sprintf(ProductDraftUuidApi::PRODUCT_DRAFT_UUID_URI, '12951d98-210e-4bRC-ab18-7fdgf1bd14f3'),
38+
new ResponseStack(
39+
new Response('{"code": 404, "message":"Resource `12951d98-210e-4bRC-ab18-7fdgf1bd14f3` does not exist."}', [], 404)
40+
)
41+
);
42+
43+
$this->expectException(\Akeneo\Pim\ApiClient\Exception\NotFoundHttpException::class);
44+
$this->expectExceptionMessage('Resource `12951d98-210e-4bRC-ab18-7fdgf1bd14f3` does not exist.');
45+
46+
$api = $this->createClientByPassword()->getProductDraftUuidApi();
47+
$api->get('12951d98-210e-4bRC-ab18-7fdgf1bd14f3');
48+
}
49+
50+
private function getProductDraft(): string
51+
{
52+
return <<<JSON
53+
[
54+
{
55+
"uuid" : "12951d98-210e-4bRC-ab18-7fdgf1bd14f3",
56+
"identifier" : "black_sneakers",
57+
"family" : "sneakers",
58+
"groups": [],
59+
"categories": ["summer_collection"],
60+
"values": [{
61+
"color": {"locale": null, "scope": null, "data": "black"}
62+
}]
63+
}
64+
]
65+
JSON;
66+
}
67+
}

0 commit comments

Comments
 (0)