Skip to content

Commit

Permalink
Merge pull request #64 from akeneo/API-305_API-306_API-307
Browse files Browse the repository at this point in the history
API-305_API-306_API-307: Add create association type, upsert association type and upsert list of association types
  • Loading branch information
momoss authored Aug 2, 2017
2 parents fbc4503 + 889bfec commit acd025b
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 1 deletion.
53 changes: 53 additions & 0 deletions spec/Api/AssociationTypeApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Akeneo\Pim\Api\GettableResourceInterface;
use Akeneo\Pim\Api\ListableResourceInterface;
use Akeneo\Pim\Client\ResourceClientInterface;
use Akeneo\Pim\Exception\InvalidArgumentException;
use Akeneo\Pim\Pagination\PageFactoryInterface;
use Akeneo\Pim\Pagination\PageInterface;
use Akeneo\Pim\Pagination\ResourceCursorFactoryInterface;
use Akeneo\Pim\Pagination\ResourceCursorInterface;
use Akeneo\Pim\Stream\UpsertResourceListResponse;
use PhpSpec\ObjectBehavior;

class AssociationTypeApiSpec extends ObjectBehavior
Expand Down Expand Up @@ -108,4 +110,55 @@ function it_returns_a_list_of_association_types_with_additional_query_parameters

$this->listPerPage(null, null, ['foo' => 'bar'])->shouldReturn($page);
}

function it_creates_an_association_type($resourceClient)
{
$resourceClient
->createResource(
AssociationTypeApi::ASSOCIATION_TYPES_URI,
[],
['code' => 'NEW_SELL']
)
->willReturn(201);

$this->create('NEW_SELL', [])->shouldReturn(201);
}

function it_throws_an_exception_if_code_is_provided_in_data_when_creating_an_association_type($resourceClient)
{
$this
->shouldThrow(new InvalidArgumentException('The parameter "code" should not be defined in the data parameter'))
->during('create', ['NEW_SELL', ['code' => 'NEW_SELL']]);
}

function it_upserts_an_association_type($resourceClient)
{
$resourceClient
->upsertResource(AssociationTypeApi::ASSOCIATION_TYPE_URI, ['UPSELL'], [])
->willReturn(204);

$this->upsert('UPSELL', [])->shouldReturn(204);
}

function it_upserts_a_list_of_association_types($resourceClient, UpsertResourceListResponse $response)
{
$resourceClient
->upsertResourceList(
AssociationTypeApi::ASSOCIATION_TYPES_URI,
[],
[
['code' => 'association_type_1'],
['code' => 'association_type_2'],
['code' => 'association_type_3'],
]
)
->willReturn($response);

$this
->upsertList([
['code' => 'association_type_1'],
['code' => 'association_type_2'],
['code' => 'association_type_3'],
])->shouldReturn($response);
}
}
33 changes: 33 additions & 0 deletions src/Api/AssociationTypeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Akeneo\Pim\Api;

use Akeneo\Pim\Client\ResourceClientInterface;
use Akeneo\Pim\Exception\HttpException;
use Akeneo\Pim\Exception\InvalidArgumentException;
use Akeneo\Pim\Pagination\PageFactoryInterface;
use Akeneo\Pim\Pagination\ResourceCursorFactoryInterface;
use Psr\Http\Message\StreamInterface;

/**
* @author Philippe Mossière <philippe.mossiere@akeneo.com>
Expand Down Expand Up @@ -66,4 +69,34 @@ public function all($pageSize = 10, array $queryParameters = [])

return $this->cursorFactory->createCursor($pageSize, $firstPage);
}

/**
* {@inheritdoc}
*/
public function create($code, array $data = [])
{
if (array_key_exists('code', $data)) {
throw new InvalidArgumentException('The parameter "code" should not be defined in the data parameter');
}

$data['code'] = $code;

return $this->resourceClient->createResource(static::ASSOCIATION_TYPES_URI, [], $data);
}

/**
* {@inheritdoc}
*/
public function upsert($code, array $data = [])
{
return $this->resourceClient->upsertResource(static::ASSOCIATION_TYPE_URI, [$code], $data);
}

/**
* {@inheritdoc}
*/
public function upsertList($associationTypes)
{
return $this->resourceClient->upsertResourceList(static::ASSOCIATION_TYPES_URI, [], $associationTypes);
}
}
7 changes: 6 additions & 1 deletion src/Api/AssociationTypeApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
interface AssociationTypeApiInterface extends GettableResourceInterface, ListableResourceInterface
interface AssociationTypeApiInterface extends
GettableResourceInterface,
ListableResourceInterface,
CreatableResourceInterface,
UpsertableResourceInterface,
UpsertableResourceListInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Akeneo\Pim\tests\v1_8\Api\AssociationType;

use Akeneo\Pim\Exception\UnprocessableEntityHttpException;
use Akeneo\Pim\tests\Common\Api\ApiTestCase;

class CreateAssociationTypeApiIntegration extends ApiTestCase
{
public function testCreate()
{
$api = $this->createClient()->getAssociationTypeApi();
$response = $api->create(
'NEW_SELL',
[
'labels' => [
'en_US' => 'New sell',
'fr_FR' => 'Nouvelle vente',
],
]
);

$this->assertSame(201, $response);

$associationType = $api->get('NEW_SELL');
$this->assertSameContent(
[
'code' => 'NEW_SELL',
'labels' => [
'en_US' => 'New sell',
'fr_FR' => 'Nouvelle vente',
],
],
$associationType
);
}

public function testCreateAnExistingAssociationType()
{
$api = $this->createClient()->getAssociationTypeApi();

try {
$api->create(
'UPSELL',
[
'labels' => [
'en_US' => 'Upsell',
],
]
);
} catch (UnprocessableEntityHttpException $exception) {
$this->assertSame(
[
[
'property' => 'code',
'message' => 'This value is already used.',
],
],
$exception->getResponseErrors()
);
}
}

/**
* @expectedException \Akeneo\Pim\Exception\UnprocessableEntityHttpException
*/
public function testCreateAnInvalidAssociationType()
{
$api = $this->createClient()->getAssociationTypeApi();
$api->create(
'fail',
[
'labels' => 'Upsell',
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Akeneo\Pim\tests\v1_8\Api\AssociationType;

use Akeneo\Pim\tests\Common\Api\ApiTestCase;

class UpsertAssociationTypeApiIntegration extends ApiTestCase
{
public function testUpsertDoingUpdate()
{
$api = $this->createClient()->getAssociationTypeApi();

$response = $api->upsert(
'X_SELL',
[
'labels' => [
'en_US' => 'Cross sell',
'fr_FR' => 'Vente croisée',
],
]
);

$this->assertSame(204, $response);

$associationType = $api->get('X_SELL');
$this->assertSameContent(
[
'code' => 'X_SELL',
'labels' => [
'en_US' => 'Cross sell',
'fr_FR' => 'Vente croisée',
],
],
$associationType
);
}

public function testUpsertDoingCreate()
{
$api = $this->createClient()->getAssociationTypeApi();
$response = $api->upsert(
'NEW_SELL',
[
'labels' => [
'en_US' => 'New sell',
'fr_FR' => 'Nouvelle vente',
],
]
);

$this->assertSame(201, $response);

$associationType = $api->get('NEW_SELL');
$this->assertSameContent(
[
'code' => 'NEW_SELL',
'labels' => [
'en_US' => 'New sell',
'fr_FR' => 'Nouvelle vente',
],
],
$associationType
);
}

/**
* @expectedException \Akeneo\Pim\Exception\UnprocessableEntityHttpException
*/
public function testUpsertWrongDataTypeFail()
{
$api = $this->createClient()->getAssociationTypeApi();
$api->upsert(
'NEW_SELL',
[
'labels' => [
'en_US' => ['New sell'],
],
]
);
}

/**
* @expectedException \Akeneo\Pim\Exception\UnprocessableEntityHttpException
*/
public function testUpsertInvalidCodeFail()
{
$api = $this->createClient()->getCategoryApi();
$api->upsert('invalid code !');
}
}
Loading

0 comments on commit acd025b

Please sign in to comment.