Skip to content

Commit 208b1ec

Browse files
committed
Test stabilization.
1 parent c587d3a commit 208b1ec

17 files changed

+275
-184
lines changed

app/code/Magento/Catalog/Model/Category.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
105105
*/
106106
protected $_urlRewrite;
107107

108-
/**
109-
* @var ResourceModel\Category
110-
*/
111-
protected $_resource;
112-
113108
/**
114109
* Use flat resource model flag
115110
*
@@ -239,6 +234,7 @@ class Category extends \Magento\Catalog\Model\AbstractModel implements
239234
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
240235
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
241236
* @param array $data
237+
* @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes
242238
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
243239
*/
244240
public function __construct(
@@ -317,16 +313,6 @@ protected function getCustomAttributesCodes()
317313
return $this->getCustomAttributeCodes->execute($this->metadataService);
318314
}
319315

320-
/**
321-
* @throws \Magento\Framework\Exception\LocalizedException
322-
* @return \Magento\Catalog\Model\ResourceModel\Category
323-
* @deprecated because resource models should be used directly
324-
*/
325-
protected function _getResource()
326-
{
327-
return parent::_getResource();
328-
}
329-
330316
/**
331317
* Get flat resource model flag
332318
*

app/code/Magento/Catalog/Model/Entity/GetCategoryCustomAttributeCodes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public function __construct(
2626
$this->baseCustomAttributeCodes = $baseCustomAttributeCodes;
2727
}
2828

29-
29+
/**
30+
* @inheritdoc
31+
*/
3032
public function execute(MetadataServiceInterface $metadataService): array
3133
{
3234
$customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService);

app/code/Magento/Catalog/Model/Entity/GetProductCustomAttributeCodes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public function __construct(
2626
$this->baseCustomAttributeCodes = $baseCustomAttributeCodes;
2727
}
2828

29-
29+
/**
30+
* @inheritdoc
31+
*/
3032
public function execute(MetadataServiceInterface $metadataService): array
3133
{
3234
$customAttributesCodes = $this->baseCustomAttributeCodes->execute($metadataService);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
120120
*/
121121
protected $_urlModel = null;
122122

123-
/**
124-
* @var ResourceModel\Product
125-
*/
126-
protected $_resource;
127-
128123
/**
129124
* @var string
130125
*/
@@ -386,7 +381,7 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
386381
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
387382
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor
388383
* @param array $data
389-
*
384+
* @param GetCustomAttributeCodesInterface|null $getCustomAttributeCodes
390385
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
391386
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
392387
*/

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
*/
1212
namespace Magento\Catalog\Model\ResourceModel;
1313

14-
use Magento\Catalog\Api\CategoryAttributeRepositoryInterface;
15-
use Magento\Catalog\Api\Data\CategoryInterface;
16-
use Magento\Framework\App\ObjectManager;
1714
use Magento\Framework\EntityManager\EntityManager;
1815

1916
/**
@@ -95,7 +92,6 @@ class Category extends AbstractResource
9592
* @param Category\CollectionFactory $categoryCollectionFactory
9693
* @param array $data
9794
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
98-
* @param CategoryAttributeRepositoryInterface|null $metaDataService
9995
*/
10096
public function __construct(
10197
\Magento\Eav\Model\Entity\Context $context,
@@ -117,7 +113,7 @@ public function __construct(
117113
$this->_categoryCollectionFactory = $categoryCollectionFactory;
118114
$this->_eventManager = $eventManager;
119115
$this->connectionName = 'catalog';
120-
$this->serializer = $serializer ?: ObjectManager::getInstance()
116+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
121117
->get(\Magento\Framework\Serialize\Serializer\Json::class);
122118
}
123119

@@ -1064,7 +1060,7 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
10641060
private function getEntityManager()
10651061
{
10661062
if (null === $this->entityManager) {
1067-
$this->entityManager = ObjectManager::getInstance()
1063+
$this->entityManager = \Magento\Framework\App\ObjectManager::getInstance()
10681064
->get(\Magento\Framework\EntityManager\EntityManager::class);
10691065
}
10701066
return $this->entityManager;
@@ -1076,7 +1072,7 @@ private function getEntityManager()
10761072
private function getAggregateCount()
10771073
{
10781074
if (null === $this->aggregateCount) {
1079-
$this->aggregateCount = ObjectManager::getInstance()
1075+
$this->aggregateCount = \Magento\Framework\App\ObjectManager::getInstance()
10801076
->get(\Magento\Catalog\Model\ResourceModel\Category\AggregateCount::class);
10811077
}
10821078
return $this->aggregateCount;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel;
77

8-
use Magento\Catalog\Api\Data\ProductInterface;
9-
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
108
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
119
use Magento\Framework\App\ObjectManager;
1210

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

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

9-
use Magento\Catalog\Api\CategoryAttributeRepositoryInterface;
109
use Magento\Catalog\Model\Indexer;
11-
use Magento\Catalog\Model\Category;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
1211

1312
/**
1413
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -106,6 +105,11 @@ class CategoryTest extends \PHPUnit\Framework\TestCase
106105
*/
107106
private $indexerRegistry;
108107

108+
/**
109+
* @var \Magento\Catalog\Api\CategoryAttributeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
110+
*/
111+
private $metadataServiceMock;
112+
109113
/**
110114
* @var \PHPUnit_Framework_MockObject_MockObject
111115
*/
@@ -116,6 +120,11 @@ class CategoryTest extends \PHPUnit\Framework\TestCase
116120
*/
117121
private $objectManager;
118122

123+
/**
124+
* @var GetCustomAttributeCodesInterface|\PHPUnit_Framework_MockObject_MockObject
125+
*/
126+
private $getCustomAttributeCodes;
127+
119128
protected function setUp()
120129
{
121130
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -151,8 +160,15 @@ protected function setUp()
151160
$this->resource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
152161
$this->indexerRegistry = $this->createPartialMock(\Magento\Framework\Indexer\IndexerRegistry::class, ['get']);
153162

163+
$this->metadataServiceMock = $this->createMock(
164+
\Magento\Catalog\Api\CategoryAttributeRepositoryInterface::class
165+
);
154166
$this->attributeValueFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
155167
->disableOriginalConstructor()->getMock();
168+
$this->getCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class)
169+
->setMethods(['execute'])
170+
->disableOriginalConstructor()
171+
->getMockForAbstractClass();
156172

157173
$this->category = $this->getCategoryModel();
158174
}
@@ -300,8 +316,9 @@ protected function getCategoryModel()
300316
'urlFinder' => $this->urlFinder,
301317
'resource' => $this->resource,
302318
'indexerRegistry' => $this->indexerRegistry,
303-
'metadataService' => $this->createMock(CategoryAttributeRepositoryInterface::class),
319+
'metadataService' => $this->metadataServiceMock,
304320
'customAttributeFactory' => $this->attributeValueFactory,
321+
'getCustomAttributeCodes' => $this->getCustomAttributeCodes
305322
]
306323
);
307324
}
@@ -424,39 +441,33 @@ public function testReindexFlatDisabled(
424441

425442
public function testGetCustomAttributes()
426443
{
427-
$interfaceAttributeCode = 'name';
428-
$customAttributeCode = 'description';
429-
430-
$this->resource
431-
->method('getCustomAttributesCodes')
432-
->willReturn([$customAttributeCode]);
433-
$this->category->setData($interfaceAttributeCode, "sub");
444+
$nameAttributeCode = 'name';
445+
$descriptionAttributeCode = 'description';
446+
$this->getCustomAttributeCodes->expects($this->exactly(3))
447+
->method('execute')
448+
->willReturn([$descriptionAttributeCode]);
449+
$this->category->setData($nameAttributeCode, "sub");
434450

435451
//The description attribute is not set, expect empty custom attribute array
436452
$this->assertEquals([], $this->category->getCustomAttributes());
437453

438454
//Set the description attribute;
439-
$initialCustomAttributeValue = "initial description";
440-
$this->category->setData($customAttributeCode, $initialCustomAttributeValue);
455+
$this->category->setData($descriptionAttributeCode, "description");
441456
$attributeValue = new \Magento\Framework\Api\AttributeValue();
442457
$attributeValue2 = new \Magento\Framework\Api\AttributeValue();
443458
$this->attributeValueFactory->expects($this->exactly(2))->method('create')
444459
->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2);
445460
$this->assertEquals(1, count($this->category->getCustomAttributes()));
446-
$this->assertNotNull($this->category->getCustomAttribute($customAttributeCode));
447-
$this->assertEquals(
448-
$initialCustomAttributeValue,
449-
$this->category->getCustomAttribute($customAttributeCode)->getValue()
450-
);
461+
$this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
462+
$this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue());
451463

452464
//Change the attribute value, should reflect in getCustomAttribute
453-
$newCustomAttributeValue = "new description";
454-
$this->category->setData($customAttributeCode, $newCustomAttributeValue);
465+
$this->category->setData($descriptionAttributeCode, "new description");
455466
$this->assertEquals(1, count($this->category->getCustomAttributes()));
456-
$this->assertNotNull($this->category->getCustomAttribute($customAttributeCode));
467+
$this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
457468
$this->assertEquals(
458-
$newCustomAttributeValue,
459-
$this->category->getCustomAttribute($customAttributeCode)->getValue()
469+
"new description",
470+
$this->category->getCustomAttribute($descriptionAttributeCode)->getValue()
460471
);
461472
}
462473

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model\Entity;
8+
9+
use Magento\Catalog\Model\Entity\GetCategoryCustomAttributeCodes;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
11+
use Magento\Framework\Api\MetadataServiceInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Provide tests for GetCategoryCustomAttributeCodes entity model.
17+
*/
18+
class GetCategoryCustomAttributeCodesTest extends TestCase
19+
{
20+
/**
21+
* Test subject.
22+
*
23+
* @var GetCategoryCustomAttributeCodes
24+
*/
25+
private $getCategoryCustomAttributeCodes;
26+
27+
/**
28+
* @var GetCustomAttributeCodesInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $baseCustomAttributeCodes;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp()
36+
{
37+
$this->baseCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class)
38+
->disableOriginalConstructor()
39+
->setMethods(['execute'])
40+
->getMockForAbstractClass();
41+
$objectManager = new ObjectManager($this);
42+
$this->getCategoryCustomAttributeCodes = $objectManager->getObject(
43+
GetCategoryCustomAttributeCodes::class,
44+
['baseCustomAttributeCodes' => $this->baseCustomAttributeCodes]
45+
);
46+
}
47+
48+
/**
49+
* Test GetCategoryCustomAttributeCodes::execute() will return only custom category attribute codes.
50+
*/
51+
public function testExecute()
52+
{
53+
/** @var MetadataServiceInterface|\PHPUnit_Framework_MockObject_MockObject $metadataService */
54+
$metadataService = $this->getMockBuilder(MetadataServiceInterface::class)
55+
->disableOriginalConstructor()
56+
->getMockForAbstractClass();
57+
$this->baseCustomAttributeCodes->expects($this->once())
58+
->method('execute')
59+
->with($this->identicalTo($metadataService))
60+
->willReturn(['test_custom_attribute_code', 'name']);
61+
$this->assertEquals(
62+
['test_custom_attribute_code'],
63+
$this->getCategoryCustomAttributeCodes->execute($metadataService)
64+
);
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Model\Entity;
8+
9+
use Magento\Catalog\Model\Entity\GetProductCustomAttributeCodes;
10+
use Magento\Eav\Model\Entity\GetCustomAttributeCodesInterface;
11+
use Magento\Framework\Api\MetadataServiceInterface;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Provide tests for GetProductCustomAttributeCodes entity model.
17+
*/
18+
class GetProductCustomAttributeCodesTest extends TestCase
19+
{
20+
/**
21+
* Test subject.
22+
*
23+
* @var GetProductCustomAttributeCodes
24+
*/
25+
private $getProductCustomAttributeCodes;
26+
27+
/**
28+
* @var GetCustomAttributeCodesInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $baseCustomAttributeCodes;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp()
36+
{
37+
$this->baseCustomAttributeCodes = $this->getMockBuilder(GetCustomAttributeCodesInterface::class)
38+
->disableOriginalConstructor()
39+
->setMethods(['execute'])
40+
->getMockForAbstractClass();
41+
$objectManager = new ObjectManager($this);
42+
$this->getProductCustomAttributeCodes = $objectManager->getObject(
43+
GetProductCustomAttributeCodes::class,
44+
['baseCustomAttributeCodes' => $this->baseCustomAttributeCodes]
45+
);
46+
}
47+
48+
/**
49+
* Test GetProductCustomAttributeCodes::execute() will return only custom product attribute codes.
50+
*/
51+
public function testExecute()
52+
{
53+
/** @var MetadataServiceInterface|\PHPUnit_Framework_MockObject_MockObject $metadataService */
54+
$metadataService = $this->getMockBuilder(MetadataServiceInterface::class)
55+
->disableOriginalConstructor()
56+
->getMockForAbstractClass();
57+
$this->baseCustomAttributeCodes->expects($this->once())
58+
->method('execute')
59+
->with($this->identicalTo($metadataService))
60+
->willReturn(['test_custom_attribute_code', 'name']);
61+
$this->assertEquals(
62+
['test_custom_attribute_code'],
63+
$this->getProductCustomAttributeCodes->execute($metadataService)
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)