From a495c4d18e466bfa874934554793dde524ef5567 Mon Sep 17 00:00:00 2001 From: Stephan Weikert Date: Thu, 7 Apr 2022 11:39:27 +0200 Subject: [PATCH] Add customerproductvisibilities endpoint --- .../create_customerproductvisibility.php | 38 ++ .../delete_customerproductvisibility.php | 10 + .../get_customerproductvisibility.php | 15 + .../page_customerproductvisibility.php | 16 + .../update_customerproductvisibility.php | 24 ++ .../CustomerproductvisibilityEndpoint.php | 92 +++++ src/OroApiClient.php | 7 + src/Resources/Customerproductvisibility.php | 64 ++++ .../CustomerproductvisibilityCollection.php | 22 ++ .../CustomerproductvisibilityEndpointTest.php | 353 ++++++++++++++++++ 10 files changed, 641 insertions(+) create mode 100644 examples/customerproductvisibilities/create_customerproductvisibility.php create mode 100644 examples/customerproductvisibilities/delete_customerproductvisibility.php create mode 100644 examples/customerproductvisibilities/get_customerproductvisibility.php create mode 100644 examples/customerproductvisibilities/page_customerproductvisibility.php create mode 100644 examples/customerproductvisibilities/update_customerproductvisibility.php create mode 100644 src/Endpoints/CustomerproductvisibilityEndpoint.php create mode 100644 src/Resources/Customerproductvisibility.php create mode 100644 src/Resources/CustomerproductvisibilityCollection.php create mode 100644 tests/Digitalprint/Oro/API/Endpoints/CustomerproductvisibilityEndpointTest.php diff --git a/examples/customerproductvisibilities/create_customerproductvisibility.php b/examples/customerproductvisibilities/create_customerproductvisibility.php new file mode 100644 index 0000000..9ea72e0 --- /dev/null +++ b/examples/customerproductvisibilities/create_customerproductvisibility.php @@ -0,0 +1,38 @@ +customerproductvisibilities->create([ + 'data' => [ + 'type' => 'customerproductvisibilities', + 'attributes' => [ + 'visibility' => 'visible', + ], + 'relationships' => [ + 'product' => [ + 'data' => [ + 'type' => 'products', + 'id' => '1', + ], + ], + 'customer' => [ + 'data' => [ + 'type' => 'customers', + 'id' => '1', + ], + ], + 'website' => [ + 'data' => [ + 'type' => 'websites', + 'id' => '1', + ], + ], + ], + ], + ]); + + echo "

Customerproductvisibilty created: $customerproductvisibility->id

"; +} catch (\Digitalprint\Oro\Api\Exceptions\ApiException $e) { + echo "API call failed: " . $e->getMessage(); +} diff --git a/examples/customerproductvisibilities/delete_customerproductvisibility.php b/examples/customerproductvisibilities/delete_customerproductvisibility.php new file mode 100644 index 0000000..283a132 --- /dev/null +++ b/examples/customerproductvisibilities/delete_customerproductvisibility.php @@ -0,0 +1,10 @@ +customerproductvisibilities->get('1-1-1')->delete(); + echo "

Customerproductvisibility deleted

"; +} catch (\Digitalprint\Oro\Api\Exceptions\ApiException $e) { + echo "API call failed: " . $e->getMessage(); +} diff --git a/examples/customerproductvisibilities/get_customerproductvisibility.php b/examples/customerproductvisibilities/get_customerproductvisibility.php new file mode 100644 index 0000000..ffa00a9 --- /dev/null +++ b/examples/customerproductvisibilities/get_customerproductvisibility.php @@ -0,0 +1,15 @@ +customerproductvisibilities->get('1-1-1'); + + echo "$customerproductvisibility->type $customerproductvisibility->id
"; + foreach ($customerproductvisibility->attributes as $key => $val) { + echo "$key: $val
"; + } + +} catch (\Digitalprint\Oro\Api\Exceptions\ApiException $e) { + echo "API call failed: {$e->getMessage()}"; +} diff --git a/examples/customerproductvisibilities/page_customerproductvisibility.php b/examples/customerproductvisibilities/page_customerproductvisibility.php new file mode 100644 index 0000000..98d6191 --- /dev/null +++ b/examples/customerproductvisibilities/page_customerproductvisibility.php @@ -0,0 +1,16 @@ +customerproductvisibilities->page(1, 10); + + foreach ($customerproductvisibilities as $customerproductvisibility) { + echo "$customerproductvisibility->type $customerproductvisibility->id
"; + foreach ($customerproductvisibility->attributes as $key => $val) { + echo "$key: $val
"; + } + } +} catch (\Digitalprint\Oro\Api\Exceptions\ApiException $e) { + echo "API call failed: {$e->getMessage()}"; +} diff --git a/examples/customerproductvisibilities/update_customerproductvisibility.php b/examples/customerproductvisibilities/update_customerproductvisibility.php new file mode 100644 index 0000000..df276a3 --- /dev/null +++ b/examples/customerproductvisibilities/update_customerproductvisibility.php @@ -0,0 +1,24 @@ +customerproductvisibilities->get('1-1-1'); + + $res = $address->update([ + 'data' => [ + 'meta' => [ + 'update' => true, + ], + 'type' => 'customerproductvisibilities', + 'id' => '1-1-1', + 'attributes' => [ + 'visibility' => 'visible', + ], + ], + ]); + + echo "

Customerproductvisibility updated: $res->id

"; +} catch (\Digitalprint\Oro\Api\Exceptions\ApiException $e) { + echo "API call failed: " . $e->getMessage(); +} diff --git a/src/Endpoints/CustomerproductvisibilityEndpoint.php b/src/Endpoints/CustomerproductvisibilityEndpoint.php new file mode 100644 index 0000000..1c997bf --- /dev/null +++ b/src/Endpoints/CustomerproductvisibilityEndpoint.php @@ -0,0 +1,92 @@ +client, $included); + } + + /** + * @param stdClass $links + * @param array $included + * @return CustomerproductvisibilityCollection + */ + protected function getResourceCollectionObject(stdClass $links, array $included = []): CustomerproductvisibilityCollection + { + return new CustomerproductvisibilityCollection($this->client, $links, $included); + } + + /** + * @param array|null $data + * @return Customerproductvisibility + * @throws ApiException + * @throws JsonException + */ + public function create(array $data = null): Customerproductvisibility + { + return $this->rest_create($data); + } + + /** + * @param string $productpriceId + * @param array $filter + * @return Customerproductvisibility + * @throws ApiException + */ + public function get(string $customerproductvisibilityId, array $filter = []): Customerproductvisibility + { + return $this->rest_read($customerproductvisibilityId, $filter); + } + + /** + * @param string|null $number + * @param string|null $size + * @param array $filter + * + * @return CustomerproductvisibilityCollection + * @throws ApiException + */ + public function page(string $number = null, string $size = null, array $filter = []): CustomerproductvisibilityCollection + { + return $this->rest_list($number, $size, $filter); + } + + /** + * @param array $data + * @return Customerproductvisibility + * @throws ApiException + * @throws JsonException + */ + public function update(array $data = []): Customerproductvisibility + { + return $this->rest_update($data); + } + + /** + * @param array $filter + * @return Customerproductvisibility|null + * @throws ApiException + */ + public function delete(array $filter = []): ?Customerproductvisibility + { + return $this->rest_delete($filter); + } +} diff --git a/src/OroApiClient.php b/src/OroApiClient.php index adad097..15acd71 100644 --- a/src/OroApiClient.php +++ b/src/OroApiClient.php @@ -6,6 +6,7 @@ use Digitalprint\Oro\Api\Endpoints\AddresstypeEndpoint; use Digitalprint\Oro\Api\Endpoints\AsyncoperationEndpoint; use Digitalprint\Oro\Api\Endpoints\AuthorizationEndpoint; +use Digitalprint\Oro\Api\Endpoints\CustomerproductvisibilityEndpoint; use Digitalprint\Oro\Api\Endpoints\ProductdescriptionEndpoint; use Digitalprint\Oro\Api\Endpoints\ProductEndpoint; use Digitalprint\Oro\Api\Endpoints\ProductimageEndpoint; @@ -94,6 +95,11 @@ class OroApiClient */ public ProductpriceEndpoint $productprices; + /** + * @var CustomerproductvisibilityEndpoint + */ + public CustomerproductvisibilityEndpoint $customerproductvisibilities; + /** * @var UserEndpoint */ @@ -145,6 +151,7 @@ public function initializeEndpoints(): void $this->addresses = new AddressEndpoint($this); $this->addresstypes = new AddresstypeEndpoint($this); $this->asyncoperations = new AsyncoperationEndpoint($this); + $this->customerproductvisibilities = new CustomerproductvisibilityEndpoint($this); $this->products = new ProductEndpoint($this); $this->productdescriptions = new ProductdescriptionEndpoint($this); $this->productimages = new ProductimageEndpoint($this); diff --git a/src/Resources/Customerproductvisibility.php b/src/Resources/Customerproductvisibility.php new file mode 100644 index 0000000..50363a4 --- /dev/null +++ b/src/Resources/Customerproductvisibility.php @@ -0,0 +1,64 @@ +client->performHttpCallToFullUrl( + OroApiClient::HTTP_GET, + $this->links->self + ); + + return ResourceFactory::createFromApiResult( + $result->data, + new Customerproductvisibility($this->client) + ); + } + + /** + * @param array $body + * @return BaseResource|null + * @throws ApiException + * @throws JsonException + */ + public function update(array $body = []): ?BaseResource + { + $result = $this->client->performHttpCallToFullUrl( + OroApiClient::HTTP_PATCH, + $this->links->self, + json_encode($body, JSON_THROW_ON_ERROR) + ); + + if ($result === null) { + return null; + } + + return ResourceFactory::createFromApiResult($result->data, new Customerproductvisibility($this->client)); + } + + /** + * @return null + * @throws ApiException + */ + public function delete() + { + if (! isset($this->links->self)) { + return $this; + } + + return $this->client->performHttpCallToFullUrl( + OroApiClient::HTTP_DELETE, + $this->links->self + ); + } +} diff --git a/src/Resources/CustomerproductvisibilityCollection.php b/src/Resources/CustomerproductvisibilityCollection.php new file mode 100644 index 0000000..f503a47 --- /dev/null +++ b/src/Resources/CustomerproductvisibilityCollection.php @@ -0,0 +1,22 @@ +client); + } +} diff --git a/tests/Digitalprint/Oro/API/Endpoints/CustomerproductvisibilityEndpointTest.php b/tests/Digitalprint/Oro/API/Endpoints/CustomerproductvisibilityEndpointTest.php new file mode 100644 index 0000000..08d40c8 --- /dev/null +++ b/tests/Digitalprint/Oro/API/Endpoints/CustomerproductvisibilityEndpointTest.php @@ -0,0 +1,353 @@ +mockApiCall( + new Request( + "POST", + "/admin/api/customerproductvisibilities", + [], + '{ + "data": { + "type": "customerproductvisibilities", + "attributes": { + "visibility": "visible" + }, + "relationships": { + "product": { + "data": { + "type": "products", + "id": "1" + } + }, + "customer": { + "data": { + "type": "customers", + "id": "1" + } + }, + "website": { + "data": { + "type": "websites", + "id": "1" + } + } + } + } + }' + ), + new Response( + 201, + [], + $this->getCustomerproductvisibilityResponse() + ) + ); + + $customerproductvisibility = $this->apiClient->customerproductvisibilities->create([ + 'data' => [ + 'type' => 'customerproductvisibilities', + 'attributes' => [ + 'visibility' => 'visible', + ], + 'relationships' => [ + 'product' => [ + 'data' => [ + 'type' => 'products', + 'id' => '1', + ], + ], + 'customer' => [ + 'data' => [ + 'type' => 'customers', + 'id' => '1', + ], + ], + 'website' => [ + 'data' => [ + 'type' => 'websites', + 'id' => '1', + ], + ], + ], + ], + ]); + + $this->assertCustomerproductvisibility($customerproductvisibility); + } + + /** + * @return void + * @throws ApiException + * @throws IncompatiblePlatform + * @throws JsonException + * @throws UnrecognizedClientException + */ + public function testUpdateCustomerproductvisibility(): void + { + $this->mockApiCall( + new Request( + "PATCH", + "/admin/api/customerproductvisibilities", + [], + '{ + "data": { + "meta": { + "update": true + }, + "type": "customerproductvisibilities", + "id": "1-1-1", + "attributes": { + "visibility": "visible" + } + } + }' + ), + new Response( + 200, + [], + $this->getCustomerproductvisibilityResponse() + ) + ); + + $customerproductvisibility = $this->apiClient->customerproductvisibilities->update([ + 'data' => [ + 'meta' => [ + 'update' => true, + ], + 'type' => 'customerproductvisibilities', + 'id' => "1-1-1", + 'attributes' => [ + 'visibility' => 'visible', + ], + ], + ]); + + $this->assertCustomerproductvisibility($customerproductvisibility); + } + + /** + * @return void + * @throws IncompatiblePlatform + * @throws UnrecognizedClientException + * @throws ApiException + */ + public function testGetCustomerproductvisibility(): void + { + $this->mockApiCall( + new Request( + "GET", + "/admin/api/customerproductvisibilities/1-1-1", + [], + '' + ), + new Response( + 200, + [], + $this->getCustomerproductvisibilityResponse() + ) + ); + + $customerproductvisibility = $this->apiClient->customerproductvisibilities->get('1-1-1'); + + $this->assertCustomerproductvisibility($customerproductvisibility); + } + + /** + * @return void + * @throws ApiException + * @throws IncompatiblePlatform + * @throws UnrecognizedClientException + */ + public function testListCustomerproductvisibility(): void + { + $this->mockApiCall( + new Request( + "GET", + "/admin/api/customerproductvisibilities", + [], + '' + ), + new Response( + 200, + [], + $this->getCustomerproductvisibilityCollectionResponse() + ) + ); + + $customerproductvisibilities = $this->apiClient->customerproductvisibilities->page(); + + $this->assertInstanceOf(CustomerproductvisibilityCollection::class, $customerproductvisibilities); + + foreach ($customerproductvisibilities as $customerproductvisibility) { + $this->assertInstanceOf(Customerproductvisibility::class, $customerproductvisibility); + $this->assertEquals("customerproductvisibilities", $customerproductvisibility->type); + $this->assertNotEmpty($customerproductvisibility->attributes); + } + + $linksObject = (object)[ + "self" => "https://myoroproxy.local/admin/api/customerproductvisibilities", + "first" => "https://myoroproxy.local/admin/api/customerproductvisibilities?page%5Bsize%5D=1&sort=id", + "prev" => "https://myoroproxy.local/admin/api/customerproductvisibilities?page%5Bsize%5D=1&sort=id", + "next" => "https://myoroproxy.local/admin/api/customerproductvisibilities?page%5Bnumber%5D=2&page%5Bsize%5D=1&sort=id", + ]; + $this->assertEquals($linksObject, $customerproductvisibilities->links); + } + + /** + * @return void + * @throws ApiException + * @throws IncompatiblePlatform + * @throws UnrecognizedClientException + */ + public function testDeleteCustomerproductvisibility(): void + { + $this->mockApiCall( + new Request( + "DELETE", + "/admin/api/customerproductvisibilities?filter%5Bproduct%5D=1", + [], + '{}' + ), + new Response(204) + ); + + $result = $this->apiClient->customerproductvisibilities->delete(['product' => '1']); + $this->assertNull($result); + } + + /** + * @param $customerproductvisibility + * @return void + */ + protected function assertCustomerproductvisibility($customerproductvisibility): void + { + $this->assertInstanceOf(Customerproductvisibility::class, $customerproductvisibility); + + $this->assertEquals("customerproductvisibilities", $customerproductvisibility->type); + $this->assertEquals("1-1-1", $customerproductvisibility->id); + + $attributesObject = (object)[ + "visibility" => "visible", + ]; + $this->assertEquals($attributesObject, $customerproductvisibility->attributes); + + $relationshipsOwnerObject = (object)[ + "data" => (object)[ + "type" => "customers", + "id" => "1", + ], + ]; + $this->assertEquals($relationshipsOwnerObject, $customerproductvisibility->relationships->customer); + } + + /** + * @return string + */ + protected function getCustomerproductvisibilityResponse(): string + { + return '{ + "data": { + "type": "customerproductvisibilities", + "id": "1-1-1", + "links": { + "self": "https://myoroproxy.local/admin/api/customerproductvisibilities/1-1-1" + }, + "attributes": { + "visibility": "visible" + }, + "relationships": { + "product": { + "data": { + "type": "products", + "id": "1" + } + }, + "customer": { + "data": { + "type": "customers", + "id": "1" + } + }, + "website": { + "data": { + "type": "websites", + "id": "1" + } + } + } + }, + "links": { + "self": "https://myoroproxy.local/admin/api/customerproductvisibilities" + } + }'; + } + + /** + * @return string + */ + protected function getCustomerproductvisibilityCollectionResponse(): string + { + return '{ + "data": [ + { + "type": "customerproductvisibilities", + "id": "1-1-1", + "links": { + "self": "https://myoroproxy.local/admin/customerproductvisibilities/1-1-1" + }, + "attributes": { + "visibility": "visible" + }, + "relationships": { + "product": { + "data": { + "type": "products", + "id": "1" + } + }, + "customer": { + "data": { + "type": "customers", + "id": "1" + } + }, + "website": { + "data": { + "type": "websites", + "id": "1" + } + } + } + } + ], + "links": { + "self": "https://myoroproxy.local/admin/api/customerproductvisibilities", + "first": "https://myoroproxy.local/admin/api/customerproductvisibilities?page%5Bsize%5D=1&sort=id", + "prev": "https://myoroproxy.local/admin/api/customerproductvisibilities?page%5Bsize%5D=1&sort=id", + "next": "https://myoroproxy.local/admin/api/customerproductvisibilities?page%5Bnumber%5D=2&page%5Bsize%5D=1&sort=id" + } + }'; + } + +}