Skip to content

Commit

Permalink
Merge pull request #248 from akeneo/API-1835-bis
Browse files Browse the repository at this point in the history
Add Product Draft UUID routes
  • Loading branch information
LevFlavien authored Sep 29, 2022
2 parents fb4deb7 + 0f1f12e commit 8619fbd
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/AkeneoPimClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductModelApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductUuidApiInterface;
Expand Down Expand Up @@ -81,6 +82,7 @@ function let(
AssetAttributeOptionApiInterface $assetAttributeOptionApi,
AssetMediaFileApiInterface $assetMediaFileApi,
ProductUuidApiInterface $productUuidApi,
ProductDraftUuidApiInterface $productDraftUuidApi,
AppCatalogApiInterface $appCatalogApi,
AppCatalogProductApiInterface $appCatalogProductApi
) {
Expand Down Expand Up @@ -120,6 +122,7 @@ function let(
$assetAttributeOptionApi,
$assetMediaFileApi,
$productUuidApi,
$productDraftUuidApi,
$appCatalogApi,
$appCatalogProductApi
);
Expand Down Expand Up @@ -300,6 +303,11 @@ function it_gets_product_uuid_api(ProductUuidApiInterface $productUuidApi)
$this->getProductUuidApi()->shouldReturn($productUuidApi);
}

function it_gets_product_draft_uuid_api(ProductDraftUuidApiInterface $productDraftUuidApi)
{
$this->getProductDraftUuidApi()->shouldReturn($productDraftUuidApi);
}

function it_gets_app_catalog_api(AppCatalogApiInterface $appCatalogApi)
{
$this->getAppCatalogApi()->shouldReturn($appCatalogApi);
Expand Down
59 changes: 59 additions & 0 deletions spec/Api/ProductDraftUuidApiSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace spec\Akeneo\Pim\ApiClient\Api;

use Akeneo\Pim\ApiClient\Api\Operation\GettableResourceInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApi;
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
use Akeneo\Pim\ApiClient\Pagination\PageFactoryInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorFactoryInterface;
use PhpSpec\ObjectBehavior;

class ProductDraftUuidApiSpec extends ObjectBehavior
{
function let(
ResourceClientInterface $resourceClient,
PageFactoryInterface $pageFactory,
ResourceCursorFactoryInterface $cursorFactory
) {
$this->beConstructedWith($resourceClient, $pageFactory, $cursorFactory);
}

function it_is_initializable()
{
$this->shouldHaveType(ProductDraftUuidApi::class);
$this->shouldImplement(ProductDraftUuidApiInterface::class);
$this->shouldImplement(GettableResourceInterface::class);
}

function it_gets_a_product_draft($resourceClient)
{
$draft = [
'uuid' => '944ca210-d8e0-4c57-9529-741e17e95c8d',
'family' => 'bar',
'parent' => null,
'groups' => [],
'categories' => [],
'enabled' => true,
'values' => [],
'created' => 'this is a date formatted to ISO-8601',
'updated' => 'this is a date formatted to ISO-8601',
'associations' => [],
'metadata' => [
'workflow_status' => 'draft_in_progress',
],
];

$resourceClient->getResource(ProductDraftUuidApi::PRODUCT_DRAFT_UUID_URI, ['944ca210-d8e0-4c57-9529-741e17e95c8d'])->willReturn($draft);

$this->get('944ca210-d8e0-4c57-9529-741e17e95c8d')->shouldReturn($draft);
}

function it_submits_a_product_draft_for_approval($resourceClient)
{
$resourceClient->createResource(ProductDraftUuidApi::PRODUCT_PROPOSAL_UUID_URI, ['944ca210-d8e0-4c57-9529-741e17e95c8d'])->willReturn(201);

$this->submitForApproval('944ca210-d8e0-4c57-9529-741e17e95c8d')->shouldReturn(201);
}
}
12 changes: 12 additions & 0 deletions src/AkeneoPimClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductModelApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductUuidApiInterface;
Expand Down Expand Up @@ -87,6 +88,7 @@ class AkeneoPimClient implements AkeneoPimClientInterface
private AppCatalogProductApiInterface $appCatalogProductApi;

private ProductUuidApiInterface $productUuidApi;
private ProductDraftUuidApiInterface $productDraftUuidApi;

public function __construct(
Authentication $authentication,
Expand Down Expand Up @@ -124,6 +126,7 @@ public function __construct(
AssetAttributeOptionApiInterface $assetAttributeOptionApi,
AssetMediaFileApiInterface $assetMediaFileApi,
ProductUuidApiInterface $productUuidApi,
ProductDraftUuidApiInterface $productDraftUuidApi,
AppCatalogApiInterface $appCatalogApi,
AppCatalogProductApiInterface $appCatalogProductApi
) {
Expand Down Expand Up @@ -162,6 +165,7 @@ public function __construct(
$this->assetAttributeOptionApi = $assetAttributeOptionApi;
$this->assetMediaFileApi = $assetMediaFileApi;
$this->productUuidApi = $productUuidApi;
$this->productDraftUuidApi = $productDraftUuidApi;
$this->appCatalogApi = $appCatalogApi;
$this->appCatalogProductApi = $appCatalogProductApi;
}
Expand Down Expand Up @@ -454,6 +458,14 @@ public function getProductUuidApi(): ProductUuidApiInterface
return $this->productUuidApi;
}

/**
* {@inheritDoc}
*/
public function getProductDraftUuidApi(): ProductDraftUuidApiInterface
{
return $this->productDraftUuidApi;
}

/**
* {@inheritDoc}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/AkeneoPimClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Akeneo\Pim\ApiClient\Api\MeasurementFamilyApi;
use Akeneo\Pim\ApiClient\Api\ProductApi;
use Akeneo\Pim\ApiClient\Api\ProductDraftApi;
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApi;
use Akeneo\Pim\ApiClient\Api\ProductMediaFileApi;
use Akeneo\Pim\ApiClient\Api\ProductModelApi;
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApi;
Expand Down Expand Up @@ -251,6 +252,7 @@ protected function buildAuthenticatedClient(Authentication $authentication): Ake
new AssetAttributeOptionApi($resourceClient),
new AssetMediaFileApi($resourceClient, $fileSystem),
new ProductUuidApi($resourceClient, $pageFactory, $cursorFactory),
new ProductDraftUuidApi($resourceClient, $pageFactory, $cursorFactory),
new AppCatalogApi($resourceClient, $pageFactory, $cursorFactory),
new AppCatalogProductApi($resourceClient, $pageFactory, $cursorFactory)
);
Expand Down
3 changes: 3 additions & 0 deletions src/AkeneoPimClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductModelApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductModelDraftApiInterface;
use Akeneo\Pim\ApiClient\Api\ProductUuidApiInterface;
Expand Down Expand Up @@ -120,6 +121,8 @@ public function getAssetMediaFileApi(): AssetMediaFileApiInterface;

public function getProductUuidApi() : ProductUuidApiInterface;

public function getProductDraftUuidApi() : ProductDraftUuidApiInterface;

public function getAppCatalogApi(): AppCatalogApiInterface;

public function getAppCatalogProductApi(): AppCatalogProductApiInterface;
Expand Down
49 changes: 49 additions & 0 deletions src/Api/ProductDraftUuidApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Akeneo\Pim\ApiClient\Api;

use Akeneo\Pim\ApiClient\Client\ResourceClientInterface;
use Akeneo\Pim\ApiClient\Pagination\PageFactoryInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorFactoryInterface;

/**
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class ProductDraftUuidApi implements ProductDraftUuidApiInterface
{
public const PRODUCT_DRAFT_UUID_URI = '/api/rest/v1/products-uuid/%s/draft';
public const PRODUCT_PROPOSAL_UUID_URI = '/api/rest/v1/products-uuid/%s/proposal';

protected ResourceClientInterface $resourceClient;
protected PageFactoryInterface $pageFactory;
protected ResourceCursorFactoryInterface $cursorFactory;

public function __construct(
ResourceClientInterface $resourceClient,
PageFactoryInterface $pageFactory,
ResourceCursorFactoryInterface $cursorFactory
) {
$this->resourceClient = $resourceClient;
$this->pageFactory = $pageFactory;
$this->cursorFactory = $cursorFactory;
}

/**
* {@inheritdoc}
*/
public function get(string $uuid): array
{
return $this->resourceClient->getResource(static::PRODUCT_DRAFT_UUID_URI, [$uuid]);
}

/**
* {@inheritdoc}
*/
public function submitForApproval(string $uuid): int
{
return $this->resourceClient->createResource(static::PRODUCT_PROPOSAL_UUID_URI, [$uuid]);
}
}
19 changes: 19 additions & 0 deletions src/Api/ProductDraftUuidApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Akeneo\Pim\ApiClient\Api;

use Akeneo\Pim\ApiClient\Api\Operation\GettableResourceInterface;

/**
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
interface ProductDraftUuidApiInterface extends GettableResourceInterface
{
/**
* Submits a product draft for approval, by its uuid.
*/
public function submitForApproval(string $uuid): int;
}
67 changes: 67 additions & 0 deletions tests/Api/ProductDraftUuid/GetProductDraftUuidIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Akeneo\Pim\ApiClient\tests\Api;

use Akeneo\Pim\ApiClient\Api\ProductDraftUuidApi;
use Akeneo\Pim\ApiClient\Client\HttpClient;
use donatj\MockWebServer\RequestInfo;
use donatj\MockWebServer\Response;
use donatj\MockWebServer\ResponseStack;
use PHPUnit\Framework\Assert;

/**
* @copyright 2022 Akeneo SAS (https://www.akeneo.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class GetProductDraftUuidIntegration extends ApiTestCase
{
public function test_get_product()
{
$this->server->setResponseOfPath(
'/'.sprintf(ProductDraftUuidApi::PRODUCT_DRAFT_UUID_URI, '12951d98-210e-4bRC-ab18-7fdgf1bd14f3'),
new ResponseStack(
new Response($this->getProductDraft(), [], HttpClient::HTTP_OK)
)
);

$api = $this->createClientByPassword()->getProductDraftUuidApi();
$product = $api->get('12951d98-210e-4bRC-ab18-7fdgf1bd14f3');

Assert::assertSame('GET', $this->server->getLastRequest()->jsonSerialize()[RequestInfo::JSON_KEY_METHOD]);
Assert::assertEquals($product, json_decode($this->getProductDraft(), true));
}

public function test_get_unknown_product()
{
$this->server->setResponseOfPath(
'/'.sprintf(ProductDraftUuidApi::PRODUCT_DRAFT_UUID_URI, '12951d98-210e-4bRC-ab18-7fdgf1bd14f3'),
new ResponseStack(
new Response('{"code": 404, "message":"Resource `12951d98-210e-4bRC-ab18-7fdgf1bd14f3` does not exist."}', [], 404)
)
);

$this->expectException(\Akeneo\Pim\ApiClient\Exception\NotFoundHttpException::class);
$this->expectExceptionMessage('Resource `12951d98-210e-4bRC-ab18-7fdgf1bd14f3` does not exist.');

$api = $this->createClientByPassword()->getProductDraftUuidApi();
$api->get('12951d98-210e-4bRC-ab18-7fdgf1bd14f3');
}

private function getProductDraft(): string
{
return <<<JSON
[
{
"uuid" : "12951d98-210e-4bRC-ab18-7fdgf1bd14f3",
"identifier" : "black_sneakers",
"family" : "sneakers",
"groups": [],
"categories": ["summer_collection"],
"values": [{
"color": {"locale": null, "scope": null, "data": "black"}
}]
}
]
JSON;
}
}

0 comments on commit 8619fbd

Please sign in to comment.