Skip to content

Commit acd025b

Browse files
authored
Merge pull request #64 from akeneo/API-305_API-306_API-307
API-305_API-306_API-307: Add create association type, upsert association type and upsert list of association types
2 parents fbc4503 + 889bfec commit acd025b

6 files changed

+377
-1
lines changed

Diff for: spec/Api/AssociationTypeApiSpec.php

+53
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Akeneo\Pim\Api\GettableResourceInterface;
88
use Akeneo\Pim\Api\ListableResourceInterface;
99
use Akeneo\Pim\Client\ResourceClientInterface;
10+
use Akeneo\Pim\Exception\InvalidArgumentException;
1011
use Akeneo\Pim\Pagination\PageFactoryInterface;
1112
use Akeneo\Pim\Pagination\PageInterface;
1213
use Akeneo\Pim\Pagination\ResourceCursorFactoryInterface;
1314
use Akeneo\Pim\Pagination\ResourceCursorInterface;
15+
use Akeneo\Pim\Stream\UpsertResourceListResponse;
1416
use PhpSpec\ObjectBehavior;
1517

1618
class AssociationTypeApiSpec extends ObjectBehavior
@@ -108,4 +110,55 @@ function it_returns_a_list_of_association_types_with_additional_query_parameters
108110

109111
$this->listPerPage(null, null, ['foo' => 'bar'])->shouldReturn($page);
110112
}
113+
114+
function it_creates_an_association_type($resourceClient)
115+
{
116+
$resourceClient
117+
->createResource(
118+
AssociationTypeApi::ASSOCIATION_TYPES_URI,
119+
[],
120+
['code' => 'NEW_SELL']
121+
)
122+
->willReturn(201);
123+
124+
$this->create('NEW_SELL', [])->shouldReturn(201);
125+
}
126+
127+
function it_throws_an_exception_if_code_is_provided_in_data_when_creating_an_association_type($resourceClient)
128+
{
129+
$this
130+
->shouldThrow(new InvalidArgumentException('The parameter "code" should not be defined in the data parameter'))
131+
->during('create', ['NEW_SELL', ['code' => 'NEW_SELL']]);
132+
}
133+
134+
function it_upserts_an_association_type($resourceClient)
135+
{
136+
$resourceClient
137+
->upsertResource(AssociationTypeApi::ASSOCIATION_TYPE_URI, ['UPSELL'], [])
138+
->willReturn(204);
139+
140+
$this->upsert('UPSELL', [])->shouldReturn(204);
141+
}
142+
143+
function it_upserts_a_list_of_association_types($resourceClient, UpsertResourceListResponse $response)
144+
{
145+
$resourceClient
146+
->upsertResourceList(
147+
AssociationTypeApi::ASSOCIATION_TYPES_URI,
148+
[],
149+
[
150+
['code' => 'association_type_1'],
151+
['code' => 'association_type_2'],
152+
['code' => 'association_type_3'],
153+
]
154+
)
155+
->willReturn($response);
156+
157+
$this
158+
->upsertList([
159+
['code' => 'association_type_1'],
160+
['code' => 'association_type_2'],
161+
['code' => 'association_type_3'],
162+
])->shouldReturn($response);
163+
}
111164
}

Diff for: src/Api/AssociationTypeApi.php

+33
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace Akeneo\Pim\Api;
44

55
use Akeneo\Pim\Client\ResourceClientInterface;
6+
use Akeneo\Pim\Exception\HttpException;
7+
use Akeneo\Pim\Exception\InvalidArgumentException;
68
use Akeneo\Pim\Pagination\PageFactoryInterface;
79
use Akeneo\Pim\Pagination\ResourceCursorFactoryInterface;
10+
use Psr\Http\Message\StreamInterface;
811

912
/**
1013
* @author Philippe Mossière <[email protected]>
@@ -66,4 +69,34 @@ public function all($pageSize = 10, array $queryParameters = [])
6669

6770
return $this->cursorFactory->createCursor($pageSize, $firstPage);
6871
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function create($code, array $data = [])
77+
{
78+
if (array_key_exists('code', $data)) {
79+
throw new InvalidArgumentException('The parameter "code" should not be defined in the data parameter');
80+
}
81+
82+
$data['code'] = $code;
83+
84+
return $this->resourceClient->createResource(static::ASSOCIATION_TYPES_URI, [], $data);
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function upsert($code, array $data = [])
91+
{
92+
return $this->resourceClient->upsertResource(static::ASSOCIATION_TYPE_URI, [$code], $data);
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
public function upsertList($associationTypes)
99+
{
100+
return $this->resourceClient->upsertResourceList(static::ASSOCIATION_TYPES_URI, [], $associationTypes);
101+
}
69102
}

Diff for: src/Api/AssociationTypeApiInterface.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
1010
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
1111
*/
12-
interface AssociationTypeApiInterface extends GettableResourceInterface, ListableResourceInterface
12+
interface AssociationTypeApiInterface extends
13+
GettableResourceInterface,
14+
ListableResourceInterface,
15+
CreatableResourceInterface,
16+
UpsertableResourceInterface,
17+
UpsertableResourceListInterface
1318
{
1419
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\tests\v1_8\Api\AssociationType;
4+
5+
use Akeneo\Pim\Exception\UnprocessableEntityHttpException;
6+
use Akeneo\Pim\tests\Common\Api\ApiTestCase;
7+
8+
class CreateAssociationTypeApiIntegration extends ApiTestCase
9+
{
10+
public function testCreate()
11+
{
12+
$api = $this->createClient()->getAssociationTypeApi();
13+
$response = $api->create(
14+
'NEW_SELL',
15+
[
16+
'labels' => [
17+
'en_US' => 'New sell',
18+
'fr_FR' => 'Nouvelle vente',
19+
],
20+
]
21+
);
22+
23+
$this->assertSame(201, $response);
24+
25+
$associationType = $api->get('NEW_SELL');
26+
$this->assertSameContent(
27+
[
28+
'code' => 'NEW_SELL',
29+
'labels' => [
30+
'en_US' => 'New sell',
31+
'fr_FR' => 'Nouvelle vente',
32+
],
33+
],
34+
$associationType
35+
);
36+
}
37+
38+
public function testCreateAnExistingAssociationType()
39+
{
40+
$api = $this->createClient()->getAssociationTypeApi();
41+
42+
try {
43+
$api->create(
44+
'UPSELL',
45+
[
46+
'labels' => [
47+
'en_US' => 'Upsell',
48+
],
49+
]
50+
);
51+
} catch (UnprocessableEntityHttpException $exception) {
52+
$this->assertSame(
53+
[
54+
[
55+
'property' => 'code',
56+
'message' => 'This value is already used.',
57+
],
58+
],
59+
$exception->getResponseErrors()
60+
);
61+
}
62+
}
63+
64+
/**
65+
* @expectedException \Akeneo\Pim\Exception\UnprocessableEntityHttpException
66+
*/
67+
public function testCreateAnInvalidAssociationType()
68+
{
69+
$api = $this->createClient()->getAssociationTypeApi();
70+
$api->create(
71+
'fail',
72+
[
73+
'labels' => 'Upsell',
74+
]
75+
);
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\tests\v1_8\Api\AssociationType;
4+
5+
use Akeneo\Pim\tests\Common\Api\ApiTestCase;
6+
7+
class UpsertAssociationTypeApiIntegration extends ApiTestCase
8+
{
9+
public function testUpsertDoingUpdate()
10+
{
11+
$api = $this->createClient()->getAssociationTypeApi();
12+
13+
$response = $api->upsert(
14+
'X_SELL',
15+
[
16+
'labels' => [
17+
'en_US' => 'Cross sell',
18+
'fr_FR' => 'Vente croisée',
19+
],
20+
]
21+
);
22+
23+
$this->assertSame(204, $response);
24+
25+
$associationType = $api->get('X_SELL');
26+
$this->assertSameContent(
27+
[
28+
'code' => 'X_SELL',
29+
'labels' => [
30+
'en_US' => 'Cross sell',
31+
'fr_FR' => 'Vente croisée',
32+
],
33+
],
34+
$associationType
35+
);
36+
}
37+
38+
public function testUpsertDoingCreate()
39+
{
40+
$api = $this->createClient()->getAssociationTypeApi();
41+
$response = $api->upsert(
42+
'NEW_SELL',
43+
[
44+
'labels' => [
45+
'en_US' => 'New sell',
46+
'fr_FR' => 'Nouvelle vente',
47+
],
48+
]
49+
);
50+
51+
$this->assertSame(201, $response);
52+
53+
$associationType = $api->get('NEW_SELL');
54+
$this->assertSameContent(
55+
[
56+
'code' => 'NEW_SELL',
57+
'labels' => [
58+
'en_US' => 'New sell',
59+
'fr_FR' => 'Nouvelle vente',
60+
],
61+
],
62+
$associationType
63+
);
64+
}
65+
66+
/**
67+
* @expectedException \Akeneo\Pim\Exception\UnprocessableEntityHttpException
68+
*/
69+
public function testUpsertWrongDataTypeFail()
70+
{
71+
$api = $this->createClient()->getAssociationTypeApi();
72+
$api->upsert(
73+
'NEW_SELL',
74+
[
75+
'labels' => [
76+
'en_US' => ['New sell'],
77+
],
78+
]
79+
);
80+
}
81+
82+
/**
83+
* @expectedException \Akeneo\Pim\Exception\UnprocessableEntityHttpException
84+
*/
85+
public function testUpsertInvalidCodeFail()
86+
{
87+
$api = $this->createClient()->getCategoryApi();
88+
$api->upsert('invalid code !');
89+
}
90+
}

0 commit comments

Comments
 (0)