diff --git a/src/DataTransferObjects/ProductAttribute.php b/src/DataTransferObjects/ProductAttribute.php new file mode 100644 index 0000000..599de98 --- /dev/null +++ b/src/DataTransferObjects/ProductAttribute.php @@ -0,0 +1,19 @@ +productId . "/relationships/" . $this->relationshipId; + } + + public function defaultBody(): array + { + return $this->payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $relationshipInfo) { + return RelationshipInformation::from($relationshipInfo); + }, $response->json('data')); + } +} diff --git a/src/Requests/CreateAssetRequest.php b/src/Requests/CreateAssetRequest.php new file mode 100644 index 0000000..34c237c --- /dev/null +++ b/src/Requests/CreateAssetRequest.php @@ -0,0 +1,38 @@ +payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $product) { + return Asset::from($product); + }, $response->json('data')); + } +} diff --git a/src/Requests/CreateProductAttributeRequest.php b/src/Requests/CreateProductAttributeRequest.php new file mode 100644 index 0000000..f3f7ed2 --- /dev/null +++ b/src/Requests/CreateProductAttributeRequest.php @@ -0,0 +1,38 @@ +payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $attribute) { + return ProductAttribute::from($attribute); + }, $response->json('data')); + } +} diff --git a/src/Requests/CreateProductCategoryRequest.php b/src/Requests/CreateProductCategoryRequest.php new file mode 100644 index 0000000..554c330 --- /dev/null +++ b/src/Requests/CreateProductCategoryRequest.php @@ -0,0 +1,38 @@ +payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $productCategory) { + return ProductCategory::from($productCategory); + }, $response->json('data')); + } +} diff --git a/src/Requests/CreateProductRequest.php b/src/Requests/CreateProductRequest.php index 3fb3aac..a57d2bf 100644 --- a/src/Requests/CreateProductRequest.php +++ b/src/Requests/CreateProductRequest.php @@ -2,9 +2,11 @@ namespace Esign\Plytix\Requests; +use Esign\Plytix\DataTransferObjects\Product; use Saloon\Contracts\Body\HasBody; use Saloon\Enums\Method; use Saloon\Http\Request; +use Saloon\Http\Response; use Saloon\Traits\Body\HasJsonBody; class CreateProductRequest extends Request implements HasBody @@ -26,4 +28,11 @@ public function defaultBody(): array { return $this->payload; } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $product) { + return Product::from($product); + }, $response->json('data')); + } } diff --git a/src/Requests/CreateProductSubcategoryRequest.php b/src/Requests/CreateProductSubcategoryRequest.php new file mode 100644 index 0000000..d960a67 --- /dev/null +++ b/src/Requests/CreateProductSubcategoryRequest.php @@ -0,0 +1,39 @@ +categoryId; + } + + public function defaultBody(): array + { + return $this->payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $productCategory) { + return ProductCategory::from($productCategory); + }, $response->json('data')); + } +} diff --git a/src/Requests/CreateRelationshipRequest.php b/src/Requests/CreateRelationshipRequest.php new file mode 100644 index 0000000..1eaadf8 --- /dev/null +++ b/src/Requests/CreateRelationshipRequest.php @@ -0,0 +1,38 @@ +payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $relationship) { + return Relationship::from($relationship); + }, $response->json('data')); + } +} diff --git a/src/Requests/LinkAssetToProductRequest.php b/src/Requests/LinkAssetToProductRequest.php new file mode 100644 index 0000000..d011156 --- /dev/null +++ b/src/Requests/LinkAssetToProductRequest.php @@ -0,0 +1,39 @@ +productId . "/assets"; + } + + public function defaultBody(): array + { + return $this->payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $asset) { + return Asset::from($asset); + }, $response->json('data')); + } +} diff --git a/src/Requests/ModifySelectedProductAttributesRequest.php b/src/Requests/ModifySelectedProductAttributesRequest.php new file mode 100644 index 0000000..b7ea9f2 --- /dev/null +++ b/src/Requests/ModifySelectedProductAttributesRequest.php @@ -0,0 +1,39 @@ +productId; + } + + public function defaultBody(): array + { + return $this->payload; + } + + public function createDtoFromResponse(Response $response): mixed + { + return array_map(function (array $product) { + return Product::from($product); + }, $response->json('data')); + } +} diff --git a/tests/Feature/Requests/AssignProductsToRelationshipRequestTest.php b/tests/Feature/Requests/AssignProductsToRelationshipRequestTest.php new file mode 100644 index 0000000..8b5c026 --- /dev/null +++ b/tests/Feature/Requests/AssignProductsToRelationshipRequestTest.php @@ -0,0 +1,35 @@ +send(new AssignProductsToRelationshipRequest('productid', 'relationshipid',[ + 'name' => '12345' + ])); + + $relationshipInformation = $response->dto()[0]; + + $mockClient->assertSent(AssignProductsToRelationshipRequest::class); + $this->assertEquals('5d8a50b547397aea2a603079', $relationshipInformation->relationshipId); + $this->assertEquals('contains', $relationshipInformation->relationshipLabel); + $this->assertIsArray($relationshipInformation->relatedProducts); + $this->assertCount(4, $relationshipInformation->relatedProducts); + $this->assertEquals(9, $relationshipInformation->relatedProducts[1]->quantity); + } +} diff --git a/tests/Feature/Requests/CreateAssetRequestTest.php b/tests/Feature/Requests/CreateAssetRequestTest.php new file mode 100644 index 0000000..39c9d44 --- /dev/null +++ b/tests/Feature/Requests/CreateAssetRequestTest.php @@ -0,0 +1,44 @@ +send(new CreateAssetRequest([ + 'filename' => 'kettle_blue.jpg', + 'url' => 'https://www.plytix.com/hubfs/Kitchen%20and%20Home%20Products/Enjoy%20Mug%20-%20Blue.jpg', + ])); + $asset = $response->dto()[0]; + + $mockClient->assertSent(CreateAssetRequest::class); + $this->assertEquals('5c4c6e10ca59f4061b800ffa', $asset->id); + $this->assertEquals(false, $asset->assigned); + $this->assertEquals('jpg', $asset->extension); + $this->assertEquals('kettle_blue.jpg', $asset->filename); + $this->assertEquals('image/jpeg', $asset->contentType); + $this->assertEquals(31885, $asset->fileSize); + $this->assertEquals('IMAGES', $asset->fileType); + $this->assertEquals(0, $asset->nCatalogs); + $this->assertEquals(0, $asset->nProducts); + $this->assertEquals(true, $asset->public); + $this->assertEquals('ACTIVE', $asset->status); + $this->assertEquals('https://files.plytix.com/api/v1.1/thumb/kettle_blue.jpg', $asset->thumbnail); + $this->assertEquals('https://files.plytix.com/api/v1.1/file/public_files/assets/kettle_blue.jpg', $asset->url); + } +} diff --git a/tests/Feature/Requests/CreateProductAttributeRequestTest.php b/tests/Feature/Requests/CreateProductAttributeRequestTest.php new file mode 100644 index 0000000..d0b693b --- /dev/null +++ b/tests/Feature/Requests/CreateProductAttributeRequestTest.php @@ -0,0 +1,37 @@ +send(new CreateProductAttributeRequest([ + 'name' => 'In stock', + 'type_class' => 'BooleanAttribute', + ])); + + $attribute = $response->dto()[0]; + + $mockClient->assertSent(CreateProductAttributeRequest::class); + $this->assertEquals('5d0b4ea525abd700016fc037', $attribute->id); + $this->assertEquals('in_stock', $attribute->label); + $this->assertEquals('In Stock', $attribute->name); + $this->assertEquals('BooleanAttribute', $attribute->typeClass); + $this->assertIsArray($attribute->groups); + $this->assertCount(0, $attribute->groups); + } +} diff --git a/tests/Feature/Requests/CreateProductCategoryRequestTest.php b/tests/Feature/Requests/CreateProductCategoryRequestTest.php new file mode 100644 index 0000000..d285729 --- /dev/null +++ b/tests/Feature/Requests/CreateProductCategoryRequestTest.php @@ -0,0 +1,39 @@ +send(new CreateProductCategoryRequest([ + 'name' => 'Kitchen Sinks' + ])); + + $category = $response->dto()[0]; + $mockClient->assertSent(CreateProductCategoryRequest::class); + $this->assertEquals('5cfa05273da03100019ba90e', $category->id); + $this->assertEquals('Kitchen Sinks', $category->name); + $this->assertEquals('5', $category->order); + $this->assertEquals('kitchen-sinks', $category->slug); + $this->assertEquals(0, $category->nChildren); + $this->assertIsArray($category->parentsIds); + $this->assertCount(0, $category->parentsIds); + $this->assertIsArray($category->path); + $this->assertCount(1, $category->path); + $this->assertEquals('Kitchen Sinks', $category->path[0]); + } +} diff --git a/tests/Feature/Requests/CreateProductRequestTest.php b/tests/Feature/Requests/CreateProductRequestTest.php index f538916..099ab2f 100644 --- a/tests/Feature/Requests/CreateProductRequestTest.php +++ b/tests/Feature/Requests/CreateProductRequestTest.php @@ -23,9 +23,21 @@ public function it_can_send_a_create_product_request() 'sku' => '12345', 'label' => 'Black Kettle', ])); + $product = $response->dto()[0]; $mockClient->assertSent(CreateProductRequest::class); - $this->assertEquals('12345', $response->json('data.0.sku')); - $this->assertEquals('Black Kettle', $response->json('data.0.label')); + $this->assertEquals('5c4ef3a9bedb5e000189befc', $product->id); + $this->assertEquals('12345', $product->sku); + $this->assertEquals('Black Kettle', $product->label); + $this->assertEquals('Completed', $product->status); + $this->assertEquals(0, $product->numVariations); + $this->assertIsArray($product->attributes); + $this->assertCount(2, $product->attributes); + $this->assertEquals(['Blue'], $product->attributes['color_create_a_multiselect_type_attribute']); + $this->assertIsArray($product->assets); + $this->assertCount(0, $product->assets); + $this->assertIsArray($product->categories); + $this->assertCount(3, $product->categories); + $this->assertEquals('Drinkware', $product->categories[1]->name); } } diff --git a/tests/Feature/Requests/CreateProductSubcategoryRequestTest.php b/tests/Feature/Requests/CreateProductSubcategoryRequestTest.php new file mode 100644 index 0000000..bb246d9 --- /dev/null +++ b/tests/Feature/Requests/CreateProductSubcategoryRequestTest.php @@ -0,0 +1,47 @@ +send(new CreateProductCategoryRequest([ + 'name' => 'Kitchen Sinks' + ]))->dto()[0]; + + $response = $plytix->send(new CreateProductSubcategoryRequest($category->id, [ + 'name' => 'Apron' + ])); + + $subcategory = $response->dto()[0]; + $mockClient->assertSent(CreateProductSubcategoryRequest::class); + + $this->assertEquals('5cfa0c1e3da03100019ba90f', $subcategory->id); + $this->assertEquals('Apron', $subcategory->name); + $this->assertEquals('1', $subcategory->order); + $this->assertEquals('apron', $subcategory->slug); + $this->assertEquals(0, $subcategory->nChildren); + $this->assertIsArray($subcategory->parentsIds); + $this->assertCount(1, $subcategory->parentsIds); + $this->assertEquals('5cfa05273da03100019ba90e', $subcategory->parentsIds[0]); + $this->assertIsArray($subcategory->path); + $this->assertCount(2, $subcategory->path); + $this->assertEquals('Kitchen Sinks', $subcategory->path[0]); + } +} diff --git a/tests/Feature/Requests/CreateRelationshipRequestTest.php b/tests/Feature/Requests/CreateRelationshipRequestTest.php new file mode 100644 index 0000000..109a1cb --- /dev/null +++ b/tests/Feature/Requests/CreateRelationshipRequestTest.php @@ -0,0 +1,33 @@ +send(new CreateRelationshipRequest([ + 'name' => '12345' + ])); + + $relationship = $response->dto()[0]; + + $mockClient->assertSent(CreateRelationshipRequest::class); + $this->assertEquals('12345', $relationship->name); + $this->assertEquals('12345', $relationship->label); + $this->assertEquals('665d88e1ffa3da952052dbf0', $relationship->id); + } +} diff --git a/tests/Feature/Requests/LinkAssetToProductRequestTest.php b/tests/Feature/Requests/LinkAssetToProductRequestTest.php new file mode 100644 index 0000000..bae852f --- /dev/null +++ b/tests/Feature/Requests/LinkAssetToProductRequestTest.php @@ -0,0 +1,31 @@ +send(new LinkAssetToProductRequest('665db0db744c81d899bb315e', [ + 'id' => '5c4ed8002f0985001e233275', + 'attribute_label' => 'thumbnail' + ])); + + $asset = $response->dto()[0]; + $mockClient->assertSent(LinkAssetToProductRequest::class); + $this->assertEquals('5c4ed8002f0985001e233275', $asset->id); + } +} diff --git a/tests/Feature/Requests/ModifySelectedProductAttributesRequestTest.php b/tests/Feature/Requests/ModifySelectedProductAttributesRequestTest.php new file mode 100644 index 0000000..fd41cee --- /dev/null +++ b/tests/Feature/Requests/ModifySelectedProductAttributesRequestTest.php @@ -0,0 +1,50 @@ +send(new ModifySelectedProductAttributesRequest('5c4ed8002f0985001e233275', [ + 'description' => '12345' + ])); + + $product = $response->dto()[0]; + + $mockClient->assertSent(ModifySelectedProductAttributesRequest::class); + $this->assertEquals('5c4ed8002f0985001e233275', $product->id); + $this->assertEquals('Sample - 001', $product->sku); + $this->assertEquals('Product ~ 1 - patch example', $product->label); + $this->assertEquals('Completed', $product->status); + $this->assertEquals(0, $product->numVariations); + + $this->assertIsArray($product->attributes); + $this->assertCount(2, $product->attributes); + + $this->assertIsArray($product->categories); + $this->assertCount(1, $product->categories); + + $this->assertIsArray($product->assets); + $this->assertCount(1, $product->assets); + + $this->assertEquals(["Blue"], $product->attributes["color_create_a_multiselect_type_attribute"]); + $this->assertEquals('Kettles & Teapots', $product->categories[0]->name); + $this->assertEquals('Kettle handle - White.png', $product->assets[0]->filename); + } +} diff --git a/tests/Fixtures/Saloon/assign-products-to-relationship.json b/tests/Fixtures/Saloon/assign-products-to-relationship.json new file mode 100644 index 0000000..1e75f32 --- /dev/null +++ b/tests/Fixtures/Saloon/assign-products-to-relationship.json @@ -0,0 +1,30 @@ +{ + "data": [ + { + "related_products": [ + { + "last_modified": "2019-09-24T17:28:07.329000+00:00", + "product_id": "5d0baf07d4bf5513d190245f", + "quantity": 1 + }, + { + "last_modified": "2019-09-24T17:28:07.329000+00:00", + "product_id": "5d0baf07d4bf5513d1902460", + "quantity": 9 + }, + { + "last_modified": "2019-09-24T17:28:07.329000+00:00", + "product_id": "5d0baf07d4bf5513d1902461", + "quantity": 6 + }, + { + "last_modified": "2019-09-24T17:28:07.329000+00:00", + "product_id": "5d0baf07d4bf5513d1902462", + "quantity": 1 + } + ], + "relationship_id": "5d8a50b547397aea2a603079", + "relationship_label": "contains" + } + ] + } \ No newline at end of file diff --git a/tests/Fixtures/Saloon/create-asset.json b/tests/Fixtures/Saloon/create-asset.json new file mode 100644 index 0000000..2ba6b41 --- /dev/null +++ b/tests/Fixtures/Saloon/create-asset.json @@ -0,0 +1,23 @@ +{ + "data": [ + { + "assigned": false, + "categories": [], + "content_type": "image/jpeg", + "created": "2019-01-26T14:26:24.826240+00:00", + "extension": "jpg", + "file_modified": "2019-01-26T14:26:24.826306", + "file_size": 31885, + "file_type": "IMAGES", + "filename": "kettle_blue.jpg", + "id": "5c4c6e10ca59f4061b800ffa", + "modified": "2019-01-26T14:26:24.827123+00:00", + "n_catalogs": 0, + "n_products": 0, + "public": true, + "status": "ACTIVE", + "thumbnail": "https://files.plytix.com/api/v1.1/thumb/kettle_blue.jpg", + "url": "https://files.plytix.com/api/v1.1/file/public_files/assets/kettle_blue.jpg" + } + ] + } \ No newline at end of file diff --git a/tests/Fixtures/Saloon/create-product-attribute.json b/tests/Fixtures/Saloon/create-product-attribute.json new file mode 100644 index 0000000..04b76a9 --- /dev/null +++ b/tests/Fixtures/Saloon/create-product-attribute.json @@ -0,0 +1,11 @@ +{ + "data": [ + { + "id": "5d0b4ea525abd700016fc037", + "label": "in_stock", + "name": "In Stock", + "groups": [], + "type_class": "BooleanAttribute" + } + ] + } \ No newline at end of file diff --git a/tests/Fixtures/Saloon/create-product-category.json b/tests/Fixtures/Saloon/create-product-category.json new file mode 100644 index 0000000..da81394 --- /dev/null +++ b/tests/Fixtures/Saloon/create-product-category.json @@ -0,0 +1,16 @@ +{ + "data": [ + { + "id": "5cfa05273da03100019ba90e", + "modified": "2019-06-07T06:33:11.965893+00:00", + "n_children": 0, + "name": "Kitchen Sinks", + "order": "5", + "parents_ids": [], + "path": [ + "Kitchen Sinks" + ], + "slug": "kitchen-sinks" + } + ] +} \ No newline at end of file diff --git a/tests/Fixtures/Saloon/create-product-subcategory.json b/tests/Fixtures/Saloon/create-product-subcategory.json new file mode 100644 index 0000000..9f26460 --- /dev/null +++ b/tests/Fixtures/Saloon/create-product-subcategory.json @@ -0,0 +1,19 @@ +{ + "data": [ + { + "id": "5cfa0c1e3da03100019ba90f", + "modified": "2019-06-07T07:02:54.817539+00:00", + "n_children": 0, + "name": "Apron", + "order": "1", + "parents_ids": [ + "5cfa05273da03100019ba90e" + ], + "path": [ + "Kitchen Sinks", + "Apron" + ], + "slug": "apron" + } + ] + } \ No newline at end of file diff --git a/tests/Fixtures/Saloon/create-relationship.json b/tests/Fixtures/Saloon/create-relationship.json new file mode 100644 index 0000000..8e29101 --- /dev/null +++ b/tests/Fixtures/Saloon/create-relationship.json @@ -0,0 +1,12 @@ +{ + "data":[ + { + "created":"2024-06-03T09:12:01.208270+00:00", + "created_at":"2024-06-03T09:12:01.208270+00:00", + "id":"665d88e1ffa3da952052dbf0", + "label":"12345", + "modified":"2024-06-03T09:12:01.210459+00:00", + "name":"12345" + } + ] + } \ No newline at end of file diff --git a/tests/Fixtures/Saloon/link-asset-to-product.json b/tests/Fixtures/Saloon/link-asset-to-product.json new file mode 100644 index 0000000..eef7821 --- /dev/null +++ b/tests/Fixtures/Saloon/link-asset-to-product.json @@ -0,0 +1,7 @@ +{ + "data": [ + { + "id": "5c4ed8002f0985001e233275" + } + ] +} \ No newline at end of file diff --git a/tests/Fixtures/Saloon/update-product-set-attribute.json b/tests/Fixtures/Saloon/update-product-set-attribute.json new file mode 100644 index 0000000..4f8544d --- /dev/null +++ b/tests/Fixtures/Saloon/update-product-set-attribute.json @@ -0,0 +1,44 @@ +{ + "data": [ + { + "_parent_id": null, + "assets": [ + { + "filename": "Kettle handle - White.png", + "id": "5c4ed8002f0985001e233276", + "thumbnail": "https://files.plytix.com/api/v1.1/thumb/public_files/pim/assets/d1/6d/8e/5b/5b8e6dd17f7f46000c7e9629/images/00/d8/4e/5c/5c4ed8002f0985001e233276/Kettle handle - White.png", + "url": "https://files.plytix.com/api/v1.1/file/public_files/pim/assets/d1/6d/8e/5b/5b8e6dd17f7f46000c7e9629/images/00/d8/4e/5c/5c4ed8002f0985001e233276/Kettle handle - White.png" + } + ], + "attributes": { + "color_create_a_multiselect_type_attribute": [ + "Blue" + ], + "description_create_a_rich_text_type_attribute": "Try making the description a Rich Text attribute so that you can format the text however you'd like." + }, + "categories": [ + { + "id": "5c4ed7f62f0985001c233279", + "name": "Kettles & Teapots", + "path": [ + "Sample Categories", + "Kettles & Teapots" + ] + } + ], + "created": "2019-01-28T10:22:56.817000+00:00", + "id": "5c4ed8002f0985001e233275", + "label": "Product ~ 1 - patch example", + "modified": "2019-01-28T10:24:44.792000+00:00", + "num_variations": 0, + "sku": "Sample - 001", + "status": "Completed", + "thumbnail": { + "filename": "assets/d1/6d/8e/5b/5b8e6dd17f7f46000c7e9629/images/00/d8/4e/5c/5c4ed8002f0985001e233276/Kettle handle - White.png", + "id": "5c4ed8002f0985001e233276", + "thumbnail": "https://files.plytix.com/api/v1.1/thumb/public_files/pim/assets/d1/6d/8e/5b/5b8e6dd17f7f46000c7e9629/images/00/d8/4e/5c/5c4ed8002f0985001e233276/Kettle handle - White.png", + "url": "https://files.plytix.com/api/v1.1/thumb/public_files/pim/assets/d1/6d/8e/5b/5b8e6dd17f7f46000c7e9629/images/00/d8/4e/5c/5c4ed8002f0985001e233276/Kettle handle - White.png" + } + } + ] + } \ No newline at end of file