diff --git a/src/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizer.php b/src/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizer.php index 2d7b6c5..6c7aace 100644 --- a/src/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizer.php +++ b/src/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizer.php @@ -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 @@ -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, @@ -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; + } } diff --git a/tests/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizerTest.php b/tests/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizerTest.php index 118d0d9..3eeb065 100644 --- a/tests/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizerTest.php +++ b/tests/FondOfSpryker/Zed/CompanyUserCartsRestApi/Business/Categorizer/ItemsCategorizerTest.php @@ -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], + ); + } }