diff --git a/src/Pdk/Product/Repository/PsPdkProductRepository.php b/src/Pdk/Product/Repository/PsPdkProductRepository.php index 7c896bdd..10d87977 100644 --- a/src/Pdk/Product/Repository/PsPdkProductRepository.php +++ b/src/Pdk/Product/Repository/PsPdkProductRepository.php @@ -85,7 +85,7 @@ public function getProduct($identifier): PdkProduct return new PdkProduct([ 'externalIdentifier' => $psProduct->id, 'name' => $translate($psProduct->name ?? []), - 'weight' => $this->weightService->convertToGrams($psProduct->weight ?? 0), + 'weight' => $this->weightService->convertToGrams((float) ($psProduct->weight ?? 0)), 'settings' => $this->getProductSettings($identifier), 'isDeliverable' => $this->isDeliverable($psProduct), 'price' => [ diff --git a/tests/Unit/Pdk/Product/Repository/PsPdkProductRepositoryTest.php b/tests/Unit/Pdk/Product/Repository/PsPdkProductRepositoryTest.php new file mode 100644 index 00000000..6ee8a20f --- /dev/null +++ b/tests/Unit/Pdk/Product/Repository/PsPdkProductRepositoryTest.php @@ -0,0 +1,43 @@ +store(); + + $pdkProduct = $productRepository->getProduct($psProduct->id); + + assertMatchesJsonSnapshot(json_encode($pdkProduct->toArrayWithoutNull(), JSON_THROW_ON_ERROR)); +})->with([ + 'simple product' => function () { + return psFactory(Product::class) + ->withPrice(1000) + ->withWeight(1); + }, + 'product with weight as string' => function () { + return psFactory(Product::class)->withWeight('234'); + }, + 'unavailable virtual product' => function () { + return psFactory(Product::class) + ->withIsAvailableForOrder(false) + ->withIsVirtual(true); + }, +]); diff --git a/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_product_with_weight_as_string__1.json b/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_product_with_weight_as_string__1.json new file mode 100644 index 00000000..786ecdaf --- /dev/null +++ b/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_product_with_weight_as_string__1.json @@ -0,0 +1,30 @@ +{ + "externalIdentifier": "1", + "isDeliverable": false, + "name": "Test product", + "price": { + "amount": 0, + "currency": "EUR" + }, + "weight": 234, + "length": 0, + "width": 0, + "height": 0, + "settings": { + "id": "product", + "countryOfOrigin": -1, + "customsCode": -1, + "disableDeliveryOptions": -1, + "dropOffDelay": -1, + "exportAgeCheck": -1, + "exportHideSender": -1, + "exportInsurance": -1, + "exportLargeFormat": -1, + "exportOnlyRecipient": -1, + "exportReturn": -1, + "exportSignature": -1, + "fitInDigitalStamp": -1, + "fitInMailbox": -1, + "packageType": -1 + } +} diff --git a/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_simple_product__1.json b/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_simple_product__1.json new file mode 100644 index 00000000..b60dcd82 --- /dev/null +++ b/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_simple_product__1.json @@ -0,0 +1,30 @@ +{ + "externalIdentifier": "1", + "isDeliverable": false, + "name": "Test product", + "price": { + "amount": 100000, + "currency": "EUR" + }, + "weight": 1, + "length": 0, + "width": 0, + "height": 0, + "settings": { + "id": "product", + "countryOfOrigin": -1, + "customsCode": -1, + "disableDeliveryOptions": -1, + "dropOffDelay": -1, + "exportAgeCheck": -1, + "exportHideSender": -1, + "exportInsurance": -1, + "exportLargeFormat": -1, + "exportOnlyRecipient": -1, + "exportReturn": -1, + "exportSignature": -1, + "fitInDigitalStamp": -1, + "fitInMailbox": -1, + "packageType": -1 + } +} diff --git a/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_unavailable_virtual_product__1.json b/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_unavailable_virtual_product__1.json new file mode 100644 index 00000000..417ac378 --- /dev/null +++ b/tests/__snapshots__/PsPdkProductRepositoryTest__it_creates_a_valid_pdk_product_with_data_set_unavailable_virtual_product__1.json @@ -0,0 +1,30 @@ +{ + "externalIdentifier": "1", + "isDeliverable": false, + "name": "Test product", + "price": { + "amount": 0, + "currency": "EUR" + }, + "weight": 0, + "length": 0, + "width": 0, + "height": 0, + "settings": { + "id": "product", + "countryOfOrigin": -1, + "customsCode": -1, + "disableDeliveryOptions": -1, + "dropOffDelay": -1, + "exportAgeCheck": -1, + "exportHideSender": -1, + "exportInsurance": -1, + "exportLargeFormat": -1, + "exportOnlyRecipient": -1, + "exportReturn": -1, + "exportSignature": -1, + "fitInDigitalStamp": -1, + "fitInMailbox": -1, + "packageType": -1 + } +} diff --git a/tests/factories/ProductFactory.php b/tests/factories/ProductFactory.php index 93bb28f3..838d18f8 100644 --- a/tests/factories/ProductFactory.php +++ b/tests/factories/ProductFactory.php @@ -7,7 +7,12 @@ /** * @see \ProductCore + * @method $this withIsActive(bool $isActive) + * @method $this withIsAvailableForOrder(bool $isAvailableForOrder) + * @method $this withIsVirtual(bool $isVirtual) * @method $this withName(array $names) + * @method $this withPrice(int $price) + * @method $this withWeight(float|string $weight) */ final class ProductFactory extends AbstractPsObjectModelFactory { @@ -17,7 +22,10 @@ protected function createDefault(): FactoryInterface ->withName([ 1 => 'Test product', 2 => 'Test product 2', - ]); + ]) + ->withIsActive(true) + ->withIsAvailableForOrder(true) + ->withIsVirtual(false); } protected function getObjectModelClass(): string