Skip to content

Commit 4dd314b

Browse files
Merge pull request #109 from akeneo/API-562
API-562: upsert a list of attribute options
2 parents 8c68def + f0de8fd commit 4dd314b

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

.ci/Jenkinsfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ def runPim2IntegrationTest(String phpVersion, String client, String psrImplem, S
385385
def files = []
386386

387387
// Find and store PHP test integration files to launch them in parallels
388+
if ("2.1" == pimVersion) {
389+
files += sh (returnStdout: true, script: 'find /home/jenkins/php-api-client/tests/v2_1/Api -name "*Integration.php"').tokenize('\n')
390+
}
388391
files += sh (returnStdout: true, script: 'find /home/jenkins/php-api-client/tests/v2_0/Api -name "*Integration.php"').tokenize('\n')
389392
files += sh (returnStdout: true, script: 'find /home/jenkins/php-api-client/tests/Common/Api -name "*Integration.php"').tokenize('\n')
390393
for (file in files) {

phpunit.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@
2525
<directory suffix="Integration.php">tests/v2_0/Api/*</directory>
2626
</testsuite>
2727

28+
<testsuite name="PHP_Client_Test_2_1">
29+
<directory suffix="Integration.php">tests/Common/Api/*</directory>
30+
<directory suffix="Integration.php">tests/v2_0/Api/*</directory>
31+
<directory suffix="Integration.php">tests/v2_1/Api/*</directory>
32+
</testsuite>
33+
2834
</phpunit>

spec/Api/AttributeOptionApiSpec.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
1111
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorFactoryInterface;
1212
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
13+
use Akeneo\Pim\ApiClient\Stream\UpsertResourceListResponse;
1314
use PhpSpec\ObjectBehavior;
1415

1516
class AttributeOptionApiSpec extends ObjectBehavior
@@ -149,4 +150,17 @@ function it_upserts_an_attribute_option($resourceClient)
149150
->upsert('foo', 'bar', ['code' => 'bar', 'attribute' => 'foo', 'sort_order' => 42])
150151
->shouldReturn(204);
151152
}
153+
154+
function it_upserts_a_list_of_attribute_options($resourceClient, UpsertResourceListResponse $response)
155+
{
156+
$resourceClient->upsertResourceList(AttributeOptionApi::ATTRIBUTE_OPTIONS_URI, ['foo'], [
157+
['code' => 'bar', 'attribute' => 'foo', 'sort_order' => 42],
158+
['code' => 'fighters', 'attribute' => 'foo', 'sort_order' => 43]
159+
])->willReturn($response);
160+
161+
$this->upsertList('foo', [
162+
['code' => 'bar', 'attribute' => 'foo', 'sort_order' => 42],
163+
['code' => 'fighters', 'attribute' => 'foo', 'sort_order' => 43]
164+
])->shouldReturn($response);
165+
}
152166
}

src/Api/AttributeOptionApi.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ public function upsert($attributeCode, $attributeOptionCode, array $data = [])
9898
{
9999
return $this->resourceClient->upsertResource(static::ATTRIBUTE_OPTION_URI, [$attributeCode, $attributeOptionCode], $data);
100100
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public function upsertList($attributeCode, $attributeOptions)
106+
{
107+
return $this->resourceClient->upsertResourceList(static::ATTRIBUTE_OPTIONS_URI, [$attributeCode], $attributeOptions);
108+
}
101109
}

src/Api/AttributeOptionApiInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,16 @@ public function create($attributeCode, $attributeOptionCode, array $data = []);
8585
* @return int returns either http code 201 if the attribute option has been created or 204 if it has been updated
8686
*/
8787
public function upsert($attributeCode, $attributeOptionCode, array $data = []);
88+
89+
/**
90+
* Updates or creates several attribute options at once.
91+
*
92+
* @param string $attributeCode code of the attribute
93+
* @param array|StreamInterface $attributeOptions array or StreamInterface object containing data of the attribute options to create or update
94+
*
95+
* @throws HttpException
96+
*
97+
* @return \Traversable returns an iterable object, each entry corresponding to the response of the upserted attribute options
98+
*/
99+
public function upsertList($attributeCode, $attributeOptions);
88100
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Akeneo\Pim\ApiClient\tests\v2_1\Api\AttributeOption;
4+
5+
use Akeneo\Pim\ApiClient\tests\Common\Api\ApiTestCase;
6+
7+
class UpsertListAttributeOptionIntegration extends ApiTestCase
8+
{
9+
public function testUpsertListSuccessful()
10+
{
11+
$api = $this->createClient()->getAttributeOptionApi();
12+
$response = $api->upsertList('weather_conditions', [
13+
[
14+
'code' => 'hot',
15+
'attribute' => 'weather_conditions',
16+
'sort_order' => 34,
17+
'labels' => [
18+
'en_US' => 'Hot!',
19+
],
20+
],
21+
[
22+
'code' => 'cloudy',
23+
'attribute' => 'weather_conditions',
24+
'sort_order' => 35,
25+
'labels' => [
26+
'en_US' => 'Cloudy',
27+
],
28+
29+
],
30+
]);
31+
32+
$this->assertInstanceOf('\Iterator', $response);
33+
34+
$responseLines = iterator_to_array($response);
35+
$this->assertCount(2, $responseLines);
36+
37+
$this->assertSame([
38+
'line' => 1,
39+
'code' => 'hot',
40+
'status_code' => 204,
41+
], $responseLines[1]);
42+
43+
$this->assertSame([
44+
'line' => 2,
45+
'code' => 'cloudy',
46+
'status_code' => 201,
47+
], $responseLines[2]);
48+
}
49+
50+
public function testUpsertListFailed()
51+
{
52+
$api = $this->createClient()->getAttributeOptionApi();
53+
$response = $api->upsertList('weather_conditions', [
54+
[
55+
'attribute' => 'weather_conditions',
56+
'sort_order' => 34,
57+
'labels' => [
58+
'en_US' => 'Hot!',
59+
],
60+
],
61+
[
62+
'code' => 'cloudy!',
63+
'attribute' => 'weather_conditions',
64+
'sort_order' => 35,
65+
'labels' => [
66+
'en_US' => 'Cloudy',
67+
],
68+
69+
],
70+
]);
71+
72+
$this->assertInstanceOf('\Iterator', $response);
73+
74+
$responseLines = iterator_to_array($response);
75+
$this->assertCount(2, $responseLines);
76+
77+
$this->assertSame([
78+
'line' => 1,
79+
'status_code' => 422,
80+
'message' => 'Code is missing.',
81+
], $responseLines[1]);
82+
83+
$this->assertSame([
84+
'line' => 2,
85+
'code' => 'cloudy!',
86+
'status_code' => 422,
87+
'message' => 'Validation failed.',
88+
'errors' => [[
89+
'property' => 'code',
90+
'message' => 'Option code may contain only letters, numbers and underscores'
91+
]]
92+
], $responseLines[2]);
93+
}
94+
}

0 commit comments

Comments
 (0)