Skip to content

Commit

Permalink
Merge pull request #191 from LinioIT/gsc-master-dev
Browse files Browse the repository at this point in the history
feat: change productid and taxclass as optional, delete description validation when is getting products from gsc
  • Loading branch information
ddurandg authored Aug 14, 2024
2 parents 1702d1d + 9d17135 commit ca3a8c9
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 43 deletions.
5 changes: 1 addition & 4 deletions src/Factory/Xml/Product/GlobalProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class GlobalProductFactory
'SellerSku',
'Name',
'PrimaryCategory',
'Description',
'Brand',
'ProductId',
'TaxClass',
'ProductData',
];

Expand All @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions src/Model/Product/BaseProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class BaseProduct implements JsonSerializable
protected $brand;

/**
* @var string
* @var string|null
*/
protected $productId;

Expand Down Expand Up @@ -171,7 +171,7 @@ public function getBrand(): Brand
return $this->brand;
}

public function getProductId(): string
public function getProductId(): ?string
{
return $this->productId;
}
Expand Down Expand Up @@ -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');
Expand All @@ -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');
}
}
}
2 changes: 1 addition & 1 deletion src/Model/Product/Contract/ProductInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getDescription(): string;

public function getBrand(): Brand;

public function getProductId(): string;
public function getProductId(): ?string;

public function getTaxClass(): ?string;

Expand Down
49 changes: 45 additions & 4 deletions src/Model/Product/GlobalProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Product/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/GlobalProductManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions tests/Unit/Product/GlobalProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -339,9 +339,6 @@ public function invalidXmlStructure(): array
['SellerSku'],
['Name'],
['Brand'],
['Description'],
['TaxClass'],
['ProductId'],
['PrimaryCategory'],
['ProductData'],
['BusinessUnit'],
Expand Down
20 changes: 0 additions & 20 deletions tests/Unit/Product/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
54 changes: 54 additions & 0 deletions tests/_schemas/Product/GlobalProductResponseEmptyAttr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<SuccessResponse>
<Head>
<RequestId />
<RequestAction>GetProducts</RequestAction>
<ResponseType>Products</ResponseType>
<Timestamp>2019-02-05T11:39:11-0300</Timestamp>
</Head>
<Body>
<Products>
<Product>
<SellerSku>testsku-01</SellerSku>
<ShopSku />
<Name>PE grabadora</Name>
<ColorBasico>Amarillo</ColorBasico>
<Color>Amarillo Claro</Color>
<Talla>XS</Talla>
<ParentSku>PE240724</ParentSku>
<ProductId />
<Url />
<MainImage />
<Images />
<ContentScore>0</ContentScore>
<Description />
<TaxClass />
<Brand>2 TOMATOES</Brand>
<PrimaryCategory>Gafas de sol</PrimaryCategory>
<QCStatus>rejected</QCStatus>
<BusinessUnits>
<BusinessUnit>
<BusinessUnit>Falabella</BusinessUnit>
<OperatorCode>facl</OperatorCode>
<Price>23100.00</Price>
<SpecialPrice />
<SpecialFromDate />
<SpecialToDate />
<Stock>3000</Stock>
<Status>active</Status>
<IsPublished>0</IsPublished>
</BusinessUnit>
</BusinessUnits>
<ProductData>
<ConditionType>Nuevo</ConditionType>
<PackageLength>1</PackageLength>
<PackageHeight>1</PackageHeight>
<PackageWidth>1</PackageWidth>
<PackageWeight>1</PackageWeight>
<GeneroDeVestuario>Bebé niño</GeneroDeVestuario>
<Uso>Antiparras Nieve</Uso>
</ProductData>
</Product>
</Products>
</Body>
</SuccessResponse>
51 changes: 51 additions & 0 deletions tests/_schemas/Product/GlobalProductResponseMissingAttr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<SuccessResponse>
<Head>
<RequestId />
<RequestAction>GetProducts</RequestAction>
<ResponseType>Products</ResponseType>
<Timestamp>2019-02-05T11:39:11-0300</Timestamp>
</Head>
<Body>
<Products>
<Product>
<SellerSku>testsku-01</SellerSku>
<ShopSku />
<Name>PE grabadora</Name>
<ColorBasico>Amarillo</ColorBasico>
<Color>Amarillo Claro</Color>
<Talla>XS</Talla>
<ParentSku>PE240724</ParentSku>
<Url />
<MainImage />
<Images />
<ContentScore>0</ContentScore>
<Brand>2 TOMATOES</Brand>
<PrimaryCategory>Gafas de sol</PrimaryCategory>
<QCStatus>rejected</QCStatus>
<BusinessUnits>
<BusinessUnit>
<BusinessUnit>Falabella</BusinessUnit>
<OperatorCode>facl</OperatorCode>
<Price>23100.00</Price>
<SpecialPrice />
<SpecialFromDate />
<SpecialToDate />
<Stock>3000</Stock>
<Status>active</Status>
<IsPublished>0</IsPublished>
</BusinessUnit>
</BusinessUnits>
<ProductData>
<ConditionType>Nuevo</ConditionType>
<PackageLength>1</PackageLength>
<PackageHeight>1</PackageHeight>
<PackageWidth>1</PackageWidth>
<PackageWeight>1</PackageWeight>
<GeneroDeVestuario>Bebé niño</GeneroDeVestuario>
<Uso>Antiparras Nieve</Uso>
</ProductData>
</Product>
</Products>
</Body>
</SuccessResponse>

0 comments on commit ca3a8c9

Please sign in to comment.