Skip to content

Commit 53e03b2

Browse files
committed
Update unit tests
1 parent 74deee4 commit 53e03b2

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

app/code/Magento/Catalog/Model/ResourceModel/AbstractResource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,5 +577,4 @@ public function getAttributeRawValue($entityId, $attribute, $store)
577577

578578
return $attributesData === false ? false : $attributesData;
579579
}
580-
581580
}

app/code/Magento/Catalog/Model/ResourceModel/Product.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function __construct(
129129
$this->setFactory = $setFactory;
130130
$this->typeFactory = $typeFactory;
131131
$this->defaultAttributes = $defaultAttributes;
132-
$this->metadataService = $metadataService ?? ObjectManager::getInstance(
132+
$this->metadataService = $metadataService ?? ObjectManager::getInstance()->get(
133133
ProductAttributeRepositoryInterface::class
134134
);
135135
parent::__construct(
@@ -253,8 +253,11 @@ public function getCustomAttributesCodes()
253253
{
254254
if ($this->customAttributesCodes === null) {
255255
$this->customAttributesCodes = $this->getEavAttributesCodes($this->metadataService);
256-
$this->customAttributesCodes = array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES);
256+
$this->customAttributesCodes = array_values(
257+
array_diff($this->customAttributesCodes, ProductInterface::ATTRIBUTES)
258+
);
257259
}
260+
return $this->customAttributesCodes;
258261
}
259262

260263
/**

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

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Catalog\Api\Data\ProductExtensionFactory;
1010
use Magento\Catalog\Api\Data\ProductExtensionInterface;
11+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\Framework\Api\Data\ImageContentInterface;
1314
use Magento\Framework\Api\ExtensibleDataInterface;
@@ -140,11 +141,6 @@ class ProductTest extends \PHPUnit\Framework\TestCase
140141
*/
141142
protected $dataObjectHelperMock;
142143

143-
/**
144-
* @var \PHPUnit_Framework_MockObject_MockObject
145-
*/
146-
protected $metadataServiceMock;
147-
148144
/**
149145
* @var \PHPUnit_Framework_MockObject_MockObject
150146
*/
@@ -274,9 +270,7 @@ protected function setUp()
274270
);
275271
$optionFactory->expects($this->any())->method('create')->willReturn($this->optionInstanceMock);
276272

277-
$this->resource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product::class)
278-
->disableOriginalConstructor()
279-
->getMock();
273+
$this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Product::class);
280274

281275
$this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
282276
->disableOriginalConstructor()
@@ -328,7 +322,6 @@ protected function setUp()
328322
->disableOriginalConstructor()
329323
->getMock();
330324

331-
$this->metadataServiceMock = $this->createMock(\Magento\Catalog\Api\ProductAttributeRepositoryInterface::class);
332325
$this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
333326
->disableOriginalConstructor()->getMock();
334327

@@ -391,7 +384,7 @@ protected function setUp()
391384
'catalogProduct' => $this->_catalogProduct,
392385
'imageCacheFactory' => $this->imageCacheFactory,
393386
'mediaGalleryEntryFactory' => $this->mediaGalleryEntryFactoryMock,
394-
'metadataService' => $this->metadataServiceMock,
387+
'metadataService' => $this->createMock(ProductAttributeRepositoryInterface::class),
395388
'customAttributeFactory' => $this->attributeValueFactory,
396389
'mediaGalleryEntryConverterPool' => $this->mediaGalleryEntryConverterPoolMock,
397390
'linkRepository' => $this->productLinkRepositoryMock,
@@ -1269,19 +1262,10 @@ public function testGetCustomAttributes()
12691262
{
12701263
$priceCode = 'price';
12711264
$colorAttributeCode = 'color';
1272-
$interfaceAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class);
1273-
$interfaceAttribute->expects($this->once())
1274-
->method('getAttributeCode')
1275-
->willReturn($priceCode);
1276-
$colorAttribute = $this->createMock(\Magento\Framework\Api\MetadataObjectInterface::class);
1277-
$colorAttribute->expects($this->once())
1278-
->method('getAttributeCode')
1279-
->willReturn($colorAttributeCode);
1280-
$customAttributesMetadata = [$interfaceAttribute, $colorAttribute];
1281-
1282-
$this->metadataServiceMock->expects($this->once())
1283-
->method('getCustomAttributesMetadata')
1284-
->willReturn($customAttributesMetadata);
1265+
1266+
$this->resource
1267+
->method('getCustomAttributesCodes')
1268+
->willReturn([$colorAttributeCode]);
12851269
$this->model->setData($priceCode, 10);
12861270

12871271
//The color attribute is not set, expect empty custom attribute array

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/ProductTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
namespace Magento\Catalog\Test\Unit\Model\ResourceModel;
88

9+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Framework\Api\MetadataObjectInterface;
912
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1013

1114
class ProductTest extends \PHPUnit\Framework\TestCase
1215
{
16+
private $metadataService;
1317
/**
1418
* @var \Magento\Catalog\Model\ResourceModel\Product
1519
*/
@@ -38,11 +42,20 @@ protected function setUp()
3842
['create', '__wakeup']
3943
);
4044

45+
$this->metadataService = $this->createMock(ProductAttributeRepositoryInterface::class);
46+
47+
$entityTypeMock = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['getEntityModel']);
48+
$entityTypeMock->method('getEntityModel')->willReturn(Product::class);
49+
$eavConfigMock = $this->createMock(\Magento\Eav\Model\Config::class);
50+
$eavConfigMock->method('getEntityType')->willReturn($entityTypeMock);
51+
4152
$this->model = $objectManager->getObject(
4253
\Magento\Catalog\Model\ResourceModel\Product::class,
4354
[
4455
'setFactory' => $this->setFactoryMock,
4556
'typeFactory' => $this->typeFactoryMock,
57+
'eavConfig' => $eavConfigMock,
58+
'metadataService' => $this->metadataService,
4659
]
4760
);
4861
}
@@ -78,4 +91,25 @@ public function testValidateWrongAttributeSet()
7891

7992
$this->assertEquals($expectedErrorMessage, $this->model->validate($productMock));
8093
}
94+
95+
public function testGetCustomAttributes()
96+
{
97+
$priceCode = 'price';
98+
$colorAttributeCode = 'color';
99+
$interfaceAttribute = $this->createMock(MetadataObjectInterface::class);
100+
$interfaceAttribute->expects($this->once())
101+
->method('getAttributeCode')
102+
->willReturn($priceCode);
103+
$colorAttribute = $this->createMock(MetadataObjectInterface::class);
104+
$colorAttribute->expects($this->once())
105+
->method('getAttributeCode')
106+
->willReturn($colorAttributeCode);
107+
$customAttributesMetadata = [$interfaceAttribute, $colorAttribute];
108+
109+
$this->metadataService->expects($this->once())
110+
->method('getCustomAttributesMetadata')
111+
->willReturn($customAttributesMetadata);
112+
113+
$this->assertEquals([$colorAttributeCode], $this->model->getCustomAttributesCodes());
114+
}
81115
}

lib/internal/Magento/Framework/Model/AbstractExtensibleModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ abstract class AbstractExtensibleModel extends AbstractModel implements
3535
protected $customAttributeFactory;
3636

3737
/**
38+
* @deprecated Attribute codes are the same for all entities of the same type and should be saved in resource model
3839
* @var string[]
3940
*/
4041
protected $customAttributesCodes = null;
@@ -286,6 +287,8 @@ protected function getCustomAttributesCodes()
286287
*
287288
* Can be used in child classes, which represent EAV entities.
288289
*
290+
* @deprecated attribute codes should be managed centrally in resource model
291+
* @see \Magento\Eav\Model\Entity\AbstractEntity::getEavAttributesCodes()
289292
* @param \Magento\Framework\Api\MetadataServiceInterface $metadataService
290293
* @return string[]
291294
*/

0 commit comments

Comments
 (0)