Skip to content

Commit da14049

Browse files
AC-15635: PHPUnit 12 Upgrade | Fix Unit Test Failure
1 parent 9a8de22 commit da14049

File tree

3 files changed

+85
-39
lines changed

3 files changed

+85
-39
lines changed

app/code/Magento/Catalog/Test/Unit/Helper/ProductTest.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
namespace Magento\Catalog\Test\Unit\Helper;
99

1010
use Magento\Catalog\Helper\Product;
11+
use Magento\Catalog\Model\Product as ProductModel;
1112
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use PHPUnit\Framework\Attributes\DataProvider;
1214
use PHPUnit\Framework\TestCase;
1315

1416
class ProductTest extends TestCase
@@ -34,8 +36,8 @@ protected function setUp(): void
3436
/**
3537
* @param mixed $data
3638
* @param boolean $result
37-
* @dataProvider getData
3839
*/
40+
#[DataProvider('getData')]
3941
public function testIsDataForPriceIndexerWasChanged($data, $result)
4042
{
4143
if (is_callable($data)) {
@@ -46,20 +48,14 @@ public function testIsDataForPriceIndexerWasChanged($data, $result)
4648

4749
protected function getMockForCatalogProduct($method)
4850
{
49-
$product = $this->getMockBuilder(
50-
Product::class
51-
)->disableOriginalConstructor()
52-
->getMock();
53-
if ($method!=null) {
54-
$product->expects(
55-
$this->once()
56-
)->method(
57-
$method
58-
)->with(
59-
'attribute'
60-
)->willReturn(
61-
true
62-
);
51+
if ($method != null) {
52+
$product = $this->createPartialMock(ProductModel::class, [$method]);
53+
$product->expects($this->once())
54+
->method($method)
55+
->with('attribute')
56+
->willReturn(true);
57+
} else {
58+
$product = $this->createMock(ProductModel::class);
6359
}
6460
return $product;
6561
}

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/EavTest.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
1414
use Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute;
1515
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory as EavAttributeFactory;
16-
use Magento\Store\Test\Unit\Helper\StoreInterfaceTestHelper;
1716
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav;
1817
use Magento\Eav\Api\Data\AttributeGroupInterface;
1918
use Magento\Eav\Model\Config;
19+
use Magento\Eav\Test\Unit\Helper\AttributeGroupInterfaceTestHelper;
2020
use Magento\Eav\Model\Entity\Attribute\Group;
2121
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
2222
use Magento\Eav\Model\Entity\Type as EntityType;
@@ -42,6 +42,7 @@
4242
use Magento\Ui\DataProvider\EavValidationRules;
4343
use Magento\Ui\DataProvider\Mapper\FormElement as FormElementMapper;
4444
use Magento\Ui\DataProvider\Mapper\MetaProperties as MetaPropertiesMapper;
45+
use PHPUnit\Framework\Attributes\DataProvider;
4546
use PHPUnit\Framework\MockObject\MockObject;
4647

4748
/**
@@ -130,6 +131,11 @@ class EavTest extends AbstractModifierTestCase
130131
* @var SearchCriteria|MockObject
131132
*/
132133
private $searchCriteriaMock;
134+
135+
/**
136+
* @var SearchResultsInterface|MockObject
137+
*/
138+
private $attributeGroupSearchResultsMock;
133139

134140
/**
135141
* @var SortOrderBuilder|MockObject
@@ -152,7 +158,7 @@ class EavTest extends AbstractModifierTestCase
152158
private $searchResultsMock;
153159

154160
/**
155-
* @var Attribute|MockObject
161+
* @var Attribute
156162
*/
157163
private $eavAttributeMock;
158164

@@ -172,7 +178,7 @@ class EavTest extends AbstractModifierTestCase
172178
protected $currencyLocaleMock;
173179

174180
/**
175-
* @var ProductAttributeInterface|MockObject
181+
* @var Attribute
176182
*/
177183
protected $productAttributeMock;
178184

@@ -229,23 +235,34 @@ protected function setUp(): void
229235
$this->metaPropertiesMapperMock = $this->createMock(MetaPropertiesMapper::class);
230236
$this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class);
231237
$this->attributeGroupRepositoryMock = $this->createMock(ProductAttributeGroupRepositoryInterface::class);
232-
$this->attributeGroupMock = $this->createMock(AttributeGroupInterface::class);
238+
$this->attributeGroupMock = $this->createPartialMock(
239+
AttributeGroupInterfaceTestHelper::class,
240+
['getAttributeGroupCode']
241+
);
233242
$this->attributeRepositoryMock = $this->createMock(ProductAttributeRepositoryInterface::class);
234-
$this->searchCriteriaMock = $this->createPartialMock(SearchCriteria::class, ['getItems']);
243+
$this->searchCriteriaMock = $this->createMock(SearchCriteria::class);
244+
$this->attributeGroupSearchResultsMock = $this->createMock(SearchResultsInterface::class);
235245
$this->sortOrderBuilderMock = $this->createMock(SortOrderBuilder::class);
236246
$this->searchResultsMock = $this->createMock(SearchResultsInterface::class);
237247
$this->eavAttributeMock = $this->createPartialMock(
238248
Attribute::class,
239249
[
240-
'load',
241250
'getApplyTo',
242251
'getFrontendInput',
243252
'getAttributeCode',
244253
'usesSource',
245254
'getSource',
246255
]
247256
);
248-
$this->productAttributeMock = $this->createMock(ProductAttributeInterface::class);
257+
$this->productAttributeMock = $this->createPartialMock(
258+
Attribute::class,
259+
[
260+
'getIsRequired',
261+
'getDefaultValue',
262+
'getAttributeCode',
263+
'getFrontendInput',
264+
]
265+
);
249266
$this->arrayManagerMock = $this->createMock(ArrayManager::class);
250267
$this->eavAttributeFactoryMock = $this->createPartialMock(
251268
EavAttributeFactory::class,
@@ -283,13 +300,13 @@ protected function setUp(): void
283300
$this->entityTypeMock->expects($this->any())
284301
->method('getAttributeCollection')
285302
->willReturn($this->attributeCollectionMock);
286-
$this->productMock->expects($this->any())
287-
->method('getAttributes')
288-
->willReturn([$this->attributeMock]);
289-
$this->storeMock = $this->createPartialMock(StoreInterfaceTestHelper::class, ['getId']);
290-
$this->eavAttributeMock->expects($this->any())
291-
->method('load')
292-
->willReturnSelf();
303+
$this->storeMock = $this->createMock(StoreInterface::class);
304+
$this->storeManagerMock->expects($this->any())
305+
->method('isSingleStoreMode')
306+
->willReturn(true);
307+
$this->attributeGroupMock->expects($this->any())
308+
->method('getAttributeGroupCode')
309+
->willReturn('product-details');
293310

294311
$this->eav =$this->getModel();
295312
}
@@ -347,15 +364,18 @@ public function testModifyData()
347364
->willReturn(4);
348365
$this->productMock->expects($this->once())->method('getData')
349366
->with(ProductAttributeInterface::CODE_PRICE)->willReturn('19.9900');
367+
$this->productMock->expects($this->any())
368+
->method('getAttributes')
369+
->willReturn([$this->attributeMock]);
350370

351371
$this->searchCriteriaBuilderMock->expects($this->any())->method('addFilter')
352372
->willReturnSelf();
353373
$this->searchCriteriaBuilderMock->expects($this->any())->method('create')
354374
->willReturn($this->searchCriteriaMock);
355-
$this->attributeGroupRepositoryMock->expects($this->any())->method('getList')
356-
->willReturn($this->searchCriteriaMock);
357-
$this->searchCriteriaMock->expects($this->once())->method('getItems')
375+
$this->attributeGroupSearchResultsMock->method('getItems')
358376
->willReturn([$this->attributeGroupMock]);
377+
$this->attributeGroupRepositoryMock->expects($this->any())->method('getList')
378+
->willReturn($this->attributeGroupSearchResultsMock);
359379
$this->sortOrderBuilderMock->expects($this->once())->method('setField')
360380
->willReturnSelf();
361381
$this->sortOrderBuilderMock->expects($this->once())->method('setAscendingDirection')
@@ -374,8 +394,6 @@ public function testModifyData()
374394
$this->attributeRepositoryMock->expects($this->once())->method('getList')
375395
->with($this->searchCriteriaMock)
376396
->willReturn($this->searchResultsMock);
377-
$this->eavAttributeMock->expects($this->any())->method('getAttributeGroupCode')
378-
->willReturn('product-details');
379397
$this->eavAttributeMock->expects($this->once())->method('getApplyTo')
380398
->willReturn([]);
381399
$this->eavAttributeMock->expects($this->once())->method('getFrontendInput')
@@ -398,8 +416,8 @@ public function testModifyData()
398416
* @param array $expectedCustomize
399417
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::isProductExists
400418
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::setupAttributeMeta
401-
* @dataProvider setupAttributeMetaDataProvider
402419
*/
420+
#[DataProvider('setupAttributeMetaDataProvider')]
403421
public function testSetupAttributeMetaDefaultAttribute(
404422
$productId,
405423
bool $productRequired,
@@ -432,17 +450,15 @@ public function testSetupAttributeMetaDefaultAttribute(
432450
$this->productAttributeMock->method('getIsRequired')->willReturn($productRequired);
433451
$this->productAttributeMock->method('getDefaultValue')->willReturn('required_value');
434452
$this->productAttributeMock->method('getAttributeCode')->willReturn('code');
435-
$this->productAttributeMock->method('getValue')->willReturn('value');
436453
$this->productAttributeMock->method('getFrontendInput')->willReturn($frontendInput);
437454

438-
$attributeMock = $this->createPartialMock(AttributeInterface::class, ['getValue']);
439-
455+
$attributeMock = $this->createStub(AttributeInterface::class);
440456
$attributeMock->method('getValue')->willReturn($attrValue);
441457

442458
$this->productMock->method('getCustomAttribute')->willReturn($attributeMock);
443459
$this->eavAttributeMock->method('usesSource')->willReturn(true);
444460

445-
$attributeSource = $this->createMock(SourceInterface::class);
461+
$attributeSource = $this->createStub(SourceInterface::class);
446462
$attributeSource->method('getAllOptions')->willReturn($attributeOptions);
447463

448464
$this->eavAttributeMock->method('getSource')->willReturn($attributeSource);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Adobe, Inc. All rights reserved.
4+
*/
5+
declare(strict_types=1);
6+
7+
namespace Magento\Eav\Test\Unit\Helper;
8+
9+
use Magento\Eav\Model\Entity\Attribute\Group;
10+
11+
/**
12+
* Test helper for AttributeGroupInterface providing extension attribute methods
13+
*/
14+
class AttributeGroupInterfaceTestHelper extends Group
15+
{
16+
private string $attributeGroupCode = '';
17+
18+
public function __construct()
19+
{
20+
// Skip parent constructor to avoid dependency injection issues
21+
}
22+
23+
public function getAttributeGroupCode()
24+
{
25+
return $this->attributeGroupCode;
26+
}
27+
28+
public function setAttributeGroupCode($value)
29+
{
30+
$this->attributeGroupCode = $value;
31+
return $this;
32+
}
33+
}
34+

0 commit comments

Comments
 (0)