Skip to content

Commit

Permalink
fix(orders): fix error when weight is a string
Browse files Browse the repository at this point in the history
INT-552

Resolves #256
  • Loading branch information
EdieLemoine committed Jul 12, 2024
1 parent df5c6a3 commit 7081a59
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Pdk/Product/Repository/PsPdkProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/Pdk/Product/Repository/PsPdkProductRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection,StaticClosureCanBeUsedInspection */

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Pdk\Product\Repository;

use MyParcelNL\Pdk\App\Order\Contract\PdkProductRepositoryInterface;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Tests\Uses\UsesMockPsPdkInstance;
use Product;
use ProductFactory;
use function MyParcelNL\Pdk\Tests\usesShared;
use function MyParcelNL\PrestaShop\psFactory;
use function Spatie\Snapshots\assertMatchesJsonSnapshot;

usesShared(new UsesMockPsPdkInstance());

it('creates a valid pdk product', function (ProductFactory $productFactory) {
/** @var \MyParcelNL\Pdk\App\Order\Contract\PdkProductRepositoryInterface $productRepository */
$productRepository = Pdk::get(PdkProductRepositoryInterface::class);

/** @var Product $psProduct */
$psProduct = $productFactory->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);
},
]);
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 9 additions & 1 deletion tests/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down

0 comments on commit 7081a59

Please sign in to comment.