Skip to content

Commit 8156199

Browse files
committed
ACP2E-2850: refactoring based on CR comments
1 parent 1864bc5 commit 8156199

File tree

6 files changed

+61
-232
lines changed

6 files changed

+61
-232
lines changed

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/GuestValidation.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
1111
use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
1212
use Magento\CheckoutAgreements\Model\AgreementsProvider;
13-
use Magento\CheckoutAgreements\Model\EmulateStore;
1413
use Magento\Framework\App\Config\ScopeConfigInterface;
1514
use Magento\Framework\Exception\CouldNotSaveException;
1615
use Magento\Framework\Exception\NoSuchEntityException;
17-
use Magento\Quote\Api\CartRepositoryInterface;
1816
use Magento\Quote\Api\Data\AddressInterface;
1917
use Magento\Quote\Api\Data\PaymentInterface;
20-
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
18+
use Magento\Quote\Api\GuestCartRepositoryInterface;
19+
use Magento\Store\Model\App\Emulation;
2120
use Magento\Store\Model\ScopeInterface;
2221
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
2322

@@ -53,45 +52,37 @@ class GuestValidation
5352
private $activeStoreAgreementsFilter;
5453

5554
/**
56-
* @var MaskedQuoteIdToQuoteId
55+
* @var GuestCartRepositoryInterface
5756
*/
58-
private MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId;
57+
private GuestCartRepositoryInterface $quoteRepository;
5958

6059
/**
61-
* @var CartRepositoryInterface
60+
* @var Emulation
6261
*/
63-
private CartRepositoryInterface $quoteRepository;
64-
65-
/**
66-
* @var EmulateStore
67-
*/
68-
private EmulateStore $emulateStore;
62+
private Emulation $storeEmulation;
6963

7064
/**
7165
* @param AgreementsValidatorInterface $agreementsValidator
7266
* @param ScopeConfigInterface $scopeConfiguration
7367
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
7468
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
75-
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
76-
* @param CartRepositoryInterface $quoteRepository
77-
* @param EmulateStore $emulateStore
69+
* @param GuestCartRepositoryInterface $quoteRepository
70+
* @param Emulation $storeEmulation
7871
*/
7972
public function __construct(
8073
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
8174
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
8275
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
8376
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
84-
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
85-
CartRepositoryInterface $quoteRepository,
86-
EmulateStore $emulateStore
77+
GuestCartRepositoryInterface $quoteRepository,
78+
Emulation $storeEmulation
8779
) {
8880
$this->agreementsValidator = $agreementsValidator;
8981
$this->scopeConfiguration = $scopeConfiguration;
9082
$this->checkoutAgreementsList = $checkoutAgreementsList;
9183
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
92-
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
9384
$this->quoteRepository = $quoteRepository;
94-
$this->emulateStore = $emulateStore;
85+
$this->storeEmulation = $storeEmulation;
9586
}
9687

9788
/**
@@ -114,8 +105,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
114105
AddressInterface $billingAddress = null
115106
) {
116107
if ($this->isAgreementEnabled()) {
117-
$quoteId = $this->maskedQuoteIdToQuoteId->execute($cartId);
118-
$quote = $this->quoteRepository->get($quoteId);
108+
$quote = $this->quoteRepository->get($cartId);
119109
$storeId = $quote->getStoreId();
120110
$this->validateAgreements($paymentMethod, $storeId);
121111
}
@@ -135,11 +125,9 @@ private function validateAgreements(PaymentInterface $paymentMethod, int $storeI
135125
? []
136126
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
137127

138-
$isValid = $this->emulateStore->execute(
139-
$storeId,
140-
$this->agreementsValidator->isValid(...),
141-
[$agreements]
142-
);
128+
$this->storeEmulation->startEnvironmentEmulation($storeId);
129+
$isValid = $this->agreementsValidator->isValid($agreements);
130+
$this->storeEmulation->stopEnvironmentEmulation();
143131

144132
if (!$isValid) {
145133
throw new CouldNotSaveException(

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/Validation.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Exception\CouldNotSaveException;
1616
use Magento\Quote\Api\CartRepositoryInterface;
1717
use Magento\Quote\Api\Data\PaymentInterface;
18+
use Magento\Store\Model\App\Emulation;
1819
use Magento\Store\Model\ScopeInterface;
1920

2021
/**
@@ -48,32 +49,32 @@ class Validation
4849
private $quoteRepository;
4950

5051
/**
51-
* @var EmulateStore
52+
* @var Emulation
5253
*/
53-
private EmulateStore $emulateStore;
54+
private Emulation $storeEmulation;
5455

5556
/**
5657
* @param AgreementsValidatorInterface $agreementsValidator
5758
* @param ScopeConfigInterface $scopeConfiguration
5859
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
5960
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
6061
* @param CartRepositoryInterface $quoteRepository
61-
* @param EmulateStore $emulateStore
62+
* @param Emulation $storeEmulation
6263
*/
6364
public function __construct(
6465
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
6566
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
6667
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
6768
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
6869
CartRepositoryInterface $quoteRepository,
69-
EmulateStore $emulateStore
70+
Emulation $storeEmulation
7071
) {
7172
$this->agreementsValidator = $agreementsValidator;
7273
$this->scopeConfiguration = $scopeConfiguration;
7374
$this->checkoutAgreementsList = $checkoutAgreementsList;
7475
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
7576
$this->quoteRepository = $quoteRepository;
76-
$this->emulateStore = $emulateStore;
77+
$this->storeEmulation = $storeEmulation;
7778
}
7879

7980
/**
@@ -114,11 +115,9 @@ private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $pa
114115
? []
115116
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
116117

117-
$isValid = $this->emulateStore->execute(
118-
$storeId,
119-
$this->agreementsValidator->isValid(...),
120-
[$agreements]
121-
);
118+
$this->storeEmulation->startEnvironmentEmulation($storeId);
119+
$isValid = $this->agreementsValidator->isValid($agreements);
120+
$this->storeEmulation->stopEnvironmentEmulation();
122121

123122
if (!$isValid) {
124123
throw new \Magento\Framework\Exception\CouldNotSaveException(

app/code/Magento/CheckoutAgreements/Model/EmulateStore.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/GuestValidationTest.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
use Magento\CheckoutAgreements\Model\AgreementsProvider;
1414
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1515
use Magento\CheckoutAgreements\Model\Checkout\Plugin\GuestValidation;
16-
use Magento\CheckoutAgreements\Model\EmulateStore;
1716
use Magento\Framework\Api\SearchCriteria;
1817
use Magento\Framework\App\Config\ScopeConfigInterface;
1918
use Magento\Quote\Api\CartRepositoryInterface;
2019
use Magento\Quote\Api\Data\AddressInterface;
21-
use Magento\Quote\Api\Data\PaymentExtension;
2220
use Magento\Quote\Api\Data\PaymentExtensionInterface;
2321
use Magento\Quote\Api\Data\PaymentInterface;
22+
use Magento\Quote\Api\GuestCartRepositoryInterface;
2423
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
2524
use Magento\Quote\Model\Quote;
25+
use Magento\Store\Model\App\Emulation;
2626
use Magento\Store\Model\ScopeInterface;
2727
use PHPUnit\Framework\MockObject\MockObject;
2828
use PHPUnit\Framework\MockObject\RuntimeException;
@@ -94,9 +94,9 @@ class GuestValidationTest extends TestCase
9494
private CartRepositoryInterface|MockObject $cartRepositoryMock;
9595

9696
/**
97-
* @var EmulateStore|MockObject
97+
* @var Emulation|MockObject
9898
*/
99-
private EmulateStore|MockObject $emulateStoreMock;
99+
private Emulation|MockObject $storeEmulationMock;
100100

101101
protected function setUp(): void
102102
{
@@ -114,29 +114,25 @@ protected function setUp(): void
114114
);
115115
$this->quoteMock = $this->createMock(Quote::class);
116116
$this->maskedQuoteIdToQuoteIdMock = $this->createMock(MaskedQuoteIdToQuoteId::class);
117-
$this->cartRepositoryMock = $this->createMock(CartRepositoryInterface::class);
118-
$this->emulateStoreMock = $this->createMock(EmulateStore::class);
117+
$this->cartRepositoryMock = $this->createMock(GuestCartRepositoryInterface::class);
118+
$this->storeEmulationMock = $this->createMock(Emulation::class);
119119

120120
$storeId = 1;
121121
$this->quoteMock->expects($this->once())
122122
->method('getStoreId')
123123
->willReturn($storeId);
124-
$this->maskedQuoteIdToQuoteIdMock->expects($this->once())
125-
->method('execute')
126-
->with('0CQwCntNHR4yN9P5PUAzbxatvDvBXOce')
127-
->willReturn(1000);
128124
$this->cartRepositoryMock->expects($this->once())
129125
->method('get')
126+
->with('0CQwCntNHR4yN9P5PUAzbxatvDvBXOce')
130127
->willReturn($this->quoteMock);
131128

132129
$this->model = new GuestValidation(
133130
$this->agreementsValidatorMock,
134131
$this->scopeConfigMock,
135132
$this->checkoutAgreementsListMock,
136133
$this->agreementsFilterMock,
137-
$this->maskedQuoteIdToQuoteIdMock,
138134
$this->cartRepositoryMock,
139-
$this->emulateStoreMock
135+
$this->storeEmulationMock
140136
);
141137
}
142138

@@ -160,15 +156,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
160156
->with($searchCriteriaMock)
161157
->willReturn([1]);
162158
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
159+
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
163160
$this->paymentMock->expects(static::atLeastOnce())
164161
->method('getExtensionAttributes')
165162
->willReturn($this->extensionAttributesMock);
166-
$this->emulateStoreMock->expects($this->once())
167-
->method('execute')
168-
->with($storeId, $this->callback(function ($callback) {
169-
return is_callable($callback);
170-
}))
171-
->willReturn(true);
163+
$this->storeEmulationMock->expects($this->once())
164+
->method('startEnvironmentEmulation')
165+
->with($storeId);
166+
$this->storeEmulationMock->expects($this->once())
167+
->method('stopEnvironmentEmulation');
172168
$this->model->beforeSavePaymentInformationAndPlaceOrder(
173169
$this->subjectMock,
174170
$cartId,
@@ -185,12 +181,6 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
185181
$cartId = '0CQwCntNHR4yN9P5PUAzbxatvDvBXOce';
186182
$email = 'email@example.com';
187183
$agreements = [1, 2, 3];
188-
$this->emulateStoreMock->expects($this->once())
189-
->method('execute')
190-
->with($storeId, $this->callback(function ($callback) {
191-
return is_callable($callback);
192-
}))
193-
->willReturn(false);
194184
$this->scopeConfigMock
195185
->expects($this->once())
196186
->method('isSetFlag')
@@ -205,9 +195,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
205195
->with($searchCriteriaMock)
206196
->willReturn([1]);
207197
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
198+
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
208199
$this->paymentMock->expects(static::atLeastOnce())
209200
->method('getExtensionAttributes')
210201
->willReturn($this->extensionAttributesMock);
202+
$this->storeEmulationMock->expects($this->once())
203+
->method('startEnvironmentEmulation')
204+
->with($storeId);
205+
$this->storeEmulationMock->expects($this->once())
206+
->method('stopEnvironmentEmulation');
211207
$this->model->beforeSavePaymentInformationAndPlaceOrder(
212208
$this->subjectMock,
213209
$cartId,

0 commit comments

Comments
 (0)