From 033354d8d29bc2b0470a42d95607e8222b3a1315 Mon Sep 17 00:00:00 2001 From: dominique durand Date: Tue, 6 Aug 2024 16:26:36 -0400 Subject: [PATCH] feat: change productid and taxclass as optional, delete validation for description in sc calls --- .../Xml/Product/GlobalProductFactory.php | 5 +- src/Model/Product/BaseProduct.php | 9 ++-- .../Product/Contract/ProductInterface.php | 2 +- src/Model/Product/GlobalProduct.php | 49 +++++++++++++++-- src/Model/Product/Product.php | 2 +- tests/Functional/GlobalProductManagerTest.php | 26 +++++++++ tests/Unit/Product/GlobalProductTest.php | 11 ++-- tests/Unit/Product/ProductTest.php | 20 ------- .../GlobalProductResponseEmptyAttr.xml | 54 +++++++++++++++++++ .../GlobalProductResponseMissingAttr.xml | 51 ++++++++++++++++++ 10 files changed, 186 insertions(+), 43 deletions(-) create mode 100644 tests/_schemas/Product/GlobalProductResponseEmptyAttr.xml create mode 100644 tests/_schemas/Product/GlobalProductResponseMissingAttr.xml diff --git a/src/Factory/Xml/Product/GlobalProductFactory.php b/src/Factory/Xml/Product/GlobalProductFactory.php index 25c6050..8ebf4ad 100644 --- a/src/Factory/Xml/Product/GlobalProductFactory.php +++ b/src/Factory/Xml/Product/GlobalProductFactory.php @@ -20,10 +20,7 @@ class GlobalProductFactory 'SellerSku', 'Name', 'PrimaryCategory', - 'Description', 'Brand', - 'ProductId', - 'TaxClass', 'ProductData', ]; @@ -47,7 +44,7 @@ public static function make(SimpleXMLElement $element): GlobalProduct $images = ImagesFactory::make($element->Images); } - $product = GlobalProduct::fromBasicData( + $product = GlobalProduct::fromMainData( (string) $element->SellerSku, (string) $element->Name, (string) $element->Variation ?? null, diff --git a/src/Model/Product/BaseProduct.php b/src/Model/Product/BaseProduct.php index 8f7238f..674ba86 100644 --- a/src/Model/Product/BaseProduct.php +++ b/src/Model/Product/BaseProduct.php @@ -69,7 +69,7 @@ abstract class BaseProduct implements JsonSerializable protected $brand; /** - * @var string + * @var string|null */ protected $productId; @@ -171,7 +171,7 @@ public function getBrand(): Brand return $this->brand; } - public function getProductId(): string + public function getProductId(): ?string { return $this->productId; } @@ -313,7 +313,7 @@ public function jsonSerialize(): stdClass return $serialized; } - protected static function ValidateArguments(string $sellerSku, string $name, string $description, string $productId): void + protected static function ValidateArguments(string $sellerSku, string $name, string $description): void { if (empty($sellerSku)) { throw new EmptyArgumentException('SellerSku'); @@ -326,8 +326,5 @@ protected static function ValidateArguments(string $sellerSku, string $name, str if (empty($description)) { throw new EmptyArgumentException('Description'); } - if (empty($productId)) { - throw new EmptyArgumentException('ProductId'); - } } } diff --git a/src/Model/Product/Contract/ProductInterface.php b/src/Model/Product/Contract/ProductInterface.php index 8c011e4..2c7c49c 100644 --- a/src/Model/Product/Contract/ProductInterface.php +++ b/src/Model/Product/Contract/ProductInterface.php @@ -35,7 +35,7 @@ public function getDescription(): string; public function getBrand(): Brand; - public function getProductId(): string; + public function getProductId(): ?string; public function getTaxClass(): ?string; diff --git a/src/Model/Product/GlobalProduct.php b/src/Model/Product/GlobalProduct.php index 509225d..3fcffe5 100644 --- a/src/Model/Product/GlobalProduct.php +++ b/src/Model/Product/GlobalProduct.php @@ -66,15 +66,50 @@ public static function fromBasicData( string $description, Brand $brand, BusinessUnits $businessUnits, - string $productId, + ?string $productId, ?string $taxClass, ProductData $productData, ?Images $images = null, ?string $qcStatus = null, ?int $contentScore = null ): self { - self::ValidateArguments($sellerSku, $name, $description, $productId); + self::ValidateArguments($sellerSku, $name, $description); + + return self::fromMainData( + $sellerSku, + $name, + $variation, + $primaryCategory, + $description, + $brand, + $businessUnits, + $productId, + $taxClass, + $productData, + $images, + $qcStatus, + $contentScore + ); + } + /** + * @return static + */ + public static function fromMainData( + string $sellerSku, + string $name, + ?string $variation, + Category $primaryCategory, + string $description, + Brand $brand, + BusinessUnits $businessUnits, + ?string $productId, + ?string $taxClass, + ProductData $productData, + ?Images $images = null, + ?string $qcStatus = null, + ?int $contentScore = null + ): self { $product = new static(); $product->setSellerSku($sellerSku); @@ -83,8 +118,6 @@ public static function fromBasicData( $product->setDescription($description); $product->setBrand($brand); $product->setBusinessUnits($businessUnits); - $product->setProductId($productId); - $product->setTaxClass($taxClass); $product->setProductData($productData); $categories = new Categories(); @@ -94,6 +127,14 @@ public static function fromBasicData( $product->setVariation($variation); } + if (!empty($productId)) { + $product->setProductId($productId); + } + + if (!empty($taxClass)) { + $product->setTaxClass($taxClass); + } + if (!empty($images)) { $product->attachImages($images); } diff --git a/src/Model/Product/Product.php b/src/Model/Product/Product.php index 75ef296..d008e73 100644 --- a/src/Model/Product/Product.php +++ b/src/Model/Product/Product.php @@ -83,7 +83,7 @@ public static function fromBasicData( ?Images $images = null, ?array $overrideAttributes = [] ): self { - self::ValidateArguments($sellerSku, $name, $description, $productId); + self::ValidateArguments($sellerSku, $name, $description); if ($price <= 0) { throw new InvalidDomainException('Price'); diff --git a/tests/Functional/GlobalProductManagerTest.php b/tests/Functional/GlobalProductManagerTest.php index 52cb08e..bebe773 100644 --- a/tests/Functional/GlobalProductManagerTest.php +++ b/tests/Functional/GlobalProductManagerTest.php @@ -198,6 +198,32 @@ public function testItReturnsACollectionOfProductsBySkuSellerList(): void $this->assertContainsOnlyInstancesOf(GlobalProduct::class, $result); } + public function testItReturnsACollectionOfProductsBySkuSellerListWithEmptyAttributesTaxclassProductIdAndDescription(): void + { + $sdkClient = $this->getSdkClient($this->getSchema('Product/GlobalProductResponseEmptyAttr.xml')); + + $skuSellerList = ['testsku-01']; + + $result = $sdkClient->globalProducts()->getProductsBySellerSku($skuSellerList); + + $this->assertIsArray($result); + $this->assertContainsOnlyInstancesOf(GlobalProduct::class, $result); + $this->assertNotEmpty($result); + } + + public function testItReturnsACollectionOfProductsBySkuSellerListWithMissingAttributesTaxclassProductIdAndDescription(): void + { + $sdkClient = $this->getSdkClient($this->getSchema('Product/GlobalProductResponseMissingAttr.xml')); + + $skuSellerList = ['testsku-01']; + + $result = $sdkClient->globalProducts()->getProductsBySellerSku($skuSellerList); + + $this->assertIsArray($result); + $this->assertContainsOnlyInstancesOf(GlobalProduct::class, $result); + $this->assertNotEmpty($result); + } + public function testItThrowsExceptionWithANullSkuSellerList(): void { $this->expectException(InvalidArgumentException::class); diff --git a/tests/Unit/Product/GlobalProductTest.php b/tests/Unit/Product/GlobalProductTest.php index 56bb4cb..cd2806c 100644 --- a/tests/Unit/Product/GlobalProductTest.php +++ b/tests/Unit/Product/GlobalProductTest.php @@ -123,8 +123,8 @@ public function testItCreatesAGlobalProductWithMandatoryParameters(): void $this->description, $this->brand, $this->businessUnits, - $this->productId, - $this->taxClass, + null, + null, $this->productData, $this->images, null, @@ -138,8 +138,8 @@ public function testItCreatesAGlobalProductWithMandatoryParameters(): void $this->assertEquals($product->getPrimaryCategory(), $this->primaryCategory); $this->assertEquals($product->getDescription(), $this->description); $this->assertEquals($product->getBrand(), $this->brand); - $this->assertEquals($product->getProductId(), $this->productId); - $this->assertEquals($product->getTaxClass(), $this->taxClass); + $this->assertEquals($product->getProductId(), null); + $this->assertEquals($product->getTaxClass(), null); $this->assertEquals($product->getProductData(), $this->productData); $this->assertEquals($product->getQcStatus(), null); $this->assertEquals($product->getContentScore(), $this->contentScore); @@ -339,9 +339,6 @@ public function invalidXmlStructure(): array ['SellerSku'], ['Name'], ['Brand'], - ['Description'], - ['TaxClass'], - ['ProductId'], ['PrimaryCategory'], ['ProductData'], ['BusinessUnit'], diff --git a/tests/Unit/Product/ProductTest.php b/tests/Unit/Product/ProductTest.php index 0aeb6bb..4421b75 100644 --- a/tests/Unit/Product/ProductTest.php +++ b/tests/Unit/Product/ProductTest.php @@ -459,26 +459,6 @@ public function testItThrowsExceptionWhenPriceIsNull(float $invalidPrice): void ); } - public function testItThrowsExceptionWhenProductIdIsNull(): void - { - $this->expectException(EmptyArgumentException::class); - - $this->expectExceptionMessage('The parameter ProductId should not be null.'); - - Product::fromBasicData( - $this->sellerSku, - $this->name, - $this->variation, - $this->primaryCategory, - $this->description, - $this->brand, - $this->price, - '', - $this->taxClass, - $this->productData - ); - } - /** * @dataProvider invalidXmlStructure */ diff --git a/tests/_schemas/Product/GlobalProductResponseEmptyAttr.xml b/tests/_schemas/Product/GlobalProductResponseEmptyAttr.xml new file mode 100644 index 0000000..c74d04a --- /dev/null +++ b/tests/_schemas/Product/GlobalProductResponseEmptyAttr.xml @@ -0,0 +1,54 @@ + + + + + GetProducts + Products + 2019-02-05T11:39:11-0300 + + + + + testsku-01 + + PE grabadora + Amarillo + Amarillo Claro + XS + PE240724 + + + + + 0 + + + 2 TOMATOES + Gafas de sol + rejected + + + Falabella + facl + 23100.00 + + + + 3000 + active + 0 + + + + Nuevo + 1 + 1 + 1 + 1 + Bebé niño + Antiparras Nieve + + + + + \ No newline at end of file diff --git a/tests/_schemas/Product/GlobalProductResponseMissingAttr.xml b/tests/_schemas/Product/GlobalProductResponseMissingAttr.xml new file mode 100644 index 0000000..c4fa0a1 --- /dev/null +++ b/tests/_schemas/Product/GlobalProductResponseMissingAttr.xml @@ -0,0 +1,51 @@ + + + + + GetProducts + Products + 2019-02-05T11:39:11-0300 + + + + + testsku-01 + + PE grabadora + Amarillo + Amarillo Claro + XS + PE240724 + + + + 0 + 2 TOMATOES + Gafas de sol + rejected + + + Falabella + facl + 23100.00 + + + + 3000 + active + 0 + + + + Nuevo + 1 + 1 + 1 + 1 + Bebé niño + Antiparras Nieve + + + + + \ No newline at end of file