Skip to content

Commit 74466ba

Browse files
Merge branch '2.4-develop' into spartans_pr_11112025
2 parents 7c2fe61 + d9a883c commit 74466ba

File tree

338 files changed

+4217
-5396
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

338 files changed

+4217
-5396
lines changed

app/code/Magento/AdvancedPricingImportExport/Test/Unit/Model/Export/AdvancedPricingTest.php

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@
1414
use Magento\Catalog\Model\ResourceModel\ProductFactory;
1515
use Magento\CatalogImportExport\Model\Export\Product;
1616
use Magento\CatalogImportExport\Model\Export\Product\Type\Factory;
17+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
18+
use Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory as OptionCollectionFactory;
1719
use Magento\CatalogImportExport\Model\Export\RowCustomizer\Composite;
1820
use Magento\CatalogImportExport\Model\Import\Product\StoreResolver;
1921
use Magento\CatalogInventory\Model\ResourceModel\Stock\ItemFactory;
2022
use Magento\Customer\Api\GroupRepositoryInterface;
2123
use Magento\Eav\Model\Config;
24+
use Magento\ImportExport\Model\Export\Config as ExportConfig;
2225
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
2326
use Magento\Eav\Model\Entity\Type;
2427
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
2528
use Magento\Framework\App\ResourceConnection;
2629
use Magento\Framework\Logger\Monolog;
2730
use Magento\Framework\Stdlib\DateTime\Timezone;
31+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
2832
use Magento\ImportExport\Model\Export\Adapter\AbstractAdapter;
2933
use Magento\ImportExport\Model\Export\ConfigInterface;
3034
use Magento\Store\Model\Store;
@@ -39,6 +43,8 @@
3943
*/
4044
class AdvancedPricingTest extends TestCase
4145
{
46+
use MockCreationTrait;
47+
4248
/**
4349
* @var Timezone|MockObject
4450
*/
@@ -157,18 +163,13 @@ protected function setUp(): void
157163
$this->localeDate = $this->createMock(Timezone::class);
158164
$this->config = $this->createPartialMock(Config::class, ['getEntityType']);
159165
$type = $this->createMock(Type::class);
160-
$this->config->expects($this->once())->method('getEntityType')->willReturn($type);
166+
$this->config->method('getEntityType')->willReturn($type);
161167
$this->resource = $this->createMock(ResourceConnection::class);
162168
$this->storeManager = $this->createMock(StoreManager::class);
163169
$this->logger = $this->createMock(Monolog::class);
164170
$this->collection = $this->createMock(CollectionFactory::class);
165-
$this->abstractCollection = $this->getMockForAbstractClass(
171+
$this->abstractCollection = $this->createPartialMockWithReflection(
166172
AbstractCollection::class,
167-
[],
168-
'',
169-
false,
170-
true,
171-
true,
172173
[
173174
'count',
174175
'setOrder',
@@ -177,29 +178,22 @@ protected function setUp(): void
177178
'getLastPageNumber',
178179
]
179180
);
180-
$this->exportConfig = $this->createMock(\Magento\ImportExport\Model\Export\Config::class);
181-
$this->productFactory = $this->getMockBuilder(ProductFactory::class)
182-
->addMethods(['getTypeId'])
183-
->onlyMethods(['create'])
184-
->disableOriginalConstructor()
185-
->getMock();
186-
$this->attrSetColFactory = $this->getMockBuilder(AttributeSetCollectionFactory::class)
187-
->addMethods(['setEntityTypeFilter'])
188-
->onlyMethods(['create'])
189-
->disableOriginalConstructor()
190-
->getMock();
191-
$this->categoryColFactory = $this->getMockBuilder(CategoryCollectionFactory::class)
192-
->addMethods(['addNameToResult'])
193-
->onlyMethods(['create'])
194-
->disableOriginalConstructor()
195-
->getMock();
196-
$this->itemFactory = $this->createMock(ItemFactory::class);
197-
$this->optionColFactory = $this->createMock(
198-
\Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory::class
181+
$this->exportConfig = $this->createMock(ExportConfig::class);
182+
$this->productFactory = $this->createPartialMockWithReflection(
183+
ProductFactory::class,
184+
['getTypeId', 'create']
185+
);
186+
$this->attrSetColFactory = $this->createPartialMockWithReflection(
187+
AttributeSetCollectionFactory::class,
188+
['setEntityTypeFilter', 'create']
199189
);
200-
$this->attributeColFactory = $this->createMock(
201-
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory::class
190+
$this->categoryColFactory = $this->createPartialMockWithReflection(
191+
CategoryCollectionFactory::class,
192+
['addNameToResult', 'create']
202193
);
194+
$this->itemFactory = $this->createMock(ItemFactory::class);
195+
$this->optionColFactory = $this->createMock(OptionCollectionFactory::class);
196+
$this->attributeColFactory = $this->createMock(AttributeCollectionFactory::class);
203197
$this->typeFactory = $this->createMock(Factory::class);
204198
$this->linkTypeProvider = $this->createMock(LinkTypeProvider::class);
205199
$this->rowCustomizer = $this->createMock(
@@ -208,7 +202,7 @@ protected function setUp(): void
208202
$this->storeResolver = $this->createMock(
209203
StoreResolver::class
210204
);
211-
$this->groupRepository = $this->getMockForAbstractClass(GroupRepositoryInterface::class);
205+
$this->groupRepository = $this->createMock(GroupRepositoryInterface::class);
212206
$this->writer = $this->createPartialMock(
213207
AbstractAdapter::class,
214208
[
@@ -242,36 +236,31 @@ protected function setUp(): void
242236
'_getCustomerGroupById',
243237
'correctExportData'
244238
]);
245-
$this->advancedPricing = $this->getMockBuilder(
246-
AdvancedPricing::class
247-
)
248-
->addMethods($mockAddMethods)
249-
->onlyMethods($mockMethods)
250-
->disableOriginalConstructor()
251-
->getMock();
239+
$this->advancedPricing = $this->createPartialMockWithReflection(
240+
AdvancedPricing::class,
241+
array_merge($mockAddMethods, $mockMethods)
242+
);
243+
244+
// Configure constructor methods to return self
252245
foreach ($constructorMethods as $method) {
253-
$this->advancedPricing->expects($this->once())->method($method)->willReturnSelf();
246+
$this->advancedPricing->method($method)->willReturnSelf();
254247
}
255-
$this->advancedPricing->__construct(
256-
$this->localeDate,
257-
$this->config,
258-
$this->resource,
259-
$this->storeManager,
260-
$this->logger,
261-
$this->collection,
262-
$this->exportConfig,
263-
$this->productFactory,
264-
$this->attrSetColFactory,
265-
$this->categoryColFactory,
266-
$this->itemFactory,
267-
$this->optionColFactory,
268-
$this->attributeColFactory,
269-
$this->typeFactory,
270-
$this->linkTypeProvider,
271-
$this->rowCustomizer,
272-
$this->storeResolver,
273-
$this->groupRepository
274-
);
248+
249+
// Set properties directly using reflection instead of calling constructor
250+
// to avoid parent constructor that needs ObjectManager
251+
$reflection = new \ReflectionClass($this->advancedPricing);
252+
253+
$storeResolverProperty = $reflection->getProperty('_storeResolver');
254+
$storeResolverProperty->setAccessible(true);
255+
$storeResolverProperty->setValue($this->advancedPricing, $this->storeResolver);
256+
257+
$groupRepositoryProperty = $reflection->getProperty('_groupRepository');
258+
$groupRepositoryProperty->setAccessible(true);
259+
$groupRepositoryProperty->setValue($this->advancedPricing, $this->groupRepository);
260+
261+
$resourceProperty = $reflection->getProperty('_resource');
262+
$resourceProperty->setAccessible(true);
263+
$resourceProperty->setValue($this->advancedPricing, $this->resource);
275264
}
276265

277266
/**

app/code/Magento/AdvancedPricingImportExport/Test/Unit/Model/Import/AdvancedPricing/Validator/TierPriceTest.php

Lines changed: 30 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\AdvancedPricingImportExport\Test\Unit\Model\Import\AdvancedPricing\Validator;
99

10+
use PHPUnit\Framework\Attributes\DataProvider;
1011
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing as AdvancedPricing;
1112
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing\Validator;
1213
use Magento\AdvancedPricingImportExport\Model\Import\AdvancedPricing\Validator\TierPrice;
@@ -21,9 +22,13 @@
2122
use Magento\Framework\App\ResourceConnection;
2223
use Magento\Framework\Json\Helper\Data;
2324
use Magento\Framework\Stdlib\StringUtils;
25+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
26+
use Magento\ImportExport\Helper\Data as ImportExportHelperData;
2427
use Magento\ImportExport\Model\ResourceModel\Helper;
28+
use Magento\ImportExport\Model\ResourceModel\Import\Data as ResourceImportData;
2529
use PHPUnit\Framework\MockObject\MockObject;
2630
use PHPUnit\Framework\TestCase;
31+
use ReflectionClass;
2732

2833
/**
2934
* @SuppressWarnings(PHPMD)
@@ -50,12 +55,15 @@ class TierPriceTest extends TestCase
5055
*/
5156
protected $tierPrice;
5257

58+
/**
59+
* @var ObjectManager
60+
*/
61+
protected $objectManager;
62+
5363
protected function setUp(): void
5464
{
55-
$this->groupRepository = $this->getMockBuilder(GroupRepositoryInterface::class)
56-
->disableOriginalConstructor()
57-
->onlyMethods(['getList'])
58-
->getMockForAbstractClass();
65+
$this->objectManager = new ObjectManager($this);
66+
$this->groupRepository = $this->createMock(GroupRepositoryInterface::class);
5967

6068
$this->searchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class);
6169
$this->storeResolver = $this->createMock(
@@ -74,48 +82,33 @@ public function testInitInternalCalls()
7482
{
7583
$searchCriteria = $this->createMock(SearchCriteria::class);
7684
$this->searchCriteriaBuilder->method('create')->willReturn($searchCriteria);
77-
$groupSearchResult = $this->getMockForAbstractClass(
78-
GroupSearchResultsInterface::class,
79-
[],
80-
'',
81-
false
82-
);
85+
$groupSearchResult = $this->createMock(GroupSearchResultsInterface::class);
8386
$this->groupRepository
8487
->method('getList')
8588
->with($searchCriteria)
8689
->willReturn($groupSearchResult);
8790

88-
$groupTest = $this->getMockBuilder(GroupInterface::class)
89-
->disableOriginalConstructor()
90-
->onlyMethods(['getCode', 'getId'])
91-
->getMockForAbstractClass();
91+
$groupTest = $this->createMock(GroupInterface::class);
9292
$groupTest->expects($this->once())->method('getCode');
9393
$groupTest->method('getId');
9494
$groups = [$groupTest];
9595
$groupSearchResult->method('getItems')->willReturn($groups);
9696

97-
$this->tierPrice->init(null);
97+
$productMock = $this->createMock(Product::class);
98+
$this->tierPrice->init($productMock);
9899
}
99100

100101
public function testInitAddToCustomerGroups()
101102
{
102103
$searchCriteria = $this->createMock(SearchCriteria::class);
103104
$this->searchCriteriaBuilder->method('create')->willReturn($searchCriteria);
104-
$groupSearchResult = $this->getMockForAbstractClass(
105-
GroupSearchResultsInterface::class,
106-
[],
107-
'',
108-
false
109-
);
105+
$groupSearchResult = $this->createMock(GroupSearchResultsInterface::class);
110106
$this->groupRepository
111107
->method('getList')
112108
->with($searchCriteria)
113109
->willReturn($groupSearchResult);
114110

115-
$groupTest = $this->getMockBuilder(GroupInterface::class)
116-
->disableOriginalConstructor()
117-
->onlyMethods(['getCode', 'getId'])
118-
->getMockForAbstractClass();
111+
$groupTest = $this->createMock(GroupInterface::class);
119112

120113
$expectedCode = 'code';
121114
$expectedId = 'id';
@@ -127,65 +120,49 @@ public function testInitAddToCustomerGroups()
127120
$groups = [$groupTest];
128121
$groupSearchResult->method('getItems')->willReturn($groups);
129122

130-
$this->tierPrice->init(null);
123+
$productMock = $this->createMock(Product::class);
124+
$this->tierPrice->init($productMock);
131125

132-
$this->assertEquals($expectedCustomerGroups, $this->getPropertyValue($this->tierPrice, 'customerGroups'));
126+
$reflection = new ReflectionClass($this->tierPrice);
127+
$property = $reflection->getProperty('customerGroups');
128+
$property->setAccessible(true);
129+
$this->assertEquals($expectedCustomerGroups, $property->getValue($this->tierPrice));
133130
}
134131

135132
public function testIsValidResultTrue()
136133
{
137134
$this->tierPrice->expects($this->once())->method('isValidValueAndLength')->willReturn(false);
138-
$this->setPropertyValue($this->tierPrice, 'customerGroups', true);
135+
$this->objectManager->setBackwardCompatibleProperty($this->tierPrice, 'customerGroups', true);
139136

140137
$result = $this->tierPrice->isValid([]);
141138
$this->assertTrue($result);
142139
}
143140

144141
/**
145-
* @dataProvider isValidAddMessagesCallDataProvider
146142
*
147143
* @param array $value
148144
* @param bool $hasEmptyColumns
149145
* @param array $customerGroups
150146
* @param array $expectedMessages
151147
*/
148+
#[DataProvider('isValidAddMessagesCallDataProvider')]
152149
public function testIsValidAddMessagesCall($value, $hasEmptyColumns, $customerGroups, $expectedMessages)
153150
{
154-
$priceContextMock = $this->getMockBuilder(Product::class)
155-
->setConstructorArgs(
156-
[
157-
Data::class,
158-
\Magento\ImportExport\Helper\Data::class,
159-
\Magento\ImportExport\Model\ResourceModel\Import\Data::class,
160-
Config::class,
161-
ResourceConnection::class,
162-
Helper::class,
163-
StringUtils::class,
164-
'ProcessingErrorAggregatorInterface'
165-
]
166-
);
151+
$priceContextMock = $this->createMock(Product::class);
167152

168153
$this->tierPrice->expects($this->once())->method('isValidValueAndLength')->willReturn(true);
169154
$this->tierPrice->method('hasEmptyColumns')->willReturn($hasEmptyColumns);
170-
$this->setPropertyValue($this->tierPrice, 'customerGroups', $customerGroups);
155+
$this->objectManager->setBackwardCompatibleProperty($this->tierPrice, 'customerGroups', $customerGroups);
171156

172157
$searchCriteria = $this->createMock(SearchCriteria::class);
173158
$this->searchCriteriaBuilder->method('create')->willReturn($searchCriteria);
174-
$groupSearchResult = $this->getMockForAbstractClass(
175-
GroupSearchResultsInterface::class,
176-
[],
177-
'',
178-
false
179-
);
159+
$groupSearchResult = $this->createMock(GroupSearchResultsInterface::class);
180160
$this->groupRepository
181161
->method('getList')
182162
->with($searchCriteria)
183163
->willReturn($groupSearchResult);
184164

185-
$groupTest = $this->getMockBuilder(GroupInterface::class)
186-
->disableOriginalConstructor()
187-
->onlyMethods(['getCode', 'getId'])
188-
->getMockForAbstractClass();
165+
$groupTest = $this->createMock(GroupInterface::class);
189166
$groupTest->expects($this->once())->method('getCode');
190167
$groupTest->method('getId');
191168
$groups = [$groupTest];
@@ -353,40 +330,4 @@ public static function isValidAddMessagesCallDataProvider()
353330
],
354331
];
355332
}
356-
357-
/**
358-
* Get any object property value.
359-
*
360-
* @param object $object
361-
* @param string $property
362-
* @return mixed
363-
* @throws \ReflectionException
364-
*/
365-
protected function getPropertyValue($object, $property)
366-
{
367-
$reflection = new \ReflectionClass(get_class($object));
368-
$reflectionProperty = $reflection->getProperty($property);
369-
$reflectionProperty->setAccessible(true);
370-
371-
return $reflectionProperty->getValue($object);
372-
}
373-
374-
/**
375-
* Set object property value.
376-
*
377-
* @param object $object
378-
* @param string $property
379-
* @param mixed $value
380-
* @return object
381-
* @throws \ReflectionException
382-
*/
383-
protected function setPropertyValue(&$object, $property, $value)
384-
{
385-
$reflection = new \ReflectionClass(get_class($object));
386-
$reflectionProperty = $reflection->getProperty($property);
387-
$reflectionProperty->setAccessible(true);
388-
$reflectionProperty->setValue($object, $value);
389-
390-
return $object;
391-
}
392333
}

0 commit comments

Comments
 (0)