From ac7bb096893e56093cc3d75a7b10f8f7c5a0b89b Mon Sep 17 00:00:00 2001 From: Marvin Muxfeld Date: Mon, 12 Aug 2024 09:20:36 +0200 Subject: [PATCH] PISHPS-311: SubscriptionCartCollector now uses LineItemAttributes to determine if a product is a subscription product --- src/Resources/config/services/services.xml | 4 +++ .../SubscriptionCartCollector.php | 31 ++++++------------- .../SubscriptionProductIdentifier.php | 21 +++++++++++++ .../SubscriptionCartCollectorTest.php | 23 +++++--------- 4 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 src/Service/Cart/Subscription/SubscriptionProductIdentifier.php diff --git a/src/Resources/config/services/services.xml b/src/Resources/config/services/services.xml index 3e85b7f76..f34a87fac 100644 --- a/src/Resources/config/services/services.xml +++ b/src/Resources/config/services/services.xml @@ -136,9 +136,13 @@ + + + + diff --git a/src/Service/Cart/Subscription/SubscriptionCartCollector.php b/src/Service/Cart/Subscription/SubscriptionCartCollector.php index 60435d35f..1f03c56a1 100644 --- a/src/Service/Cart/Subscription/SubscriptionCartCollector.php +++ b/src/Service/Cart/Subscription/SubscriptionCartCollector.php @@ -8,7 +8,6 @@ use Shopware\Core\Checkout\Cart\CartBehavior; use Shopware\Core\Checkout\Cart\CartDataCollectorInterface; use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection; -use Shopware\Core\Checkout\Cart\LineItem\LineItem as CheckoutCartLineItem; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -20,21 +19,26 @@ */ class SubscriptionCartCollector implements CartDataCollectorInterface { - private const SUBSCRIPTION_ENABLED = 'mollie_payments_product_subscription_enabled'; - /** * @var EventDispatcherInterface */ private $dispatcher; + /** + * @var SubscriptionProductIdentifier + */ + private $subscriptionProductIdentifier; + /** * SubscriptionCartCollector constructor. * * @param EventDispatcherInterface $dispatcher The event dispatcher + * @param SubscriptionProductIdentifier $subscriptionProductIdentifier */ - public function __construct(EventDispatcherInterface $dispatcher) + public function __construct(EventDispatcherInterface $dispatcher, SubscriptionProductIdentifier $subscriptionProductIdentifier) { $this->dispatcher = $dispatcher; + $this->subscriptionProductIdentifier = $subscriptionProductIdentifier; } /** @@ -49,27 +53,10 @@ public function collect(CartDataCollection $data, Cart $original, SalesChannelCo { $events = []; foreach ($original->getLineItems() as $lineItem) { - if ($this->lineItemIsSubscriptionProduct($lineItem)) { + if ($this->subscriptionProductIdentifier->isSubscriptionProduct($lineItem)) { $events[] = new MollieSubscriptionCartItemAddedEvent($context, $lineItem); } } array_map([$this->dispatcher, 'dispatch'], $events); } - - /** - * Checks if a line item is a subscription product. - * - * @param CheckoutCartLineItem $lineItem The line item to check - * @return bool True if the line item is a subscription product, false otherwise - */ - private function lineItemIsSubscriptionProduct(CheckoutCartLineItem $lineItem): bool - { - $customFields = $lineItem->getPayloadValue('customFields'); - - if (is_array($customFields) === false || isset($customFields[self::SUBSCRIPTION_ENABLED]) === false) { - return false; - } - - return (bool)$customFields[self::SUBSCRIPTION_ENABLED]; - } } diff --git a/src/Service/Cart/Subscription/SubscriptionProductIdentifier.php b/src/Service/Cart/Subscription/SubscriptionProductIdentifier.php new file mode 100644 index 000000000..c86fd9321 --- /dev/null +++ b/src/Service/Cart/Subscription/SubscriptionProductIdentifier.php @@ -0,0 +1,21 @@ +isSubscriptionProduct(); + } +} diff --git a/tests/PHPUnit/Service/Cart/Subscription/SubscriptionCartCollectorTest.php b/tests/PHPUnit/Service/Cart/Subscription/SubscriptionCartCollectorTest.php index 87748d70e..aec153d36 100644 --- a/tests/PHPUnit/Service/Cart/Subscription/SubscriptionCartCollectorTest.php +++ b/tests/PHPUnit/Service/Cart/Subscription/SubscriptionCartCollectorTest.php @@ -6,6 +6,7 @@ use Kiener\MolliePayments\Event\MollieSubscriptionCartItemAddedEvent; use Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionCartCollector; +use Kiener\MolliePayments\Service\Cart\Subscription\SubscriptionProductIdentifier; use PHPUnit\Framework\TestCase; use Shopware\Core\Checkout\Cart\Cart; use Shopware\Core\Checkout\Cart\CartBehavior; @@ -19,19 +20,23 @@ class SubscriptionCartCollectorTest extends TestCase { const SUBSCRIPTION_ENABLED = 'mollie_payments_product_subscription_enabled'; private $dispatcher; - + private $identifier; private $collector; - private $data; protected function setUp(): void { $this->dispatcher = $this->createMock(EventDispatcherInterface::class); - $this->collector = new SubscriptionCartCollector($this->dispatcher); + $this->identifier = $this->createMock(SubscriptionProductIdentifier::class); + $this->collector = new SubscriptionCartCollector($this->dispatcher, $this->identifier); $this->data = $this->createMock(CartDataCollection::class); $this->original = $this->createMock(Cart::class); $this->context = $this->createMock(SalesChannelContext::class); $this->behavior = $this->createMock(CartBehavior::class); + + $this->identifier->method('isSubscriptionProduct')->willReturnCallback(function (CheckoutCartLineItem $lineItem) { + return $lineItem->getPayloadValue(self::SUBSCRIPTION_ENABLED)[self::SUBSCRIPTION_ENABLED] ?? false; + }); } public function testDispatchesEventWhenAProductIsAMollieSubscriptionProduct(): void @@ -65,18 +70,6 @@ public function testDoesNotDispatchEventWhenNoMollieSubscriptionProductIsAdded() $this->collector->collect($this->data, $this->original, $this->context, $this->behavior); } - public function testDoesNotDispatchEventWhenCustomFieldsIsMissingSubscriptionData(): void - { - $incorrectlyConfiguredProduct = $this->createLineItemMockWithPayloadValue((object)[self::SUBSCRIPTION_ENABLED => false,]); - - $this->configureGetLineItemsMethodOfCart($incorrectlyConfiguredProduct); - - // we expect the event to not be dispatched - $this->dispatcher->expects($this->never())->method('dispatch'); - - $this->collector->collect($this->data, $this->original, $this->context, $this->behavior); - } - private function createLineItemMockWithPayloadValue($value): CheckoutCartLineItem { return $this->createConfiguredMock(