Skip to content

Commit 305ce5f

Browse files
committed
AC-15104::[CE] PHPUnit 12: Upgrade Checkout & Cart Management related test cases
1 parent 7189d2b commit 305ce5f

File tree

14 files changed

+224
-48
lines changed

14 files changed

+224
-48
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CheckoutAgreements\Test\Unit\Helper;
9+
10+
use Magento\CheckoutAgreements\Model\Agreement as AgreementModel;
11+
12+
/**
13+
* Test helper for Agreement model to expose setStores() for PHPUnit 12.
14+
*/
15+
class AgreementModelTestHelper extends AgreementModel
16+
{
17+
/** @var array|null */
18+
private ?array $stores = null;
19+
20+
public function __construct()
21+
{
22+
// Intentionally skip parent constructor
23+
}
24+
25+
/**
26+
* Set stores for tests.
27+
*
28+
* @param array|null $stores
29+
* @return $this
30+
*/
31+
public function setStores($stores)
32+
{
33+
$this->stores = $stores;
34+
return $this;
35+
}
36+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
use Magento\Framework\App\Config\ScopeConfigInterface;
1818
use Magento\Quote\Api\CartRepositoryInterface;
1919
use Magento\Quote\Api\Data\AddressInterface;
20+
use Magento\Quote\Api\Data\PaymentExtensionInterface;
2021
use Magento\Quote\Api\Data\PaymentInterface;
2122
use Magento\Quote\Api\GuestCartRepositoryInterface;
2223
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
2324
use Magento\Quote\Model\Quote;
2425
use Magento\Store\Model\App\Emulation;
2526
use Magento\Store\Model\ScopeInterface;
27+
use Magento\Quote\Test\Unit\Helper\PaymentExtensionTestHelper;
2628
use PHPUnit\Framework\MockObject\MockObject;
29+
use PHPUnit\Framework\MockObject\RuntimeException;
2730
use PHPUnit\Framework\TestCase;
28-
use Magento\Quote\Test\Unit\Helper\PaymentExtensionAgreementIdsTestHelper;
2931

3032
/**
3133
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -58,7 +60,7 @@ class GuestValidationTest extends TestCase
5860
private $addressMock;
5961

6062
/**
61-
* @var object
63+
* @var MockObject
6264
*/
6365
private $extensionAttributesMock;
6466

@@ -154,9 +156,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
154156
->method('getList')
155157
->with($searchCriteriaMock)
156158
->willReturn([1]);
157-
if (method_exists($this->extensionAttributesMock, 'setAgreementIds')) {
158-
$this->extensionAttributesMock->setAgreementIds($agreements);
159-
}
159+
$this->extensionAttributesMock->method('getAgreementIds')->willReturn($agreements);
160160
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
161161
$this->paymentMock->expects(static::atLeastOnce())
162162
->method('getExtensionAttributes')
@@ -195,9 +195,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
195195
->method('getList')
196196
->with($searchCriteriaMock)
197197
->willReturn([1]);
198-
if (method_exists($this->extensionAttributesMock, 'setAgreementIds')) {
199-
$this->extensionAttributesMock->setAgreementIds($agreements);
200-
}
198+
$this->extensionAttributesMock->method('getAgreementIds')->willReturn($agreements);
201199
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
202200
$this->paymentMock->expects(static::atLeastOnce())
203201
->method('getExtensionAttributes')
@@ -221,10 +219,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
221219
}
222220

223221
/**
224-
* Build payment extension attributes stub.
222+
* Build payment extension mock.
223+
*
224+
* @return MockObject
225225
*/
226-
private function getPaymentExtension(): object
226+
private function getPaymentExtension(): PaymentExtensionInterface
227227
{
228-
return new PaymentExtensionAgreementIdsTestHelper();
228+
return $this->createPartialMock(
229+
PaymentExtensionTestHelper::class,
230+
['getAgreementIds', 'setAgreementIds']
231+
);
229232
}
230233
}

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
use Magento\Framework\App\Config\ScopeConfigInterface;
1919
use Magento\Quote\Api\CartRepositoryInterface;
2020
use Magento\Quote\Api\Data\AddressInterface;
21+
use Magento\Quote\Api\Data\PaymentExtensionInterface;
2122
use Magento\Quote\Api\Data\PaymentInterface;
23+
use Magento\Quote\Model\Quote;
2224
use Magento\Store\Model\App\Emulation;
2325
use Magento\Store\Model\ScopeInterface;
26+
use Magento\Quote\Test\Unit\Helper\PaymentExtensionTestHelper;
2427
use PHPUnit\Framework\MockObject\MockObject;
28+
use PHPUnit\Framework\MockObject\RuntimeException;
2529
use PHPUnit\Framework\TestCase;
26-
use Magento\Quote\Test\Unit\Helper\PaymentExtensionAgreementIdsTestHelper;
27-
use Magento\Quote\Test\Unit\Helper\QuoteIsMultiShippingTestHelper;
2830

2931
/**
3032
* Class ValidationTest validates the agreement based on the payment method
@@ -58,7 +60,7 @@ class ValidationTest extends TestCase
5860
protected $addressMock;
5961

6062
/**
61-
* @var object
63+
* @var MockObject
6264
*/
6365
protected $extensionAttributesMock;
6466

@@ -78,7 +80,7 @@ class ValidationTest extends TestCase
7880
private $agreementsFilterMock;
7981

8082
/**
81-
* @var QuoteIsMultiShippingTestHelper
83+
* @var MockObject
8284
*/
8385
private $quoteMock;
8486

@@ -98,7 +100,10 @@ protected function setUp(): void
98100
$this->subjectMock = $this->createMock(PaymentInformationManagementInterface::class);
99101
$this->paymentMock = $this->createMock(PaymentInterface::class);
100102
$this->addressMock = $this->createMock(AddressInterface::class);
101-
$this->quoteMock = new QuoteIsMultiShippingTestHelper(1, false);
103+
$this->quoteMock = $this->createPartialMock(
104+
\Magento\Quote\Test\Unit\Helper\QuoteTestHelper::class,
105+
['getIsMultiShipping', 'getStoreId']
106+
);
102107
$this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
103108
$this->extensionAttributesMock = $this->getPaymentExtension();
104109
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
@@ -110,6 +115,10 @@ protected function setUp(): void
110115
);
111116
$this->storeEmulationMock = $this->createMock(Emulation::class);
112117

118+
$storeId = 1;
119+
$this->quoteMock->expects($this->once())
120+
->method('getStoreId')
121+
->willReturn($storeId);
113122
$this->quoteRepositoryMock->expects($this->once())
114123
->method('get')
115124
->willReturn($this->quoteMock);
@@ -126,6 +135,7 @@ protected function setUp(): void
126135

127136
public function testBeforeSavePaymentInformationAndPlaceOrder()
128137
{
138+
$storeId = 1;
129139
$cartId = 100;
130140
$agreements = [1, 2, 3];
131141
$this->scopeConfigMock
@@ -134,6 +144,9 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
134144
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
135145
->willReturn(true);
136146
$searchCriteriaMock = $this->createMock(SearchCriteria::class);
147+
$this->quoteMock
148+
->method('getIsMultiShipping')
149+
->willReturn(false);
137150
$this->quoteRepositoryMock
138151
->method('getActive')
139152
->with($cartId)
@@ -145,14 +158,14 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
145158
->method('getList')
146159
->with($searchCriteriaMock)
147160
->willReturn([1]);
148-
$this->extensionAttributesMock->setAgreementIds($agreements);
161+
$this->extensionAttributesMock->method('getAgreementIds')->willReturn($agreements);
149162
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
150163
$this->paymentMock->expects(static::atLeastOnce())
151164
->method('getExtensionAttributes')
152165
->willReturn($this->extensionAttributesMock);
153166
$this->storeEmulationMock->expects($this->once())
154167
->method('startEnvironmentEmulation')
155-
->with($this->anything());
168+
->with($storeId);
156169
$this->storeEmulationMock->expects($this->once())
157170
->method('stopEnvironmentEmulation');
158171
$this->model->beforeSavePaymentInformationAndPlaceOrder(
@@ -175,6 +188,9 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
175188
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
176189
->willReturn(true);
177190
$searchCriteriaMock = $this->createMock(SearchCriteria::class);
191+
$this->quoteMock
192+
->method('getIsMultiShipping')
193+
->willReturn(false);
178194
$this->quoteRepositoryMock
179195
->method('getActive')
180196
->with($cartId)
@@ -186,7 +202,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
186202
->method('getList')
187203
->with($searchCriteriaMock)
188204
->willReturn([1]);
189-
$this->extensionAttributesMock->setAgreementIds($agreements);
205+
$this->extensionAttributesMock->method('getAgreementIds')->willReturn($agreements);
190206
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
191207
$this->paymentMock->expects(static::atLeastOnce())
192208
->method('getExtensionAttributes')
@@ -208,10 +224,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
208224
}
209225

210226
/**
211-
* Build payment extension attributes stub.
227+
* Build payment extension mock.
228+
*
229+
* @return MockObject
212230
*/
213-
private function getPaymentExtension(): object
231+
private function getPaymentExtension(): PaymentExtensionInterface
214232
{
215-
return new PaymentExtensionAgreementIdsTestHelper();
233+
return $this->createPartialMock(
234+
PaymentExtensionTestHelper::class,
235+
['getAgreementIds', 'setAgreementIds']
236+
);
216237
}
217238
}

app/code/Magento/CheckoutAgreements/Test/Unit/Model/CheckoutAgreementsRepositoryTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\CheckoutAgreements\Test\Unit\Model;
99

1010
use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
11+
use Magento\CheckoutAgreements\Model\Agreement as AgreementModel;
12+
use Magento\CheckoutAgreements\Test\Unit\Helper\AgreementModelTestHelper;
1113
use Magento\CheckoutAgreements\Model\AgreementFactory;
1214
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1315
use Magento\CheckoutAgreements\Model\CheckoutAgreementsRepository;
@@ -22,7 +24,6 @@
2224
use Magento\Store\Model\StoreManagerInterface;
2325
use PHPUnit\Framework\MockObject\MockObject;
2426
use PHPUnit\Framework\TestCase;
25-
use Magento\CheckoutAgreements\Test\Unit\Helper\AgreementModelSetStoresTestHelper;
2627

2728
/**
2829
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -104,10 +105,10 @@ protected function setUp(): void
104105
AgreementFactory::class,
105106
['create']
106107
);
107-
$this->agreementMock = $this->getMockBuilder(AgreementModelSetStoresTestHelper::class)
108-
->onlyMethods(['addData', 'getData', 'getAgreementId', 'getId', 'setStores'])
109-
->disableOriginalConstructor()
110-
->getMock();
108+
$this->agreementMock = $this->createPartialMock(
109+
AgreementModelTestHelper::class,
110+
['setStores', 'addData', 'getData', 'getAgreementId', 'getId']
111+
);
111112
$this->storeMock = $this->createMock(Store::class);
112113
$this->extensionAttributesJoinProcessorMock = $this->createPartialMock(
113114
JoinProcessor::class,

app/code/Magento/Quote/Test/Unit/Helper/CartExtensionTestHelper.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
*/
1515
class CartExtensionTestHelper extends CartExtension
1616
{
17-
/**
18-
* @var array|null
19-
*/
20-
private $shippingAssignments = null;
2117
/**
2218
* Set shipping assignments for tests.
2319
*
@@ -26,7 +22,7 @@ class CartExtensionTestHelper extends CartExtension
2622
*/
2723
public function setShippingAssignments($shippingAssignments)
2824
{
29-
$this->shippingAssignments = $shippingAssignments;
25+
$this->setData('shipping_assignments', $shippingAssignments);
3026
return $this;
3127
}
3228

@@ -37,6 +33,6 @@ public function setShippingAssignments($shippingAssignments)
3733
*/
3834
public function getShippingAssignments()
3935
{
40-
return $this->shippingAssignments;
36+
return $this->getData('shipping_assignments');
4137
}
4238
}

app/code/Magento/Quote/Test/Unit/Helper/DataObjectTestHelper.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99

1010
use Magento\Framework\DataObject;
1111
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
12+
use Magento\Store\Api\Data\StoreInterface;
1213

1314
/**
1415
* Helper that extends DataObject for tests that need a product accessor.
1516
*/
16-
class DataObjectTestHelper extends DataObject
17+
class DataObjectTestHelper extends DataObject implements ContextExtensionInterface
1718
{
1819
/**
1920
* Get product from internal data storage for tests.
@@ -136,4 +137,67 @@ public function getStore()
136137
{
137138
return $this->getData('store');
138139
}
140+
141+
/**
142+
* Set store for tests.
143+
*
144+
* @param StoreInterface $store
145+
* @return $this
146+
*/
147+
public function setStore($store)
148+
{
149+
$this->setData('store', $store);
150+
return $this;
151+
}
152+
153+
/**
154+
* Get is customer flag for tests.
155+
*/
156+
public function getIsCustomer()
157+
{
158+
return (bool)$this->getData('is_customer');
159+
}
160+
161+
/**
162+
* Set is customer flag for tests.
163+
*/
164+
public function setIsCustomer($flag)
165+
{
166+
$this->setData('is_customer', $flag);
167+
return $this;
168+
}
169+
170+
/**
171+
* Get sales channel for tests.
172+
*/
173+
public function getSalesChannel()
174+
{
175+
return $this->getData('sales_channel');
176+
}
177+
178+
/**
179+
* Set sales channel for tests.
180+
*/
181+
public function setSalesChannel($salesChannel)
182+
{
183+
$this->setData('sales_channel', $salesChannel);
184+
return $this;
185+
}
186+
187+
/**
188+
* Get customer group id for tests.
189+
*/
190+
public function getCustomerGroupId()
191+
{
192+
return $this->getData('customer_group_id');
193+
}
194+
195+
/**
196+
* Set customer group id for tests.
197+
*/
198+
public function setCustomerGroupId($id)
199+
{
200+
$this->setData('customer_group_id', $id);
201+
return $this;
202+
}
139203
}

0 commit comments

Comments
 (0)