From 6912c5f20510f03acb4c4c9b97cc180128faf547 Mon Sep 17 00:00:00 2001 From: Joona Melartin Date: Wed, 6 May 2026 09:42:50 +0300 Subject: [PATCH 1/2] Fixed issue in one of the module unit tests that wasn't compatible with different Magento versions --- Test/Unit/Model/Cart/FullUpdateTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Test/Unit/Model/Cart/FullUpdateTest.php b/Test/Unit/Model/Cart/FullUpdateTest.php index a646471..0877f62 100644 --- a/Test/Unit/Model/Cart/FullUpdateTest.php +++ b/Test/Unit/Model/Cart/FullUpdateTest.php @@ -6,7 +6,7 @@ * and LICENSE files that were distributed with this source code. */ -namespace Klarna\Kco\Test\Unit\Model\Checkout; +namespace Klarna\Kco\Test\Unit\Model\Cart; use Klarna\Kco\Model\Cart\FullUpdate; use Klarna\Base\Test\Unit\Mock\MockFactory; @@ -112,7 +112,12 @@ protected function setUp(): void $this->quote->method('getShippingAddress') ->willReturn($quoteShippingAddress); - $extensionAttributes = $this->mockFactory->create(CartExtension::class, [], ['getShippingAssignments']); + if (method_exists(CartExtension::class, 'getShippingAssignments')) { + $extensionAttributes = $this->mockFactory->create(CartExtension::class, ['getShippingAssignments']); + } else { + $extensionAttributes = $this->mockFactory->create(CartExtension::class, [], ['getShippingAssignments']); + } + $extensionAttributes->method('getShippingAssignments') ->willReturn([]); From a5838558aef131d6520d1690594d5c6a117bc134 Mon Sep 17 00:00:00 2001 From: Joona Melartin Date: Wed, 6 May 2026 15:59:51 +0300 Subject: [PATCH 2/2] KUSTOM-90: Updated unit tests to verify that WorkflowProvider is correctly caching retrieved order/quote data to the instance and to replicate an issue where consecutive calls of setKlarnaOrderId with different values doesn't result in the data in the instance not getting reset. Applied clearing of instance cache data in WorkflowProvider when setKlarnaOrderId is called. --- Model/WorkflowProvider.php | 4 + Test/Unit/Model/WorkflowProviderTest.php | 143 +++++++++++++++++++---- 2 files changed, 123 insertions(+), 24 deletions(-) diff --git a/Model/WorkflowProvider.php b/Model/WorkflowProvider.php index 55a28b2..041c9bc 100644 --- a/Model/WorkflowProvider.php +++ b/Model/WorkflowProvider.php @@ -97,6 +97,10 @@ public function setKlarnaOrderId(string $klarnaOrderId): void } $this->klarnaOrderId = $klarnaOrderId; + $this->kcoQuote = null; + $this->magentoQuote = null; + $this->klarnaOrder = null; + $this->magentoOrder = null; } /** diff --git a/Test/Unit/Model/WorkflowProviderTest.php b/Test/Unit/Model/WorkflowProviderTest.php index a28d787..d03cc7b 100644 --- a/Test/Unit/Model/WorkflowProviderTest.php +++ b/Test/Unit/Model/WorkflowProviderTest.php @@ -1,4 +1,5 @@ mockFactory = new MockFactory($this); + $objectFactory = new TestObjectFactory($this->mockFactory); + $this->model = $objectFactory->create(WorkflowProvider::class); + $this->dependencyMocks = $objectFactory->getDependencyMocks(); + + $this->model->setKlarnaOrderId('123'); + } + /** * @covers ::setKlarnaOrderId */ @@ -59,15 +72,34 @@ public function testSetKlarnaOrderIdInputIsNotEmpty(): void /** * @covers ::getKcoQuote */ - public function testGetKcoQuoteReturnsInstance(): void + public function testGetKcoQuoteReturnsInstanceWithCache(): void { $instance = $this->mockFactory->create(KcoQuote::class); - $this->dependencyMocks['kcoQuoteRepository']->method('getByCheckoutId') + $this->dependencyMocks['kcoQuoteRepository']->expects($this->once())->method('getByCheckoutId') ->with('123') ->willReturn($instance); - self::assertSame($instance, $this->model->getKcoQuote()); + $this->assertSame($instance, $this->model->getKcoQuote()); + $this->assertSame($instance, $this->model->getKcoQuote(), 'Assert that cache works'); + } + + /** + * @covers ::getKcoQuote + */ + public function testGetKcoQuoteReturnsDifferentInstanceAfterSetKlarnaOrder(): void + { + $instance1 = $this->mockFactory->create(KcoQuote::class); + $instance2 = $this->mockFactory->create(KcoQuote::class); + + $this->dependencyMocks['kcoQuoteRepository']->expects($this->exactly(2)) + ->method('getByCheckoutId') + ->willReturnOnConsecutiveCalls($instance1, $instance2); + + $this->assertSame($instance1, $this->model->getKcoQuote()); + $this->assertSame($instance1, $this->model->getKcoQuote(), 'Assert that cache works'); + $this->model->setKlarnaOrderId('234'); + $this->assertSame($instance2, $this->model->getKcoQuote(), 'Assert that we get different instance'); } /** @@ -86,21 +118,48 @@ public function testGetKcoQuoteNoQuoteFound(): void /** * @covers ::getMagentoQuote */ - public function testGetMagentoQuoteReturnsInstance(): void + public function testGetMagentoQuoteReturnsInstanceWithCache(): void { $kcoQuote = $this->mockFactory->create(KcoQuote::class); $kcoQuote->method('getQuoteId') ->willReturn('456'); - $this->dependencyMocks['kcoQuoteRepository']->method('getByCheckoutId') + $this->dependencyMocks['kcoQuoteRepository']->expects($this->once())->method('getByCheckoutId') ->with('123') ->willReturn($kcoQuote); $magentoQuote = $this->mockFactory->create(MagentoQuote::class); - $this->dependencyMocks['magentoQuoteRepository']->method('get') + $this->dependencyMocks['magentoQuoteRepository']->expects($this->once())->method('get') ->with('456') ->willReturn($magentoQuote); - self::assertSame($magentoQuote, $this->model->getMagentoQuote()); + $this->assertSame($magentoQuote, $this->model->getMagentoQuote()); + $this->assertSame($magentoQuote, $this->model->getMagentoQuote(), 'Assert that cache works'); + } + + /** + * @covers ::getMagentoQuote + */ + public function testGetMagentoQuoteReturnsDifferentInstanceAfterSetKlarnaOrder(): void + { + $kcoQuote1 = $this->mockFactory->create(KcoQuote::class); + $kcoQuote1->method('getQuoteId') + ->willReturn('456'); + $kcoQuote2 = $this->mockFactory->create(KcoQuote::class); + $kcoQuote2->method('getQuoteId') + ->willReturn('567'); + + $this->dependencyMocks['kcoQuoteRepository']->expects($this->exactly(2))->method('getByCheckoutId') + ->willReturnOnConsecutiveCalls($kcoQuote1, $kcoQuote2); + + $magentoQuote1 = $this->mockFactory->create(MagentoQuote::class); + $magentoQuote2 = $this->mockFactory->create(MagentoQuote::class); + $this->dependencyMocks['magentoQuoteRepository']->expects($this->exactly(2))->method('get') + ->willReturnOnConsecutiveCalls($magentoQuote1, $magentoQuote2); + + $this->assertSame($magentoQuote1, $this->model->getMagentoQuote()); + $this->assertSame($magentoQuote1, $this->model->getMagentoQuote(), 'Assert that cache works'); + $this->model->setKlarnaOrderId('234'); + $this->assertSame($magentoQuote2, $this->model->getMagentoQuote(), 'Assert that we get different instance'); } /** @@ -126,15 +185,33 @@ public function testGetMagentoQuoteNoQuoteFound(): void /** * @covers ::getKlarnaOrder */ - public function testGetKlarnaOrderReturnsInstance(): void + public function testGetKlarnaOrderReturnsInstanceWithCache(): void { $instance = $this->mockFactory->create(KlarnaOrder::class); - $this->dependencyMocks['klarnaOrderRepository']->method('getByKlarnaOrderId') + $this->dependencyMocks['klarnaOrderRepository']->expects($this->once())->method('getByKlarnaOrderId') ->with('123') ->willReturn($instance); - self::assertSame($instance, $this->model->getKlarnaOrder()); + $this->assertSame($instance, $this->model->getKlarnaOrder()); + $this->assertSame($instance, $this->model->getKlarnaOrder(), 'Assert that cache works'); + } + + /** + * @covers ::getKlarnaOrder + */ + public function testGetKlarnaOrderReturnsDifferentInstanceAfterSetKlarnaOrder(): void + { + $instance1 = $this->mockFactory->create(KlarnaOrder::class); + $instance2 = $this->mockFactory->create(KlarnaOrder::class); + + $this->dependencyMocks['klarnaOrderRepository']->expects($this->exactly(2))->method('getByKlarnaOrderId') + ->willReturnOnConsecutiveCalls($instance1, $instance2); + + $this->assertSame($instance1, $this->model->getKlarnaOrder()); + $this->assertSame($instance1, $this->model->getKlarnaOrder(), 'Assert that cache works'); + $this->model->setKlarnaOrderId('234'); + $this->assertSame($instance2, $this->model->getKlarnaOrder(), 'Assert that we get different instance'); } /** @@ -153,11 +230,11 @@ public function testGetKlarnaOrderNoOrderFound(): void /** * @covers ::getMagentoOrder */ - public function testGetMagentoOrderReturnsInstance(): void + public function testGetMagentoOrderReturnsInstanceWithCache(): void { $magentoInstance = $this->mockFactory->create(MagentoOrder::class); - $this->dependencyMocks['magentoOrderRepository']->method('get') + $this->dependencyMocks['magentoOrderRepository']->expects($this->once())->method('get') ->with('456') ->willReturn($magentoInstance); @@ -165,11 +242,39 @@ public function testGetMagentoOrderReturnsInstance(): void $klarnaInstance->method('getOrderId') ->willReturn('456'); - $this->dependencyMocks['klarnaOrderRepository']->method('getByKlarnaOrderId') + $this->dependencyMocks['klarnaOrderRepository']->expects($this->once())->method('getByKlarnaOrderId') ->with('123') ->willReturn($klarnaInstance); - self::assertSame($magentoInstance, $this->model->getMagentoOrder()); + $this->assertSame($magentoInstance, $this->model->getMagentoOrder()); + $this->assertSame($magentoInstance, $this->model->getMagentoOrder(), 'Assert that cache works'); + } + + /** + * @covers ::getMagentoOrder + */ + public function testGetMagentoOrderReturnsDifferentInstanceAfterSetKlarnaOrder(): void + { + $magentoInstance1 = $this->mockFactory->create(MagentoOrder::class); + $magentoInstance2 = $this->mockFactory->create(MagentoOrder::class); + + $this->dependencyMocks['magentoOrderRepository']->expects($this->exactly(2))->method('get') + ->willReturn($magentoInstance1, $magentoInstance2); + + $klarnaInstance1 = $this->mockFactory->create(KlarnaOrder::class); + $klarnaInstance1->method('getOrderId') + ->willReturn('456'); + $klarnaInstance2 = $this->mockFactory->create(KlarnaOrder::class); + $klarnaInstance2->method('getOrderId') + ->willReturn('567'); + + $this->dependencyMocks['klarnaOrderRepository']->expects($this->exactly(2))->method('getByKlarnaOrderId') + ->willReturn($klarnaInstance1, $klarnaInstance2); + + $this->assertSame($magentoInstance1, $this->model->getMagentoOrder()); + $this->assertSame($magentoInstance1, $this->model->getMagentoOrder(), 'Assert that cache works'); + $this->model->setKlarnaOrderId('234'); + $this->assertSame($magentoInstance2, $this->model->getMagentoOrder(), 'Assert that we get different instance'); } /** @@ -192,14 +297,4 @@ public function testGetMagentoOrderNoOrderFound(): void $this->model->getMagentoOrder(); } - - protected function setUp(): void - { - $this->mockFactory = new MockFactory($this); - $objectFactory = new TestObjectFactory($this->mockFactory); - $this->model = $objectFactory->create(WorkflowProvider::class); - $this->dependencyMocks = $objectFactory->getDependencyMocks(); - - $this->model->setKlarnaOrderId('123'); - } }