8
8
use Magento \Eav \Model \Attribute ;
9
9
use Magento \Eav \Model \Config ;
10
10
use Magento \Eav \Model \Entity \AbstractEntity ;
11
- use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
12
11
use Magento \Eav \Model \Entity \AttributeLoader ;
13
12
use Magento \Eav \Model \Entity \Type ;
14
13
use Magento \Framework \DataObject ;
@@ -43,10 +42,13 @@ class AttributeLoaderTest extends \PHPUnit\Framework\TestCase
43
42
44
43
protected function setUp ()
45
44
{
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 );
50
52
$ this ->attributeLoader = new AttributeLoader (
51
53
$ this ->configMock ,
52
54
$ this ->objectManagerMock
@@ -55,61 +57,53 @@ protected function setUp()
55
57
56
58
public function testLoadAllAttributes ()
57
59
{
58
- $ defaultAttributes = [ 'bar ' ] ;
60
+ $ attributeCode = 'bar ' ;
59
61
$ entityTypeId = 1 ;
60
62
$ 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 ();
84
82
$ this ->attributeLoader ->loadAllAttributes ($ this ->entityMock , $ dataObject );
85
83
}
86
84
87
85
public function testLoadAllAttributesAttributeCodesPresentInDefaultAttributes ()
88
86
{
89
- $ attributeMock = $ this ->createPartialMock (\Magento \Eav \Model \Attribute::class, [
87
+ $ attributeMock = $ this ->createPartialMock (\Magento \Eav \Model \Attribute::class,
88
+ [
90
89
'setAttributeCode ' ,
91
90
'setBackendType ' ,
92
91
'setIsGlobal ' ,
93
92
'setEntityType ' ,
94
93
'setEntityTypeId '
95
- ]);
96
- $ attributeCodes = ['bar ' =>$ attributeMock ];
94
+ ]
95
+ );
96
+ $ attributeCodes = ['bar ' => $ attributeMock ];
97
97
$ defaultAttributes = ['bar ' ];
98
98
$ 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 );
102
100
$ 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 ();
108
104
$ 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 ' );
113
107
$ this ->attributeLoader ->loadAllAttributes ($ this ->entityMock , $ dataObject );
114
108
}
115
109
}
0 commit comments