Skip to content

Commit 3b3cab4

Browse files
author
Igor Melnikov
committed
MAGETWO-69137: upgrading Magento2 Project PHPUnit version to latest
- refactor tests
2 parents 1844300 + 4e4039f commit 3b3cab4

File tree

5 files changed

+6945
-356
lines changed

5 files changed

+6945
-356
lines changed

app/code/Magento/Eav/Model/Entity/AttributeLoader.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function loadAllAttributes(AbstractEntity $resource, DataObject $object =
6969
* Check and init default attributes
7070
*/
7171
$defaultAttributesCodes = array_diff($resource->getDefaultAttributes(), $attributeCodes);
72+
73+
$resource->unsetAttributes();
74+
7275
foreach ($defaultAttributesCodes as $attributeCode) {
7376
$resource->addAttribute($this->_getDefaultAttribute($resource, $attributeCode));
7477
}

app/code/Magento/Eav/Test/Unit/Model/Entity/AttributeLoaderTest.php

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\Eav\Model\Attribute;
99
use Magento\Eav\Model\Config;
1010
use Magento\Eav\Model\Entity\AbstractEntity;
11-
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1211
use Magento\Eav\Model\Entity\AttributeLoader;
1312
use Magento\Eav\Model\Entity\Type;
1413
use Magento\Framework\DataObject;
@@ -43,10 +42,13 @@ class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
4342

4443
protected function setUp()
4544
{
46-
$this->configMock = $this->createMock(Config::class);
47-
$this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
48-
$this->entityMock = $this->createMock(AbstractEntity::class);
49-
$this->entityTypeMock = $this->createMock(Type::class);
45+
$this->configMock = $this->createMock(Config::class, [], [], '', false);
46+
$this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
47+
->setMethods(['create'])
48+
->disableOriginalConstructor()
49+
->getMockForAbstractClass();
50+
$this->entityMock = $this->createMock(AbstractEntity::class, [], [], '', false);
51+
$this->entityTypeMock = $this->createMock(Type::class, [], [], '', false);
5052
$this->attributeLoader = new AttributeLoader(
5153
$this->configMock,
5254
$this->objectManagerMock
@@ -55,61 +57,53 @@ protected function setUp()
5557

5658
public function testLoadAllAttributes()
5759
{
58-
$defaultAttributes = ['bar'];
60+
$attributeCode = 'bar';
5961
$entityTypeId = 1;
6062
$dataObject = new DataObject();
61-
$this->entityMock->expects($this->any())
62-
->method('getEntityType')
63-
->willReturn($this->entityTypeMock);
64-
65-
$this->entityMock->expects($this->once())
66-
->method('getDefaultAttributes')
67-
->willReturn($defaultAttributes);
68-
$this->entityTypeMock->expects($this->any())
69-
->method('getId')
70-
->willReturn($entityTypeId);
71-
$attributeMock = $this->createPartialMock(\Magento\Eav\Model\Attribute::class, [
72-
'setAttributeCode',
73-
'setBackendType',
74-
'setIsGlobal',
75-
'setEntityType',
76-
'setEntityTypeId'
77-
]);
78-
$this->configMock->expects($this->once())
79-
->method('getEntityAttributes')
80-
->willReturn(['bar' => $attributeMock]);
81-
$this->entityMock->expects($this->once())
82-
->method('addAttribute')
83-
->with($attributeMock);
63+
$this->entityMock->expects($this->atLeastOnce())->method('getEntityType')->willReturn($this->entityTypeMock);
64+
$this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([$attributeCode]);
65+
$this->entityTypeMock->expects($this->atLeastOnce())->method('getId')->willReturn($entityTypeId);
66+
$this->configMock->expects($this->once())->method('getEntityAttributes')->willReturn([]);
67+
$this->entityMock->expects($this->once())->method('unsetAttributes')->willReturnSelf();
68+
$this->entityTypeMock->expects($this->once())
69+
->method('getAttributeModel')->willReturn(\Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL);
70+
$attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
71+
->setMethods(['setAttributeCode', 'setBackendType', 'setIsGlobal', 'setEntityType', 'setEntityTypeId'])
72+
->disableOriginalConstructor()->getMock();
73+
$this->objectManagerMock->expects($this->once())
74+
->method('create')->with(\Magento\Eav\Model\Entity::DEFAULT_ATTRIBUTE_MODEL)->willReturn($attributeMock);
75+
$attributeMock->expects($this->once())->method('setAttributeCode')->with($attributeCode)->willReturnSelf();
76+
$attributeMock->expects($this->once())->method('setBackendType')
77+
->with(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::TYPE_STATIC)->willReturnSelf();
78+
$attributeMock->expects($this->once())->method('setIsGlobal')->with(1)->willReturnSelf();
79+
$attributeMock->expects($this->once())->method('setEntityType')->with($this->entityTypeMock)->willReturnSelf();
80+
$attributeMock->expects($this->once())->method('setEntityTypeId')->with($entityTypeId)->willReturnSelf();
81+
$this->entityMock->expects($this->once())->method('addAttribute')->with($attributeMock)->willReturnSelf();
8482
$this->attributeLoader->loadAllAttributes($this->entityMock, $dataObject);
8583
}
8684

8785
public function testLoadAllAttributesAttributeCodesPresentInDefaultAttributes()
8886
{
89-
$attributeMock = $this->createPartialMock(\Magento\Eav\Model\Attribute::class, [
87+
$attributeMock = $this->createPartialMock(\Magento\Eav\Model\Attribute::class,
88+
[
9089
'setAttributeCode',
9190
'setBackendType',
9291
'setIsGlobal',
9392
'setEntityType',
9493
'setEntityTypeId'
95-
]);
96-
$attributeCodes = ['bar'=>$attributeMock];
94+
]
95+
);
96+
$attributeCodes = ['bar' => $attributeMock];
9797
$defaultAttributes = ['bar'];
9898
$dataObject = new DataObject();
99-
$this->entityMock->expects($this->any())
100-
->method('getEntityType')
101-
->willReturn($this->entityTypeMock);
99+
$this->entityMock->expects($this->once())->method('getEntityType')->willReturn($this->entityTypeMock);
102100
$this->configMock->expects($this->once())
103-
->method('getEntityAttributes')
104-
->willReturn($attributeCodes, $dataObject);
105-
$this->entityMock->expects($this->once())
106-
->method('getDefaultAttributes')
107-
->willReturn($defaultAttributes);
101+
->method('getEntityAttributes')->willReturn($attributeCodes);
102+
$this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn($defaultAttributes);
103+
$this->entityMock->expects($this->once())->method('unsetAttributes')->willReturnSelf();
108104
$this->entityMock->expects($this->atLeastOnce())
109-
->method('addAttribute')->with($attributeMock);
110-
111-
$this->objectManagerMock->expects($this->never())
112-
->method('create');
105+
->method('addAttribute')->with($attributeMock)->willReturnSelf();
106+
$this->objectManagerMock->expects($this->never())->method('create');
113107
$this->attributeLoader->loadAllAttributes($this->entityMock, $dataObject);
114108
}
115109
}

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function testSaveActionCategoryWithDangerRequest()
339339
);
340340
$this->dispatch('backend/catalog/category/save');
341341
$this->assertSessionMessages(
342-
$this->equalTo(['The value of attribute "is_active" must be set']),
342+
$this->equalTo(['The value of attribute "name" must be set']),
343343
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
344344
);
345345
}

0 commit comments

Comments
 (0)