Skip to content

Commit

Permalink
🐛 fix a bug when the quantity is not numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
julianzimmermann committed Jan 11, 2024
1 parent 4f5c6b2 commit 1c26a99
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use FondOfSpryker\Zed\CompanyUserCartsRestApi\Business\Grouper\ItemsGrouperInterface;
use FondOfSpryker\Zed\CompanyUserCartsRestApi\Business\Mapper\ItemMapperInterface;
use Generated\Shared\Transfer\QuoteTransfer;
use Generated\Shared\Transfer\RestCartItemTransfer;
use Generated\Shared\Transfer\RestCartsRequestAttributesTransfer;

class ItemsCategorizer implements ItemsCategorizerInterface
Expand Down Expand Up @@ -57,7 +58,7 @@ public function categorize(
];

foreach ($restCartsRequestAttributesTransfer->getItems() as $restCartItemTransfer) {
$newQuantity = $restCartItemTransfer->getQuantity();
$newQuantity = $this->resolveQuantityFromRequest($restCartItemTransfer);
$oldItemTransfer = $this->itemFinder->findInGroupedItemsByRestCartItem(
$groupedItemTransfers,
$restCartItemTransfer,
Expand Down Expand Up @@ -88,4 +89,20 @@ public function categorize(

return $categorisedItemTransfers;
}

/**
* @param \Generated\Shared\Transfer\RestCartItemTransfer $restCartItemTransfer
*
* @return int
*/
protected function resolveQuantityFromRequest(RestCartItemTransfer $restCartItemTransfer): int
{
$newQuantity = $restCartItemTransfer->getQuantity();

if (is_numeric($newQuantity)) {
return (int)$newQuantity;
}

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,99 @@ public function testCategorize(): void
$categorisedItemTransfers[ItemsCategorizerInterface::CATEGORY_ADDABLE][1],
);
}

/**
* @return void
*/
public function testCategorizeWithString(): void
{
$newQuantities = [2, 0, ''];
$currentQuantities = [1, 1];

$this->itemsGrouperMock->expects(static::atLeastOnce())
->method('groupByQuote')
->with($this->quoteTransferMock)
->willReturn($this->itemTransferMocks);

$this->restCartRequestAttributesTransferMock->expects(static::atLeastOnce())
->method('getItems')
->willReturn(new ArrayObject($this->restCartItemTransferMocks));

$this->itemFinderMock->expects(static::atLeastOnce())
->method('findInGroupedItemsByRestCartItem')
->withConsecutive(
[$this->itemTransferMocks, $this->restCartItemTransferMocks[0]],
[$this->itemTransferMocks, $this->restCartItemTransferMocks[1]],
[$this->itemTransferMocks, $this->restCartItemTransferMocks[2]],
)->willReturnOnConsecutiveCalls(
null,
$this->itemTransferMocks['foo.bar-1'],
$this->itemTransferMocks['foo.bar-2'],
);

$this->itemMapperMock->expects(static::atLeastOnce())
->method('fromRestCartItem')
->withConsecutive(
[$this->restCartItemTransferMocks[0]],
[$this->restCartItemTransferMocks[1]],
[$this->restCartItemTransferMocks[2]],
)->willReturnOnConsecutiveCalls(
$this->newItemTransferMocks[0],
$this->newItemTransferMocks[1],
$this->newItemTransferMocks[2],
);

$this->restCartItemTransferMocks[0]->expects(static::atLeastOnce())
->method('getQuantity')
->willReturn($newQuantities[0]);

$this->restCartItemTransferMocks[1]->expects(static::atLeastOnce())
->method('getQuantity')
->willReturn($newQuantities[1]);

$this->itemTransferMocks['foo.bar-1']->expects(static::atLeastOnce())
->method('getQuantity')
->willReturn($currentQuantities[0]);

$this->newItemTransferMocks[1]->expects(static::atLeastOnce())
->method('setQuantity')
->with(1)
->willReturn($this->newItemTransferMocks[1]);

$this->restCartItemTransferMocks[2]->expects(static::atLeastOnce())
->method('getQuantity')
->willReturn($newQuantities[2]);

$this->itemTransferMocks['foo.bar-2']->expects(static::atLeastOnce())
->method('getQuantity')
->willReturn($currentQuantities[1]);

$this->newItemTransferMocks[2]->expects(static::atLeastOnce())
->method('setQuantity')
->with(1)
->willReturn($this->newItemTransferMocks[2]);

$categorisedItemTransfers = $this->itemsCategorizer->categorize(
$this->quoteTransferMock,
$this->restCartRequestAttributesTransferMock,
);

static::assertCount(1, $categorisedItemTransfers[ItemsCategorizerInterface::CATEGORY_ADDABLE]);
static::assertCount(2, $categorisedItemTransfers[ItemsCategorizerInterface::CATEGORY_REMOVABLE]);

static::assertEquals(
$this->newItemTransferMocks[0],
$categorisedItemTransfers[ItemsCategorizerInterface::CATEGORY_ADDABLE][0],
);

static::assertEquals(
$this->newItemTransferMocks[1],
$categorisedItemTransfers[ItemsCategorizerInterface::CATEGORY_REMOVABLE][0],
);

static::assertEquals(
$this->newItemTransferMocks[2],
$categorisedItemTransfers[ItemsCategorizerInterface::CATEGORY_ADDABLE][0],
);
}
}

0 comments on commit 1c26a99

Please sign in to comment.