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/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([]); 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'); - } }