Skip to content

Commit 486cbf1

Browse files
AC-12823:Investigate the unit test failure due to phpunit patch update during component upgrade-Fix unit tests
1 parent 5d1a851 commit 486cbf1

File tree

8 files changed

+151
-31
lines changed

8 files changed

+151
-31
lines changed

app/code/Magento/Bundle/Test/Unit/Model/Product/TypeTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,9 +1179,11 @@ function ($key) use ($optionCollection, $selectionCollection) {
11791179

11801180
$this->arrayUtility->expects($this->once())->method('flatten')->willReturn($bundleOptions);
11811181

1182-
$selectionCollection
1183-
->method('getItems')
1184-
->willReturnOnConsecutiveCalls([$selection], []);
1182+
$callCount = 0;
1183+
$selectionCollection->method('getItems')
1184+
->willReturnCallback(function () use (&$callCount, $selection) {
1185+
return $callCount++ === 0 ? [$selection] : [];
1186+
});
11851187
$selectionCollection
11861188
->method('getSize')
11871189
->willReturnOnConsecutiveCalls(1, 0);
@@ -1452,8 +1454,11 @@ function ($key) use ($optionCollection) {
14521454
$buyRequest->expects($this->once())
14531455
->method('getBundleOption')
14541456
->willReturn([3 => 5]);
1457+
$callCount = 0;
14551458
$option->method('getId')
1456-
->willReturnOnConsecutiveCalls(3);
1459+
->willReturnCallback(function () use (&$callCount) {
1460+
return $callCount++ === 0 ? 3 : '';
1461+
});
14571462
$option->expects($this->once())
14581463
->method('getRequired')
14591464
->willReturn(true);
@@ -1630,9 +1635,11 @@ public function testGetSkuWithoutType(): void
16301635
$selectionMock->expects(($this->any()))
16311636
->method('getItemByColumnValue')
16321637
->willReturn($selectionItemMock);
1633-
$selectionItemMock
1634-
->method('getEntityId')
1635-
->willReturnOnConsecutiveCalls(1);
1638+
$callCount = 0;
1639+
$selectionItemMock->method('getEntityId')
1640+
->willReturnCallback(function () use (&$callCount) {
1641+
return $callCount++ === 0 ? 1 : '';
1642+
});
16361643
$selectionItemMock->expects($this->once())
16371644
->method('getSku')
16381645
->willReturn($itemSku);

app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/BundleTest.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Framework\EntityManager\MetadataPool;
2323
use Magento\ImportExport\Model\Import;
2424
use Magento\ImportExport\Test\Unit\Model\Import\AbstractImportTestCase;
25+
use Magento\Store\Model\StoreManagerInterface;
2526
use PHPUnit\Framework\MockObject\MockObject;
2627

2728
/**
@@ -208,6 +209,19 @@ protected function setUp(): void
208209
->disableOriginalConstructor()
209210
->onlyMethods(['getScope'])
210211
->getMockForAbstractClass();
212+
213+
$objects = [
214+
[
215+
Bundle\RelationsDataSaver::class,
216+
$this->createMock(Bundle\RelationsDataSaver::class)
217+
],
218+
[
219+
StoreManagerInterface::class,
220+
$this->createMock(StoreManagerInterface::class)
221+
]
222+
];
223+
$this->objectManagerHelper->prepareObjectManager($objects);
224+
211225
$this->bundle = $this->objectManagerHelper->getObject(
212226
Bundle::class,
213227
[
@@ -248,9 +262,12 @@ public function testSaveData(array $skus, array $bunch, bool $allowImport): void
248262
{
249263
$this->entityModel->expects($this->any())->method('getBehavior')->willReturn(Import::BEHAVIOR_APPEND);
250264
$this->entityModel->expects($this->once())->method('getNewSku')->willReturn($skus['newSku']);
265+
$callCount = 0;
251266
$this->entityModel
252267
->method('getNextBunch')
253-
->willReturnOnConsecutiveCalls([$bunch]);
268+
->willReturnCallback(function () use (&$callCount, $bunch) {
269+
return $callCount++ === 0 ? [$bunch] : null;
270+
});
254271
$this->entityModel->expects($this->any())->method('isRowAllowedToImport')->willReturn($allowImport);
255272
$scope = $this->getMockBuilder(ScopeInterface::class)->getMockForAbstractClass();
256273
$this->scopeResolver->expects($this->any())->method('getScope')->willReturn($scope);
@@ -321,7 +338,7 @@ public function testSaveData(array $skus, array $bunch, bool $allowImport): void
321338
*
322339
* @return array
323340
*/
324-
public function saveDataProvider(): array
341+
public static function saveDataProvider(): array
325342
{
326343
return [
327344
[
@@ -395,13 +412,12 @@ public function testSaveDataDelete(): void
395412
$this->entityModel->expects($this->once())->method('getNewSku')->willReturn([
396413
'sku' => ['sku' => 'sku', 'entity_id' => 3, 'attr_set_code' => 'Default', 'type_id' => 'bundle']
397414
]);
415+
$callCount = 0;
398416
$this->entityModel
399417
->method('getNextBunch')
400-
->willReturnOnConsecutiveCalls(
401-
[
402-
['bundle_values' => 'value1', 'sku' => 'sku', 'name' => 'name']
403-
]
404-
);
418+
->willReturnCallback(function () use (&$callCount) {
419+
return $callCount++ === 0 ? [['bundle_values' => 'value1', 'sku' => 'sku', 'name' => 'name']] : null;
420+
});
405421
$this->entityModel->expects($this->any())->method('isRowAllowedToImport')->willReturn(true);
406422
$select = $this->createMock(Select::class);
407423
$this->connection->expects($this->any())->method('select')->willReturn($select);

app/code/Magento/Catalog/Test/Unit/Model/CategoryRepositoryTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ protected function setUp(): void
109109
->disableOriginalConstructor()
110110
->getMock();
111111

112+
$objectHelper = new ObjectManager($this);
113+
$objects = [
114+
[
115+
PopulateWithValues::class,
116+
$this->createMock(PopulateWithValues::class)
117+
]
118+
];
119+
$objectHelper->prepareObjectManager($objects);
120+
112121
$this->model = (new ObjectManager($this))->getObject(
113122
CategoryRepository::class,
114123
[
@@ -171,7 +180,7 @@ public function testGetWhenCategoryDoesNotExist()
171180
/**
172181
* @return array
173182
*/
174-
public function filterExtraFieldsOnUpdateCategoryDataProvider()
183+
public static function filterExtraFieldsOnUpdateCategoryDataProvider()
175184
{
176185
return [
177186
[
@@ -239,8 +248,11 @@ public function testCreateNewCategory()
239248
->willReturn($categoryData);
240249
$categoryMock = $this->createMock(CategoryModel::class);
241250
$parentCategoryMock = $this->createMock(CategoryModel::class);
251+
$callCount = 0;
242252
$categoryMock->expects($this->any())->method('getId')
243-
->will($this->onConsecutiveCalls($categoryId, $newCategoryId));
253+
->willReturnCallback(function() use (&$callCount, $categoryId, $newCategoryId) {
254+
return $callCount++ === 0 ? $categoryId : $newCategoryId;
255+
});
244256
$this->categoryFactoryMock->expects($this->exactly(2))->method('create')->willReturn($parentCategoryMock);
245257
$parentCategoryMock->expects($this->atLeastOnce())->method('getId')->willReturn($parentCategoryId);
246258

@@ -312,7 +324,7 @@ public function testSaveWithValidateCategoryException($error, $expectedException
312324
/**
313325
* @return array
314326
*/
315-
public function saveWithValidateCategoryExceptionDataProvider()
327+
public static function saveWithValidateCategoryExceptionDataProvider()
316328
{
317329
return [
318330
[

app/code/Magento/CatalogInventory/Test/Unit/Model/Quote/Item/QuantityValidator/Initializer/QuantityValidatorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public function testValidateWithOptionsAndError(int $quantity, int $productStatu
372372
/**
373373
* @return array
374374
*/
375-
public function validateWithOptionsDataProvider(): array
375+
public static function validateWithOptionsDataProvider(): array
376376
{
377377
return [
378378
'when product is enabled and in stock' =>
@@ -457,9 +457,11 @@ public function testRemoveError(): void
457457
$this->stockRegistryMock
458458
->method('getStockItem')
459459
->willReturnOnConsecutiveCalls($this->stockItemMock);
460-
$this->stockRegistryMock
461-
->method('getStockStatus')
462-
->willReturnOnConsecutiveCalls($this->stockStatusMock);
460+
$callCount = 0;
461+
$this->stockRegistryMock->method('getStockStatus')
462+
->willReturnCallback(function () use (&$callCount) {
463+
return $callCount++ === 0 ? $this->stockStatusMock : null;
464+
});
463465
$this->quoteItemMock->expects($this->any())
464466
->method('getParentItem')
465467
->willReturn($this->parentItemMock);

app/code/Magento/Customer/Test/Unit/Model/AccountManagementTest.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Customer\Test\Unit\Model;
99

10+
use Magento\Customer\Api\AccountManagementInterface;
1011
use Magento\Customer\Api\AddressRepositoryInterface;
1112
use Magento\Customer\Api\CustomerMetadataInterface;
1213
use Magento\Customer\Api\CustomerRepositoryInterface;
@@ -29,9 +30,12 @@
2930
use Magento\Customer\Model\Metadata\Validator;
3031
use Magento\Customer\Model\ResourceModel\Visitor\CollectionFactory;
3132
use Magento\Directory\Model\AllowedCountries;
33+
use Magento\Email\Model\ResourceModel\Template\CollectionFactory as TemplateCollectionFactory;
34+
use Magento\Framework\Api\DataObjectHelper;
3235
use Magento\Framework\Api\ExtensibleDataObjectConverter;
3336
use Magento\Framework\Api\SearchCriteriaBuilder;
3437
use Magento\Framework\App\Area;
38+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
3539
use Magento\Framework\App\Config\ScopeConfigInterface;
3640
use Magento\Framework\DataObjectFactory;
3741
use Magento\Framework\Encryption\EncryptorInterface;
@@ -279,6 +283,7 @@ class AccountManagementTest extends TestCase
279283
*/
280284
protected function setUp(): void
281285
{
286+
282287
$this->customerFactory = $this->createPartialMock(CustomerFactory::class, ['create']);
283288
$this->manager = $this->getMockForAbstractClass(ManagerInterface::class);
284289
$this->store = $this->getMockBuilder(Store::class)
@@ -342,6 +347,53 @@ protected function setUp(): void
342347
->getMockForAbstractClass();
343348

344349
$this->objectManagerHelper = new ObjectManagerHelper($this);
350+
$objects = [
351+
[
352+
AccountManagementInterface::class,
353+
$this->createMock(AccountManagementInterface::class)
354+
],
355+
[
356+
CustomerInterfaceFactory::class,
357+
$this->createMock(CustomerInterfaceFactory::class)
358+
],
359+
[
360+
DataObjectHelper::class,
361+
$this->createMock(DataObjectHelper::class)
362+
],
363+
[
364+
StoreManagerInterface::class,
365+
$this->createMock(StoreManagerInterface::class)
366+
],
367+
[
368+
CustomerRepositoryInterface::class,
369+
$this->createMock(CustomerRepositoryInterface::class)
370+
],
371+
[
372+
ExtensibleDataObjectConverter::class,
373+
$this->createMock(ExtensibleDataObjectConverter::class)
374+
],
375+
[
376+
CustomerFactory::class,
377+
$this->createMock(CustomerFactory::class)
378+
],
379+
[
380+
Random::class,
381+
$this->createMock(Random::class)
382+
],
383+
[
384+
EncryptorInterface::class,
385+
$this->createMock(EncryptorInterface::class)
386+
],
387+
[
388+
MutableScopeConfigInterface::class,
389+
$this->createMock(MutableScopeConfigInterface::class)
390+
],
391+
[
392+
TemplateCollectionFactory::class,
393+
$this->createMock(TemplateCollectionFactory::class)
394+
],
395+
];
396+
$this->objectManagerHelper->prepareObjectManager($objects);
345397
$this->accountManagement = $this->objectManagerHelper->getObject(
346398
AccountManagement::class,
347399
[
@@ -648,9 +700,12 @@ public function testCreateAccountWithPasswordHashWithAddressException(): void
648700
$customer->expects($this->atLeastOnce())
649701
->method('getWebsiteId')
650702
->willReturn($websiteId);
703+
$callCount = 0;
651704
$customer
652705
->method('getStoreId')
653-
->willReturnOnConsecutiveCalls(null, null, 1);
706+
->willReturnCallback(function () use (&$callCount) {
707+
return $callCount++ < 2 ? null : 1;
708+
});
654709
$customer->expects($this->once())
655710
->method('setStoreId')
656711
->with($defaultStoreId);

app/code/Magento/Customer/Test/Unit/Model/FileProcessorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,15 @@ public function testMoveTemporaryFileNewFileName(): void
477477
$mockRead = $this->createMock(ReadInterface::class);
478478
$objectManagerMock->method('get')->willReturn($mockFileSystem);
479479
$mockFileSystem->method('getDirectoryRead')->willReturn($mockRead);
480-
$mockRead->method('isExist')->willReturnOnConsecutiveCalls(true, true, false);
480+
$callCount = 0;
481+
$mockRead->method('isExist')
482+
->willReturnCallback(function () use (&$callCount) {
483+
$callCount++;
484+
if ($callCount === 1 || $callCount === 2) {
485+
return true;
486+
}
487+
return false;
488+
});
481489
ObjectManager::setInstance($objectManagerMock);
482490

483491
$this->mediaDirectory->expects($this->once())

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/FileTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ public function testExtractValueNoRequestScope($expected, $attributeCode = '', $
118118
{
119119
$value = 'value';
120120

121-
$this->requestMock
122-
->method('getParam')
123-
->willReturnOnConsecutiveCalls($this->returnValue(['delete' => $delete]));
121+
$callCount = 0;
122+
$this->requestMock->method('getParam')
123+
->willReturnCallback(function () use (&$callCount, $delete) {
124+
return $callCount++ === 0 ? ['delete' => $delete] : '';
125+
});
124126

125127
$this->attributeMetadataMock->expects(
126128
$this->any()

app/code/Magento/GroupedImportExport/Test/Unit/Model/Import/Product/Type/GroupedTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Catalog\Api\Data\ProductInterface;
1212
use Magento\CatalogImportExport\Model\Import\Product;
13+
use Magento\CatalogImportExport\Model\Import\Product\SkuStorage;
1314
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
1415
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
1516
use Magento\Framework\App\ResourceConnection;
@@ -172,6 +173,17 @@ protected function setUp(): void
172173
);
173174
$this->resource->expects($this->any())->method('getConnection')->willReturn($this->connection);
174175
$this->resource->expects($this->any())->method('getTableName')->willReturn('tableName');
176+
$objects = [
177+
[
178+
ConfigInterface::class,
179+
$this->createMock(ConfigInterface::class)
180+
],
181+
[
182+
SkuStorage::class,
183+
$this->createMock(SkuStorage::class)
184+
]
185+
];
186+
$this->objectManagerHelper->prepareObjectManager($objects);
175187
$this->grouped = $this->objectManagerHelper->getObject(
176188
Grouped::class,
177189
[
@@ -231,9 +243,12 @@ public function testSaveData($skus, $bunch): void
231243
$attributes = ['position' => ['id' => 0], 'qty' => ['id' => 0]];
232244
$this->links->expects($this->once())->method('getAttributes')->willReturn($attributes);
233245

246+
$callCount = 0;
234247
$this->entityModel
235248
->method('getNextBunch')
236-
->willReturnOnConsecutiveCalls([$bunch]);
249+
->willReturnCallback(function () use (&$callCount, $bunch) {
250+
return $callCount++ === 0 ? [$bunch] : null;
251+
});
237252
$this->entityModel->expects($this->any())->method('isRowAllowedToImport')->willReturn(true);
238253
$this->entityModel->expects($this->any())->method('getRowScope')->willReturn(Product::SCOPE_DEFAULT);
239254

@@ -246,7 +261,7 @@ public function testSaveData($skus, $bunch): void
246261
*
247262
* @return array
248263
*/
249-
public function saveDataProvider(): array
264+
public static function saveDataProvider(): array
250265
{
251266
return [
252267
[
@@ -342,9 +357,12 @@ public function testSaveDataScopeStore(): void
342357
]
343358
];
344359
$this->entityModel->expects($this->any())->method('isRowAllowedToImport')->willReturn(true);
360+
$callCount = 0;
345361
$this->entityModel
346362
->method('getNextBunch')
347-
->willReturnOnConsecutiveCalls($bunch);
363+
->willReturnCallback(function () use (&$callCount, $bunch) {
364+
return $callCount++ === 0 ? $bunch : null;
365+
});
348366
$this->entityModel
349367
->method('getRowScope')
350368
->willReturnOnConsecutiveCalls(Product::SCOPE_DEFAULT, Product::SCOPE_STORE);
@@ -379,11 +397,11 @@ public function testSaveDataAssociatedComposite(): void
379397
];
380398

381399
$this->entityModel->expects($this->any())->method('isRowAllowedToImport')->willReturn(true);
400+
$callCount = 0;
382401
$this->entityModel
383402
->method('getNextBunch')
384403
->willReturnCallback(function () use (&$callCount, $bunch) {
385-
$callCount++;
386-
return $bunch[$callCount - 1] ?? null;
404+
return $callCount++ === 0 ? $bunch : null;
387405
});
388406
$this->entityModel
389407
->method('getRowScope')

0 commit comments

Comments
 (0)