6
6
namespace Magento \Framework \ObjectManager \Test \Unit \Config ;
7
7
8
8
use Magento \Framework \ObjectManager \Config \Compiled ;
9
- use Magento \Framework \Serialize \SerializerInterface ;
10
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManager ;
11
10
12
11
class CompiledTest extends \PHPUnit_Framework_TestCase
@@ -16,11 +15,6 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
16
15
*/
17
16
private $ objectManager ;
18
17
19
- /**
20
- * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
21
- */
22
- private $ serializerMock ;
23
-
24
18
/**
25
19
* @var \Magento\Framework\ObjectManager\Config\Compiled
26
20
*/
@@ -29,15 +23,14 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
29
23
protected function setUp ()
30
24
{
31
25
$ this ->objectManager = new ObjectManager ($ this );
32
- $ this ->serializerMock = $ this ->getMock (SerializerInterface::class);
33
26
34
27
$ initialData = [
35
28
'arguments ' => [
36
29
'type1 ' => 'initial serialized configuration for type1 ' ,
37
30
'class_with_no_arguments_serialized ' => null ,
38
- 'class_with_arguments_serialized ' => 'serialized arguments ' ,
39
- 'class_with_arguments_unserialized ' => ['unserialized ' , 'arguments ' ],
40
- 'class_with_no_arguments_unserialized ' => [],
31
+ 'class_with_arguments_string ' => 'string arguments ' ,
32
+ 'class_with_arguments_array ' => ['unserialized ' , 'arguments ' ],
33
+ 'class_with_no_arguments_empty_array ' => [],
41
34
],
42
35
'instanceTypes ' => [
43
36
'instanceType1 ' => 'instanceTypeValue1 ' ,
@@ -53,58 +46,79 @@ protected function setUp()
53
46
Compiled::class,
54
47
[
55
48
'data ' => $ initialData ,
56
- 'serializer ' => $ this ->serializerMock
57
49
]
58
50
);
59
51
}
60
52
61
- public function testExtend ()
53
+ /**
54
+ * Test is it possible extend/overwrite arguments for the DI.
55
+ *
56
+ */
57
+ public function testExtendArguments ()
62
58
{
63
59
$ configuration = [
64
60
'arguments ' => [
65
- 'type1 ' => 'serialized configuration for type1 ' ,
66
- 'type2 ' => 'serialized configuration for type2 '
61
+ 'type1 ' => 'configuration for type1 ' ,
62
+ 'type2 ' => [
63
+ 'argument2_1 ' => 'newArgumentValue2_1 ' ,
64
+ ]
67
65
],
68
66
'instanceTypes ' => [
69
67
'instanceType2 ' => 'newInstanceTypeValue2 ' ,
70
- 'instanceType3 ' => 'newInstanceTypeValue3 '
68
+ 'instanceType3 ' => 'newInstanceTypeValue3 ' ,
71
69
],
72
70
'preferences ' => [
73
- 'preference1 ' => 'newPreferenceValue1 '
74
- ]
71
+ 'preference1 ' => 'newPreferenceValue1 ' ,
72
+ ],
75
73
];
76
74
$ expectedArguments = [
77
- 'type1 ' => [
78
- 'argument1_1 ' => 'newArgumentValue1_1 '
79
- ],
75
+ 'type1 ' => 'configuration for type1 ' ,
80
76
'type2 ' => [
81
- 'argument2_1 ' => 'newArgumentValue2_1 '
77
+ 'argument2_1 ' => 'newArgumentValue2_1 ' ,
82
78
]
83
79
];
84
- $ expectedVirtualTypes = [
80
+
81
+ $ this ->compiled ->extend ($ configuration );
82
+ foreach ($ expectedArguments as $ type => $ arguments ) {
83
+ $ this ->assertEquals ($ arguments , $ this ->compiled ->getArguments ($ type ));
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Test getting virtual types from the DI.
89
+ */
90
+ public function testVirtualTypes ()
91
+ {
92
+ $ configuration = [
93
+ 'instanceTypes ' => [
94
+ 'instanceType2 ' => 'newInstanceTypeValue2 ' ,
95
+ 'instanceType3 ' => 'newInstanceTypeValue3 '
96
+ ],
97
+ ];
98
+ $ expectedTypes = [
85
99
'instanceType1 ' => 'instanceTypeValue1 ' ,
86
100
'instanceType2 ' => 'newInstanceTypeValue2 ' ,
87
101
'instanceType3 ' => 'newInstanceTypeValue3 '
88
102
];
103
+ $ this ->compiled ->extend ($ configuration );
104
+ $ this ->assertEquals ($ expectedTypes , $ this ->compiled ->getVirtualTypes ());
105
+ }
106
+
107
+ /**
108
+ * Test getting preferences from the DI.
109
+ */
110
+ public function testPreferences ()
111
+ {
112
+ $ configuration = [
113
+ 'preferences ' => [
114
+ 'preference1 ' => 'newPreferenceValue1 '
115
+ ]
116
+ ];
89
117
$ expectedPreferences = [
90
118
'preference1 ' => 'newPreferenceValue1 ' ,
91
119
'preference2 ' => 'preferenceValue2 '
92
120
];
93
-
94
- $ this ->serializerMock ->expects ($ this ->at (0 ))
95
- ->method ('unserialize ' )
96
- ->with ($ configuration ['arguments ' ]['type1 ' ])
97
- ->willReturn ($ expectedArguments ['type1 ' ]);
98
- $ this ->serializerMock ->expects ($ this ->at (1 ))
99
- ->method ('unserialize ' )
100
- ->with ($ configuration ['arguments ' ]['type2 ' ])
101
- ->willReturn ($ expectedArguments ['type2 ' ]);
102
-
103
121
$ this ->compiled ->extend ($ configuration );
104
- foreach ($ expectedArguments as $ type => $ arguments ) {
105
- $ this ->assertEquals ($ arguments , $ this ->compiled ->getArguments ($ type ));
106
- }
107
- $ this ->assertEquals ($ expectedVirtualTypes , $ this ->compiled ->getVirtualTypes ());
108
122
$ this ->assertEquals ($ expectedPreferences , $ this ->compiled ->getPreferences ());
109
123
}
110
124
@@ -113,23 +127,17 @@ public function testExtend()
113
127
*/
114
128
public function testGetArgumentsSerialized ()
115
129
{
116
- $ unserializedArguments = ['unserialized ' , 'arguments ' ];
117
-
118
- // method called twice but after one unserialization, unserialized version should be stored
119
- $ this ->serializerMock ->expects ($ this ->once ())->method ('unserialize ' )
120
- ->with ('serialized arguments ' )
121
- ->willReturn ($ unserializedArguments );
130
+ $ unserializedArguments = 'string arguments ' ;
122
131
123
- $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_serialized ' ));
124
- $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_serialized ' ));
132
+ $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_string ' ));
133
+ $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_string ' ));
125
134
}
126
135
127
136
/**
128
137
* Arguments defined in array, have not previously been unserialized
129
138
*/
130
139
public function testGetArgumentsSerializedEmpty ()
131
140
{
132
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
133
141
$ this ->assertSame ([], $ this ->compiled ->getArguments ('class_with_no_arguments_serialized ' ));
134
142
}
135
143
@@ -139,25 +147,22 @@ public function testGetArgumentsSerializedEmpty()
139
147
public function testGetArgumentsUnserialized ()
140
148
{
141
149
$ unserializedArguments = ['unserialized ' , 'arguments ' ];
142
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
143
- $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_unserialized ' ));
150
+ $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_array ' ));
144
151
}
145
152
146
153
/**
147
154
* Arguments are defined but empty
148
155
*/
149
156
public function testGetArgumentsUnserializedEmpty ()
150
157
{
151
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
152
- $ this ->assertSame ([], $ this ->compiled ->getArguments ('class_with_no_arguments_unserialized ' ));
158
+ $ this ->assertSame ([], $ this ->compiled ->getArguments ('class_with_no_arguments_empty_array ' ));
153
159
}
154
160
155
161
/**
156
162
* Arguments not defined in array
157
163
*/
158
164
public function testGetArgumentsNotDefined ()
159
165
{
160
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
161
166
$ this ->assertSame (null , $ this ->compiled ->getArguments ('class_not_stored_in_config ' ));
162
167
}
163
168
}
0 commit comments