Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Adapters/PrestaShopAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function transformProduct(array $product): array
{
$result = [
'id' => $this->getRequiredField($product, 'remoteId'),
'sku' => $this->getRequiredField($product, 'sku'),
'sku' => (string) ($product['sku'] ?? ''),
'price' => $this->getRequiredField($product, 'price'),
'basePrice' => $this->getRequiredField($product, 'basePrice'),
'priceTaxExcluded' => $this->getRequiredField($product, 'priceTaxExcluded'),
Expand Down
5 changes: 1 addition & 4 deletions src/Adapters/PrestaShopAdapterV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function transform(array $prestaShopData): array
public function transformProduct(array $product): Product
{
$id = $this->getRequiredField($product, 'remoteId');
$sku = $this->getRequiredField($product, 'sku');
$sku = (string) ($product['sku'] ?? '');

$pricing = new ProductPricing(
$this->extractPrice($product, 'price'),
Expand Down Expand Up @@ -180,9 +180,6 @@ public function transformVariant(array $variant, string $locale): ProductVariant
}

$sku = (string) ($variant['sku'] ?? '');
if ($sku === '') {
throw new ValidationException("Variant 'sku' is required");
}

$pricing = new ProductPricing(
$this->extractPrice($variant, 'price'),
Expand Down
4 changes: 2 additions & 2 deletions src/Adapters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ $result = $adapter->transform($prestaShopData);
| PrestaShop Field | BradSearch Field | Notes |
| ---------------------- | -------------------------------- | ----------------------- |
| `remoteId` | `id` | Required |
| `sku` | `sku` | Required |
| `sku` | `sku` | Optional, `''` if none |
| `localizedNames` | `name` (+ locale suffixes) | Multi-locale support |
| `brand.localizedNames` | `brand` (+ locale suffixes) | Multi-locale support |
| `productUrl` | `productUrl` (+ locale suffixes) | Multi-locale support |
Expand Down Expand Up @@ -135,7 +135,7 @@ PrestaShop variants are transformed to match BradSearch requirements:

The adapter validates input data and throws `ValidationException` for:

- Missing required fields (`remoteId`, `sku`)
- Missing required fields (`remoteId`)
- Invalid data structure
- Missing product array

Expand Down
15 changes: 0 additions & 15 deletions src/V2/ValueObjects/BulkOperations/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function __construct(
public array $additionalFields = []
) {
$this->validateId($id);
$this->validateSku($sku);
}

/**
Expand Down Expand Up @@ -239,18 +238,4 @@ private function validateId(string $id): void
);
}
}

/**
* @throws InvalidArgumentException
*/
private function validateSku(string $sku): void
{
if (trim($sku) === '') {
throw new InvalidArgumentException(
'The product SKU cannot be empty.',
'sku',
$sku
);
}
}
}
12 changes: 2 additions & 10 deletions src/V2/ValueObjects/BulkOperations/ProductBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
final class ProductBuilder
{
private ?string $id = null;
private ?string $sku = null;
private string $sku = '';
private ?ProductPricing $pricing = null;
private ?ImageUrl $imageUrl = null;
private ?bool $inStock = null;
Expand Down Expand Up @@ -149,14 +149,6 @@ public function build(): Product
);
}

if ($this->sku === null) {
throw new InvalidArgumentException(
'Product SKU is required.',
'sku',
null
);
}

if ($this->pricing === null) {
throw new InvalidArgumentException(
'Product pricing is required.',
Expand Down Expand Up @@ -190,7 +182,7 @@ public function build(): Product
public function reset(): self
{
$this->id = null;
$this->sku = null;
$this->sku = '';
$this->pricing = null;
$this->imageUrl = null;
$this->inStock = null;
Expand Down
15 changes: 0 additions & 15 deletions src/V2/ValueObjects/BulkOperations/ProductVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
public array $attrs = []
) {
$this->validateId($id);
$this->validateSku($sku);
$this->validateProductUrl($productUrl);
}

Expand Down Expand Up @@ -173,20 +172,6 @@ private function validateId(string $id): void
}
}

/**
* @throws InvalidArgumentException
*/
private function validateSku(string $sku): void
{
if (trim($sku) === '') {
throw new InvalidArgumentException(
'The variant SKU cannot be empty.',
'sku',
$sku
);
}
}

/**
* @throws InvalidArgumentException
*/
Expand Down
71 changes: 65 additions & 6 deletions tests/Adapters/PrestaShopAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,20 +456,79 @@ public function testTransformProductWithMissingSku(): void
'products' => [
[
'remoteId' => '1807',
// Missing sku
'price' => '99.99',
'basePrice' => '99.99',
'priceTaxExcluded' => '82.64',
'basePriceTaxExcluded' => '82.64',
'localizedNames' => [
'en-US' => 'Test Product'
]
],
'categories' => [],
'variants' => []
]
]
];

$result = $this->adapter->transform($prestaShopData);

$this->assertCount(0, $result['products']);
$this->assertCount(1, $result['errors']);
$this->assertEquals('transformation_error', $result['errors'][0]['type']);
$this->assertEquals("Required field 'sku' is missing from PrestaShop data", $result['errors'][0]['message']);
$this->assertCount(0, $result['errors']);
$product = $this->getProductFromResult($result);
$this->assertEquals('1807', $product['id']);
$this->assertSame('', $product['sku']);
}

public function testTransformProductWithNullSku(): void
{
$prestaShopData = [
'products' => [
[
'remoteId' => '1807',
'sku' => null,
'price' => '99.99',
'basePrice' => '99.99',
'priceTaxExcluded' => '82.64',
'basePriceTaxExcluded' => '82.64',
'localizedNames' => [
'en-US' => 'Test Product'
],
'categories' => [],
'variants' => []
]
]
];

$result = $this->adapter->transform($prestaShopData);

$this->assertCount(0, $result['errors']);
$product = $this->getProductFromResult($result);
$this->assertSame('', $product['sku']);
}

public function testTransformProductWithEmptySku(): void
{
$prestaShopData = [
'products' => [
[
'remoteId' => '1807',
'sku' => '',
'price' => '99.99',
'basePrice' => '99.99',
'priceTaxExcluded' => '82.64',
'basePriceTaxExcluded' => '82.64',
'localizedNames' => [
'en-US' => 'Test Product'
],
'categories' => [],
'variants' => []
]
]
];

$result = $this->adapter->transform($prestaShopData);

$this->assertCount(0, $result['errors']);
$product = $this->getProductFromResult($result);
$this->assertSame('', $product['sku']);
}

public function testTransformVariantWithoutRemoteId(): void
Expand Down
83 changes: 83 additions & 0 deletions tests/Adapters/PrestaShopAdapterV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,63 @@ public function testTransformVariantMissingRemoteId(): void
$this->adapter->transformVariant($variant, 'en-US');
}

public function testTransformProductWithNullSku(): void
{
$product = $this->getMinimalProductData('1807', 'SKU-123');
$product['sku'] = null;

$result = $this->adapter->transformProduct($product);

$this->assertEquals('1807', $result->id);
$this->assertSame('', $result->sku);
$this->assertSame('', $result->jsonSerialize()['sku']);
}

public function testTransformProductWithEmptySku(): void
{
$product = $this->getMinimalProductData('1807', '');

$result = $this->adapter->transformProduct($product);

$this->assertEquals('1807', $result->id);
$this->assertSame('', $result->sku);
$this->assertSame('', $result->jsonSerialize()['sku']);
}

public function testTransformProductWithMissingSku(): void
{
$product = $this->getMinimalProductData('1807', 'SKU-123');
unset($product['sku']);

$result = $this->adapter->transform(['products' => [$product]]);

$this->assertCount(0, $result['errors']);
$this->assertCount(1, $result['products']);
$this->assertSame('', $result['products'][0]->sku);
}

public function testTransformVariantWithEmptySku(): void
{
$variant = $this->getMinimalVariantData();
$variant['sku'] = '';

$result = $this->adapter->transformVariant($variant, 'en-US');

$this->assertEquals('12345', $result->id);
$this->assertSame('', $result->sku);
}

public function testTransformVariantWithMissingSku(): void
{
$variant = $this->getMinimalVariantData();
unset($variant['sku']);

$result = $this->adapter->transformVariant($variant, 'en-US');

$this->assertSame('', $result->sku);
$this->assertSame('', $result->jsonSerialize()['sku']);
}

public function testTransformProductWithMissingRequiredFields(): void
{
$prestaShopData = [
Expand Down Expand Up @@ -1143,6 +1200,32 @@ private function getMinimalValidProduct(): array
];
}

/**
* Helper method to get minimal variant data.
*
* @return array<string, mixed>
*/
private function getMinimalVariantData(): array
{
return [
'remoteId' => '12345',
'sku' => 'VARIANT-SKU',
'price' => 29.99,
'basePrice' => 39.99,
'priceTaxExcluded' => 24.79,
'basePriceTaxExcluded' => 33.05,
'productUrl' => [
'localizedValues' => [
'en-US' => 'http://example.com/variant',
],
],
'imageUrl' => [
'small' => 'http://example.com/small.jpg',
'medium' => 'http://example.com/medium.jpg',
],
];
}

/**
* Helper method to get minimal product data.
*
Expand Down
10 changes: 4 additions & 6 deletions tests/V2/ValueObjects/BulkOperations/ProductBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,16 @@ public function testThrowsExceptionForMissingId(): void
->build();
}

public function testThrowsExceptionForMissingSku(): void
public function testBuildWithoutSkuDefaultsToEmptyString(): void
{
$builder = new ProductBuilder();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Product SKU is required.');

$builder
$product = $builder
->id(self::PRODUCT_ID)
->pricing($this->createPricing())
->imageUrl($this->createImageUrl())
->build();

$this->assertSame('', $product->sku);
}

public function testThrowsExceptionForMissingPricing(): void
Expand Down
Loading
Loading