Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Model/WorkflowProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Test/Unit/Model/Cart/FullUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([]);

Expand Down
143 changes: 119 additions & 24 deletions Test/Unit/Model/WorkflowProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright © Klarna Bank AB (publ)
*
Expand Down Expand Up @@ -29,15 +30,27 @@ class WorkflowProviderTest extends TestCase
* @var WorkflowProvider
*/
private $model;

/**
* @var MockObject[]
*/
private $dependencyMocks;

/**
* @var MockFactory
*/
private $mockFactory;

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');
}

/**
* @covers ::setKlarnaOrderId
*/
Expand All @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -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');
}

/**
Expand All @@ -153,23 +230,51 @@ 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);

$klarnaInstance = $this->mockFactory->create(KlarnaOrder::class);
$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');
}

/**
Expand All @@ -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');
}
}