Skip to content

Commit 86000ce

Browse files
committed
repair \Magento\Captcha\Test\Unit\Model\Customer\Plugin\AjaxLoginTest
Data providers are executed before setUp. In previous phpunit versions $this->formIds[0] would just convert to null, explaining why the logAttempt method was never called. Fixing this issue leads to different expectations of the test result in testAroundExecuteCaptchaIsNotRequired. I have adjusted the necessary. No need to test if this method returns nothing if the return type is already void Fix quote unit tests by explicitely declaring methods that are vital to quote operations. I think it is better to declare these methods explicitely than to depend on DataObject methods Update extensionattribute mock repair partial mock with non-existing method mocking Revert "Fix quote unit tests by explicitely declaring methods that are vital" This reverts commit e29506ce3b353d55cbe64d064e863c4969c3d6cd. fix mock object with non existing method mocks replace partial mocks with non-existing method mocking
1 parent f3a160c commit 86000ce

File tree

6 files changed

+56
-41
lines changed

6 files changed

+56
-41
lines changed

app/code/Magento/Captcha/Test/Unit/Model/Customer/Plugin/AjaxLoginTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AjaxLoginTest extends TestCase
6363
/**
6464
* @var array
6565
*/
66-
protected $formIds;
66+
protected $formIds = ['user_login'];
6767

6868
/**
6969
* @var AjaxLogin
@@ -97,7 +97,6 @@ protected function setUp(): void
9797
->method('getCaptcha')
9898
->willReturn($this->captchaMock);
9999

100-
$this->formIds = ['user_login'];
101100
$this->serializerMock = $this->createMock(Json::class);
102101

103102
$this->model = new AjaxLogin(
@@ -194,7 +193,10 @@ public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent
194193

195194
$this->captchaMock->expects($this->once())->method('isRequired')->with($username)
196195
->willReturn(false);
197-
$this->captchaMock->expects($this->never())->method('logAttempt')->with($username);
196+
$expectLogAttempt = $requestContent['captcha_form_id'] ?? false;
197+
$this->captchaMock
198+
->expects($expectLogAttempt ? $this->once() : $this->never())
199+
->method('logAttempt')->with($username);
198200
$this->captchaMock->expects($this->never())->method('isCorrect');
199201

200202
$closure = function () {

app/code/Magento/Developer/Test/Unit/Console/Command/XmlCatalogGenerateCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testExecuteVsCodeFormat()
9797
->with(
9898
$this->equalTo(['urn:magento:framework:Module/etc/module.xsd' => $fixtureXmlFile]),
9999
$this->equalTo('test')
100-
)->willReturn(null);
100+
);
101101

102102
$formats = ['vscode' => $vscodeFormatMock];
103103
$readFactory = $this->createMock(ReadFactory::class);

app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,10 @@ public function testSetGuest()
228228
->method('removePersistentCookie')->willReturn($this->sessionMock);
229229
$this->quoteMock->expects($this->once())->method('isVirtual')->willReturn(false);
230230
$this->quoteMock->expects($this->once())->method('getItemsQty')->willReturn(1);
231-
$extensionAttributes = $this->createPartialMock(
232-
CartExtensionInterface::class,
233-
[
234-
'setShippingAssignments',
235-
'getShippingAssignments'
236-
]
237-
);
231+
$extensionAttributes = $this->getMockBuilder(CartExtensionInterface::class)
232+
->addMethods(['getShippingAssignments', 'setShippingAssignments'])
233+
->disableArgumentCloning()
234+
->getMockForAbstractClass();
238235
$shippingAssignment = $this->createMock(ShippingAssignmentInterface::class);
239236
$extensionAttributes->expects($this->once())
240237
->method('setShippingAssignments')

app/code/Magento/Quote/Test/Unit/Model/Quote/Address/Total/SubtotalTest.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function setUp(): void
5959

6060
$this->stockRegistry = $this->createPartialMock(
6161
StockRegistry::class,
62-
['getStockItem', '__wakeup']
62+
['getStockItem']
6363
);
6464
$this->stockItemMock = $this->createPartialMock(
6565
\Magento\CatalogInventory\Model\Stock\Item::class,
@@ -110,10 +110,14 @@ public function testCollect($price, $originalPrice, $itemHasParent, $expectedPri
110110
]
111111
);
112112
/** @var Address|MockObject $address */
113-
$address = $this->createPartialMock(
114-
Address::class,
115-
['setTotalQty', 'getTotalQty', 'removeItem', 'getQuote']
116-
);
113+
$address = $this->getMockBuilder(Address::class)
114+
->disableOriginalConstructor()
115+
->disableOriginalClone()
116+
->disableArgumentCloning()
117+
->disallowMockingUnknownTypes()
118+
->onlyMethods(['removeItem', 'getQuote'])
119+
->addMethods(['setTotalQty', 'getTotalQty'])
120+
->getMock();
117121

118122
/** @var Product|MockObject $product */
119123
$product = $this->createMock(Product::class);
@@ -161,10 +165,13 @@ public function testCollect($price, $originalPrice, $itemHasParent, $expectedPri
161165
$shippingAssignmentMock->expects($this->exactly(2))->method('getShipping')->willReturn($shipping);
162166
$shippingAssignmentMock->expects($this->once())->method('getItems')->willReturn([$quoteItem]);
163167

164-
$total = $this->createPartialMock(
165-
Total::class,
166-
['setBaseVirtualAmount', 'setVirtualAmount']
167-
);
168+
$total = $this->getMockBuilder(Total::class)
169+
->disableOriginalConstructor()
170+
->disableOriginalClone()
171+
->disableArgumentCloning()
172+
->disallowMockingUnknownTypes()
173+
->addMethods(['setVirtualAmount', 'setBaseVirtualAmount'])
174+
->getMock();
168175
$total->expects($this->once())->method('setBaseVirtualAmount')->willReturnSelf();
169176
$total->expects($this->once())->method('setVirtualAmount')->willReturnSelf();
170177

@@ -185,7 +192,10 @@ public function testFetch()
185192
];
186193

187194
$quoteMock = $this->createMock(Quote::class);
188-
$totalMock = $this->createPartialMock(Total::class, ['getSubtotal']);
195+
$totalMock = $this->getMockBuilder(Total::class)
196+
->addMethods(['getSubtotal'])
197+
->disableArgumentCloning()
198+
->getMockForAbstractClass();
189199
$totalMock->expects($this->once())->method('getSubtotal')->willReturn(100);
190200

191201
$this->assertEquals($expectedResult, $this->subtotalModel->fetch($quoteMock, $totalMock));
@@ -229,13 +239,14 @@ public function testCollectWithInvalidItems()
229239
$address->expects($this->once())
230240
->method('removeItem')
231241
->with($addressItemId);
232-
$addressItem = $this->createPartialMock(
233-
AddressItem::class,
234-
[
235-
'getId',
236-
'getQuoteItemId'
237-
]
238-
);
242+
$addressItem = $this->getMockBuilder(AddressItem::class)
243+
->disableOriginalConstructor()
244+
->disableOriginalClone()
245+
->disableArgumentCloning()
246+
->disallowMockingUnknownTypes()
247+
->onlyMethods(['getId'])
248+
->addMethods(['getQuoteItemId'])
249+
->getMock();
239250
$addressItem->setAddress($address);
240251
$addressItem->method('getId')
241252
->willReturn($addressItemId);

app/code/Magento/Sales/Test/Unit/Model/Order/CreditmemoFactoryTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ class CreditmemoFactoryTest extends TestCase
4949
*/
5050
protected function setUp(): void
5151
{
52-
$this->orderItemMock = $this->createPartialMock(
53-
Item::class,
54-
['getChildrenItems', 'isDummy', 'getHasChildren', 'getId', 'getParentItemId']
55-
);
52+
$this->orderItemMock = $this->getMockBuilder(Item::class)
53+
->disableOriginalConstructor()
54+
->disableOriginalClone()
55+
->disableArgumentCloning()
56+
->disallowMockingUnknownTypes()
57+
->onlyMethods(['getChildrenItems', 'isDummy', 'getId', 'getParentItemId'])
58+
->addMethods(['getHasChildren'])
59+
->getMock();
5660
$this->orderChildItemOneMock = $this->createPartialMock(
5761
Item::class,
5862
['getQtyToRefund', 'getId']

lib/internal/Magento/Framework/Pricing/Test/Unit/Adjustment/CollectionTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function setUp(): void
5050
'adj3' => $adj3,
5151
'adj4' => $adj4,
5252
];
53+
$this->adjustmentsData = $adjustmentsData;
5354

5455
/** @var Pool|MockObject $adjustmentPool */
5556
$adjustmentPool = $this->getMockBuilder(Pool::class)
@@ -64,14 +65,13 @@ function ($code) use ($adjustmentsData) {
6465
return $adjustmentsData[$code];
6566
}
6667
);
67-
6868
$this->adjustmentPool = $adjustmentPool;
69-
$this->adjustmentsData = $adjustmentsData;
7069
}
7170

7271
/**
7372
* @param string[] $adjustments
7473
* @param string[] $expectedResult
74+
*
7575
* @dataProvider getItemsDataProvider
7676
*/
7777
public function testGetItems($adjustments, $expectedResult)
@@ -92,14 +92,15 @@ public function getItemsDataProvider()
9292
[['adj1'], ['adj1']],
9393
[['adj4'], ['adj4']],
9494
[['adj1', 'adj4'], ['adj1', 'adj4']],
95-
[['adj1', 'adj2', 'adj3', 'adj4'], ['adj3', 'adj1', 'adj2', 'adj4']]
95+
[['adj1', 'adj2', 'adj3', 'adj4'], ['adj3', 'adj1', 'adj2', 'adj4']],
9696
];
9797
}
9898

9999
/**
100100
* @param string[] $adjustments
101101
* @param string $code
102102
* @param $expectedResult
103+
*
103104
* @dataProvider getItemByCodeDataProvider
104105
*/
105106
public function testGetItemByCode($adjustments, $code, $expectedResult)
@@ -108,7 +109,7 @@ public function testGetItemByCode($adjustments, $code, $expectedResult)
108109

109110
$item = $collection->getItemByCode($code);
110111

111-
$this->assertEquals($expectedResult, $item->getAdjustmentCode());
112+
$this->assertEquals($expectedResult, $item->getSortOrder());
112113
}
113114

114115
/**
@@ -117,11 +118,11 @@ public function testGetItemByCode($adjustments, $code, $expectedResult)
117118
public function getItemByCodeDataProvider()
118119
{
119120
return [
120-
[['adj1'], 'adj1', $this->adjustmentsData['adj1']],
121-
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj1', $this->adjustmentsData['adj1']],
122-
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj2', $this->adjustmentsData['adj2']],
123-
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj3', $this->adjustmentsData['adj3']],
124-
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj4', $this->adjustmentsData['adj4']],
121+
[['adj1'], 'adj1', 10],
122+
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj1', 10],
123+
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj2', 20],
124+
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj3', 5],
125+
[['adj1', 'adj2', 'adj3', 'adj4'], 'adj4', Pool::DEFAULT_SORT_ORDER],
125126
];
126127
}
127128

0 commit comments

Comments
 (0)