From d711babfba19e05a1e52127d36463790336bf902 Mon Sep 17 00:00:00 2001 From: Laurent Petard Date: Fri, 18 Jan 2019 11:42:05 +0100 Subject: [PATCH 1/2] Rename method upsertResourceList --- spec/Api/AssociationTypeApiSpec.php | 2 +- spec/Api/AttributeApiSpec.php | 2 +- spec/Api/AttributeGroupApiSpec.php | 2 +- spec/Api/AttributeOptionApiSpec.php | 2 +- spec/Api/CategoryApiSpec.php | 2 +- spec/Api/ChannelApiSpec.php | 2 +- spec/Api/FamilyApiSpec.php | 2 +- spec/Api/FamilyVariantApiSpec.php | 2 +- spec/Api/ProductApiSpec.php | 2 +- spec/Api/ProductModelApiSpec.php | 2 +- spec/Client/ResourceClientSpec.php | 6 +++--- src/Api/AssociationTypeApi.php | 2 +- src/Api/AttributeApi.php | 2 +- src/Api/AttributeGroupApi.php | 2 +- src/Api/AttributeOptionApi.php | 2 +- src/Api/CategoryApi.php | 2 +- src/Api/ChannelApi.php | 2 +- src/Api/FamilyApi.php | 2 +- src/Api/FamilyVariantApi.php | 2 +- src/Api/ProductApi.php | 2 +- src/Api/ProductModelApi.php | 2 +- src/Client/ResourceClient.php | 2 +- src/Client/ResourceClientInterface.php | 4 ++-- 23 files changed, 26 insertions(+), 26 deletions(-) diff --git a/spec/Api/AssociationTypeApiSpec.php b/spec/Api/AssociationTypeApiSpec.php index 65f154d6..0199df1f 100644 --- a/spec/Api/AssociationTypeApiSpec.php +++ b/spec/Api/AssociationTypeApiSpec.php @@ -149,7 +149,7 @@ function it_upserts_an_association_type($resourceClient) function it_upserts_a_list_of_association_types($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( AssociationTypeApi::ASSOCIATION_TYPES_URI, [], [ diff --git a/spec/Api/AttributeApiSpec.php b/spec/Api/AttributeApiSpec.php index cd7c7fe0..2957b10b 100644 --- a/spec/Api/AttributeApiSpec.php +++ b/spec/Api/AttributeApiSpec.php @@ -143,7 +143,7 @@ function it_upserts_an_attribute($resourceClient) function it_upserts_a_list_of_attributes($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( AttributeApi::ATTRIBUTES_URI, [], [ diff --git a/spec/Api/AttributeGroupApiSpec.php b/spec/Api/AttributeGroupApiSpec.php index 47517489..fceca441 100644 --- a/spec/Api/AttributeGroupApiSpec.php +++ b/spec/Api/AttributeGroupApiSpec.php @@ -141,7 +141,7 @@ function it_upserts_an_attribute_group($resourceClient) function it_upserts_a_list_of_attribute_groups($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( AttributeGroupApi::ATTRIBUTE_GROUPS_URI, [], [ diff --git a/spec/Api/AttributeOptionApiSpec.php b/spec/Api/AttributeOptionApiSpec.php index 881d187c..c48e00cd 100644 --- a/spec/Api/AttributeOptionApiSpec.php +++ b/spec/Api/AttributeOptionApiSpec.php @@ -153,7 +153,7 @@ function it_upserts_an_attribute_option($resourceClient) function it_upserts_a_list_of_attribute_options($resourceClient, UpsertResourceListResponse $response) { - $resourceClient->upsertResourceList(AttributeOptionApi::ATTRIBUTE_OPTIONS_URI, ['foo'], [ + $resourceClient->upsertStreamResourceList(AttributeOptionApi::ATTRIBUTE_OPTIONS_URI, ['foo'], [ ['code' => 'bar', 'attribute' => 'foo', 'sort_order' => 42], ['code' => 'fighters', 'attribute' => 'foo', 'sort_order' => 43] ])->willReturn($response); diff --git a/spec/Api/CategoryApiSpec.php b/spec/Api/CategoryApiSpec.php index bca886df..f8117b96 100644 --- a/spec/Api/CategoryApiSpec.php +++ b/spec/Api/CategoryApiSpec.php @@ -139,7 +139,7 @@ function it_upserts_a_category($resourceClient) function it_upserts_a_list_of_categories($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( CategoryApi::CATEGORIES_URI, [], [ diff --git a/spec/Api/ChannelApiSpec.php b/spec/Api/ChannelApiSpec.php index e8e7c3c4..2ee1911d 100644 --- a/spec/Api/ChannelApiSpec.php +++ b/spec/Api/ChannelApiSpec.php @@ -143,7 +143,7 @@ function it_upserts_a_channel($resourceClient) function it_upserts_a_list_of_channel($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( ChannelApi::CHANNELS_URI, [], [ diff --git a/spec/Api/FamilyApiSpec.php b/spec/Api/FamilyApiSpec.php index f6ca401d..1b74e374 100644 --- a/spec/Api/FamilyApiSpec.php +++ b/spec/Api/FamilyApiSpec.php @@ -142,7 +142,7 @@ function it_upserts_a_family($resourceClient) function it_upserts_a_list_of_families($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( FamilyApi::FAMILIES_URI, [], [ diff --git a/spec/Api/FamilyVariantApiSpec.php b/spec/Api/FamilyVariantApiSpec.php index 46b688d3..23e0bb9c 100644 --- a/spec/Api/FamilyVariantApiSpec.php +++ b/spec/Api/FamilyVariantApiSpec.php @@ -210,7 +210,7 @@ function it_upserts_a_list_of_family_variants($resourceClient, UpsertResourceLis ]; $resourceClient - ->upsertResourceList(FamilyVariantApi::FAMILY_VARIANTS_URI, ['boots'], $data) + ->upsertStreamResourceList(FamilyVariantApi::FAMILY_VARIANTS_URI, ['boots'], $data) ->willReturn($response); $this->upsertList('boots', $data)->shouldReturn($response); diff --git a/spec/Api/ProductApiSpec.php b/spec/Api/ProductApiSpec.php index 34c23a6f..25a2e8b4 100644 --- a/spec/Api/ProductApiSpec.php +++ b/spec/Api/ProductApiSpec.php @@ -153,7 +153,7 @@ function it_deletes_a_product($resourceClient) function it_upserts_a_list_of_products($resourceClient, UpsertResourceListResponse $response) { $resourceClient - ->upsertResourceList( + ->upsertStreamResourceList( ProductApi::PRODUCTS_URI, [], [ diff --git a/spec/Api/ProductModelApiSpec.php b/spec/Api/ProductModelApiSpec.php index e8c157f4..52f46f46 100644 --- a/spec/Api/ProductModelApiSpec.php +++ b/spec/Api/ProductModelApiSpec.php @@ -151,7 +151,7 @@ function it_upserts_a_list_of_product_models($resourceClient, UpsertResourceList ] ]; - $resourceClient->upsertResourceList(ProductModelApi::PRODUCT_MODELS_URI, [], $data)->willReturn($response); + $resourceClient->upsertStreamResourceList(ProductModelApi::PRODUCT_MODELS_URI, [], $data)->willReturn($response); $this->upsertList($data)->shouldReturn($response); } diff --git a/spec/Client/ResourceClientSpec.php b/spec/Client/ResourceClientSpec.php index 2396aee5..21bf06a5 100644 --- a/spec/Client/ResourceClientSpec.php +++ b/spec/Client/ResourceClientSpec.php @@ -231,7 +231,7 @@ function it_upserts_a_list_of_resources_from_an_array( $responseFactory->create($responseBodyStream)->willReturn($listResponse); $this - ->upsertResourceList( + ->upsertStreamResourceList( 'api/rest/v1/categories', [], [ @@ -271,7 +271,7 @@ function it_upserts_a_list_of_resources_from_an_stream( $responseFactory->create($responseBodyStream)->willReturn($listResponse); $this - ->upsertResourceList('api/rest/v1/categories', [], $resourcesStream) + ->upsertStreamResourceList('api/rest/v1/categories', [], $resourcesStream) ->shouldReturn($listResponse); } @@ -293,7 +293,7 @@ function it_throws_an_exception_if_resources_is_not_an_array_and_not_a_stream_wh { $this ->shouldthrow(new InvalidArgumentException('The parameter "resources" must be an array or an instance of StreamInterface.')) - ->during('upsertResourceList', ['api/rest/v1/categories', [], 'foo']); + ->during('upsertStreamResourceList', ['api/rest/v1/categories', [], 'foo']); } function it_creates_a_multipart_resource( diff --git a/src/Api/AssociationTypeApi.php b/src/Api/AssociationTypeApi.php index 188ef67f..8bbeb76b 100644 --- a/src/Api/AssociationTypeApi.php +++ b/src/Api/AssociationTypeApi.php @@ -95,6 +95,6 @@ public function upsert($code, array $data = []) */ public function upsertList($associationTypes) { - return $this->resourceClient->upsertResourceList(static::ASSOCIATION_TYPES_URI, [], $associationTypes); + return $this->resourceClient->upsertStreamResourceList(static::ASSOCIATION_TYPES_URI, [], $associationTypes); } } diff --git a/src/Api/AttributeApi.php b/src/Api/AttributeApi.php index b304cc2b..d7b0368f 100644 --- a/src/Api/AttributeApi.php +++ b/src/Api/AttributeApi.php @@ -98,6 +98,6 @@ public function upsert($code, array $data = []) */ public function upsertList($attributes) { - return $this->resourceClient->upsertResourceList(static::ATTRIBUTES_URI, [], $attributes); + return $this->resourceClient->upsertStreamResourceList(static::ATTRIBUTES_URI, [], $attributes); } } diff --git a/src/Api/AttributeGroupApi.php b/src/Api/AttributeGroupApi.php index b620c6e8..882a9095 100644 --- a/src/Api/AttributeGroupApi.php +++ b/src/Api/AttributeGroupApi.php @@ -100,6 +100,6 @@ public function upsert($code, array $data = []) */ public function upsertList($attributeGroups) { - return $this->resourceClient->upsertResourceList(static::ATTRIBUTE_GROUPS_URI, [], $attributeGroups); + return $this->resourceClient->upsertStreamResourceList(static::ATTRIBUTE_GROUPS_URI, [], $attributeGroups); } } diff --git a/src/Api/AttributeOptionApi.php b/src/Api/AttributeOptionApi.php index 6fd47e95..98b5680b 100644 --- a/src/Api/AttributeOptionApi.php +++ b/src/Api/AttributeOptionApi.php @@ -104,6 +104,6 @@ public function upsert($attributeCode, $attributeOptionCode, array $data = []) */ public function upsertList($attributeCode, $attributeOptions) { - return $this->resourceClient->upsertResourceList(static::ATTRIBUTE_OPTIONS_URI, [$attributeCode], $attributeOptions); + return $this->resourceClient->upsertStreamResourceList(static::ATTRIBUTE_OPTIONS_URI, [$attributeCode], $attributeOptions); } } diff --git a/src/Api/CategoryApi.php b/src/Api/CategoryApi.php index addb0f9e..257a76ca 100644 --- a/src/Api/CategoryApi.php +++ b/src/Api/CategoryApi.php @@ -98,6 +98,6 @@ public function upsert($code, array $data = []) */ public function upsertList($categories) { - return $this->resourceClient->upsertResourceList(static::CATEGORIES_URI, [], $categories); + return $this->resourceClient->upsertStreamResourceList(static::CATEGORIES_URI, [], $categories); } } diff --git a/src/Api/ChannelApi.php b/src/Api/ChannelApi.php index 2858e3bc..d260c122 100644 --- a/src/Api/ChannelApi.php +++ b/src/Api/ChannelApi.php @@ -98,6 +98,6 @@ public function upsert($code, array $data = []) */ public function upsertList($channels) { - return $this->resourceClient->upsertResourceList(static::CHANNELS_URI, [], $channels); + return $this->resourceClient->upsertStreamResourceList(static::CHANNELS_URI, [], $channels); } } diff --git a/src/Api/FamilyApi.php b/src/Api/FamilyApi.php index 6811307a..aa42ea7f 100644 --- a/src/Api/FamilyApi.php +++ b/src/Api/FamilyApi.php @@ -98,6 +98,6 @@ public function upsert($code, array $data = []) */ public function upsertList($families) { - return $this->resourceClient->upsertResourceList(static::FAMILIES_URI, [], $families); + return $this->resourceClient->upsertStreamResourceList(static::FAMILIES_URI, [], $families); } } diff --git a/src/Api/FamilyVariantApi.php b/src/Api/FamilyVariantApi.php index bbb6bc9f..8a9db818 100644 --- a/src/Api/FamilyVariantApi.php +++ b/src/Api/FamilyVariantApi.php @@ -114,6 +114,6 @@ public function all($familyCode, $pageSize = 10, array $queryParameters = []) */ public function upsertList($familyCode, $familyVariants) { - return $this->resourceClient->upsertResourceList(static::FAMILY_VARIANTS_URI, [$familyCode], $familyVariants); + return $this->resourceClient->upsertStreamResourceList(static::FAMILY_VARIANTS_URI, [$familyCode], $familyVariants); } } diff --git a/src/Api/ProductApi.php b/src/Api/ProductApi.php index 511e910c..29c1c81a 100644 --- a/src/Api/ProductApi.php +++ b/src/Api/ProductApi.php @@ -108,6 +108,6 @@ public function delete($code) */ public function upsertList($products) { - return $this->resourceClient->upsertResourceList(static::PRODUCTS_URI, [], $products); + return $this->resourceClient->upsertStreamResourceList(static::PRODUCTS_URI, [], $products); } } diff --git a/src/Api/ProductModelApi.php b/src/Api/ProductModelApi.php index 657a9756..8a659f2c 100644 --- a/src/Api/ProductModelApi.php +++ b/src/Api/ProductModelApi.php @@ -120,6 +120,6 @@ public function all($pageSize = 10, array $queryParameters = []) */ public function upsertList($productModels) { - return $this->resourceClient->upsertResourceList(static::PRODUCT_MODELS_URI, [], $productModels); + return $this->resourceClient->upsertStreamResourceList(static::PRODUCT_MODELS_URI, [], $productModels); } } diff --git a/src/Client/ResourceClient.php b/src/Client/ResourceClient.php index 8895660b..338679f9 100644 --- a/src/Client/ResourceClient.php +++ b/src/Client/ResourceClient.php @@ -150,7 +150,7 @@ public function upsertResource($uri, array $uriParameters = [], array $body = [] /** * {@inheritdoc} */ - public function upsertResourceList($uri, array $uriParameters = [], $resources = []) + public function upsertStreamResourceList($uri, array $uriParameters = [], $resources = []) { if (!is_array($resources) && !$resources instanceof StreamInterface) { throw new InvalidArgumentException('The parameter "resources" must be an array or an instance of StreamInterface.'); diff --git a/src/Client/ResourceClientInterface.php b/src/Client/ResourceClientInterface.php index f695f181..8ca2ab5f 100644 --- a/src/Client/ResourceClientInterface.php +++ b/src/Client/ResourceClientInterface.php @@ -90,7 +90,7 @@ public function createMultipartResource($uri, array $uriParameters = [], array $ public function upsertResource($uri, array $uriParameters = [], array $body = []); /** - * Updates or creates several resources. + * Updates or creates several resources using a stream. * * @param string $uri URI of the resource * @param array $uriParameters URI parameters of the resource @@ -102,7 +102,7 @@ public function upsertResource($uri, array $uriParameters = [], array $body = [] * * @return \Traversable returns an iterable object, each entry corresponding to the response of the upserted resource */ - public function upsertResourceList($uri, array $uriParameters = [], $resources = []); + public function upsertStreamResourceList($uri, array $uriParameters = [], $resources = []); /** * Deletes a resource. From dd9ac0d74c450830519f0b0cd7f1ff544c08bc38 Mon Sep 17 00:00:00 2001 From: Laurent Petard Date: Fri, 18 Jan 2019 14:34:03 +0100 Subject: [PATCH 2/2] Add resource client method to upsert resources using a JSON string --- spec/Client/ResourceClientSpec.php | 69 +++++++++++++++++++++++++- src/Client/ResourceClient.php | 24 +++++++++ src/Client/ResourceClientInterface.php | 18 ++++++- 3 files changed, 108 insertions(+), 3 deletions(-) diff --git a/spec/Client/ResourceClientSpec.php b/spec/Client/ResourceClientSpec.php index 21bf06a5..25205693 100644 --- a/spec/Client/ResourceClientSpec.php +++ b/spec/Client/ResourceClientSpec.php @@ -197,7 +197,7 @@ function it_upserts_a_resource( ->shouldReturn(201); } - function it_upserts_a_list_of_resources_from_an_array( + function it_upserts_a_list_of_streamed_resources_from_an_array( $httpClient, $uriGenerator, $responseFactory, @@ -244,7 +244,7 @@ function it_upserts_a_list_of_resources_from_an_array( ->shouldReturn($listResponse); } - function it_upserts_a_list_of_resources_from_an_stream( + function it_upserts_a_list_of_streamed_resources_from_an_stream( $httpClient, $uriGenerator, $responseFactory, @@ -275,6 +275,71 @@ function it_upserts_a_list_of_resources_from_an_stream( ->shouldReturn($listResponse); } + function it_upserts_a_list_of_json_resources( + HttpClient $httpClient, + UriGeneratorInterface $uriGenerator, + ResponseInterface $response, + StreamInterface $responseBody + ) { + $uri = 'http://akeneo.com/api/rest/v1/reference-entities/designer/records'; + + $uriGenerator + ->generate('api/rest/v1/reference-entities/%s/records', ['designer']) + ->willReturn($uri); + + $body = <<sendRequest('PATCH', $uri, ['Content-Type' => 'application/json'], $body) + ->willReturn($response); + + $response + ->getBody() + ->willReturn($responseBody); + + $upsertResponse = <<getContents() + ->willReturn($upsertResponse); + + $this->upsertJsonResourceList('api/rest/v1/reference-entities/%s/records', ['designer'], [ + ['code' => 'designer_1'], + ['code' => 'designer_2'], + ['code' => 'designer_3'], + ])->shouldReturn([ + [ + 'code' => 'designer_1', + 'status_code' =>204 + ], + [ + 'code' => 'designer_2', + 'status_code' =>204 + ], + [ + 'code' => 'designer_3', + 'status_code' =>201 + ] + ]); + } + function it_throws_an_exception_if_limit_is_defined_in_additional_parameters_to_get_resources() { $this diff --git a/src/Client/ResourceClient.php b/src/Client/ResourceClient.php index 338679f9..06727f39 100644 --- a/src/Client/ResourceClient.php +++ b/src/Client/ResourceClient.php @@ -1,8 +1,11 @@ upsertListResponseFactory->create($response->getBody()); } + /** + * {@inheritdoc} + */ + public function upsertJsonResourceList(string $uri, array $uriParameters = [], array $resources = []): array + { + $uri = $this->uriGenerator->generate($uri, $uriParameters); + $response = $this->httpClient->sendRequest( + 'PATCH', + $uri, + ['Content-Type' => 'application/json'], + json_encode($resources) + ); + + $response = json_decode($response->getBody()->getContents(), true); + if (!is_array($response)) { + throw new RuntimeException('The server response is not a valid JSON'); + } + + return $response; + } + /** * {@inheritdoc} */ diff --git a/src/Client/ResourceClientInterface.php b/src/Client/ResourceClientInterface.php index 8ca2ab5f..82d3c3d7 100644 --- a/src/Client/ResourceClientInterface.php +++ b/src/Client/ResourceClientInterface.php @@ -1,5 +1,7 @@