Skip to content

Commit

Permalink
Refactor / better Quote getting
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusjp committed Apr 6, 2022
1 parent 22d4b8c commit 390d3ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/Service/CustomerQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,33 @@ public function getQuote(): CartInterface
try {
// User is logged in and has a filled cart
$quote = $this->checkoutSession->getQuote();
$quoteId = $quote->getEntityId();
} catch (NoSuchEntityException | LocalizedException $e) {
// User is logged in but has an empty cart
// User is logged in but has an empty cart or
// something went wrong when trying to get the customer cart
$quoteId = $this->cartManagement->createEmptyCartForCustomer($this->customerSession->getCustomerId());
$quote = $this->quoteRepository->get($quoteId);
}
} else {
try {
// User is not logged in and has a filled cart
// User is not logged in and has a filled or empty cart
$quote = $this->checkoutSession->getQuote();
$quoteId = $quote->getEntityId();
} catch (NoSuchEntityException | LocalizedException $e) {
// User is not logged in and has an empty cart
// User is not logged in and has an empty cart or
// something went wrong when trying to get the guest cart
$cartApiId = $this->guestCartManagement->createEmptyCart();
$quoteId = $this->maskedQuoteIdToQuoteId->execute($cartApiId);
$this->checkoutSession->setQuoteId($quoteId);
$quote = $this->quoteRepository->get($quoteId);
}
}

if ($quote === null) {
$quote = $this->quoteRepository->get($quoteId);
// Always return a fully loaded Quote
if ($quote->getId() === null) {
$this->quoteRepository->save($quote);
$quote = $this->quoteRepository->get($quote->getId());
}

$this->checkoutSession->setQuoteId($quote->getId());

return $quote;
}

Expand Down
28 changes: 19 additions & 9 deletions src/Test/Unit/Service/CustomerQuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,24 @@ public function testGetQuoteGuestWithoutExistingCart(): void
->with('maskedID1234')
->willReturn(123);

$this->checkoutSession
->expects(self::once())
->method('setQuoteId')
->with(123);

$quoteMock = $this->createQuote();

$quoteMock
->expects(self::exactly(2))
->method('getId')
->willReturn(123, 123);

$this->quoteRepository
->expects(self::once())
->method('get')
->with(123)
->willReturn($quoteMock);

$this->checkoutSession
->expects(self::once())
->method('setQuoteId')
->with(123);

$quote = $customerQuote->getQuote();

self::assertEquals($quoteMock, $quote);
Expand All @@ -100,8 +105,8 @@ public function testGetQuoteGuestWithExistingCart(): void

$quoteMock = $this->createQuote();
$quoteMock
->expects(self::once())
->method('getEntityId')
->expects(self::exactly(2))
->method('getId')
->willReturn(1);

$this->checkoutSession
Expand Down Expand Up @@ -144,6 +149,11 @@ public function testGetQuoteCustomerWithoutExistingCart(): void

$quoteMock = $this->createQuote();

$quoteMock
->expects(self::exactly(2))
->method('getId')
->willReturn(123, 123);

$this->quoteRepository
->expects(self::once())
->method('get')
Expand All @@ -169,8 +179,8 @@ public function testGetQuoteCustomerWithExistingCart(): void

$quoteMock = $this->createQuote();
$quoteMock
->expects(self::once())
->method('getEntityId')
->expects(self::exactly(2))
->method('getId')
->willReturn(1);

$this->checkoutSession
Expand Down
2 changes: 1 addition & 1 deletion src/registration.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

// @codeCoverageIgnoreStart
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

// @codeCoverageIgnoreStart
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Coddin_CartBridge',
Expand Down

0 comments on commit 390d3ee

Please sign in to comment.