7
7
8
8
use Magento \Framework \ObjectManager \Config \Compiled ;
9
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManager ;
10
- use Magento \Framework \Serialize \SerializerInterface ;
11
10
12
11
class CompiledTest extends \PHPUnit_Framework_TestCase
13
12
{
@@ -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,59 +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
-
64
59
$ configuration = [
65
60
'arguments ' => [
66
- 'type1 ' => 'serialized configuration for type1 ' ,
67
- 'type2 ' => 'serialized configuration for type2 '
61
+ 'type1 ' => 'configuration for type1 ' ,
62
+ 'type2 ' => [
63
+ 'argument2_1 ' => 'newArgumentValue2_1 ' ,
64
+ ]
68
65
],
69
66
'instanceTypes ' => [
70
67
'instanceType2 ' => 'newInstanceTypeValue2 ' ,
71
- 'instanceType3 ' => 'newInstanceTypeValue3 '
68
+ 'instanceType3 ' => 'newInstanceTypeValue3 ' ,
72
69
],
73
70
'preferences ' => [
74
- 'preference1 ' => 'newPreferenceValue1 '
75
- ]
71
+ 'preference1 ' => 'newPreferenceValue1 ' ,
72
+ ],
76
73
];
77
74
$ expectedArguments = [
78
- 'type1 ' => [
79
- 'argument1_1 ' => 'newArgumentValue1_1 '
80
- ],
75
+ 'type1 ' => 'configuration for type1 ' ,
81
76
'type2 ' => [
82
- 'argument2_1 ' => 'newArgumentValue2_1 '
77
+ 'argument2_1 ' => 'newArgumentValue2_1 ' ,
83
78
]
84
79
];
85
- $ 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 = [
86
99
'instanceType1 ' => 'instanceTypeValue1 ' ,
87
100
'instanceType2 ' => 'newInstanceTypeValue2 ' ,
88
101
'instanceType3 ' => 'newInstanceTypeValue3 '
89
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
+ ];
90
117
$ expectedPreferences = [
91
118
'preference1 ' => 'newPreferenceValue1 ' ,
92
119
'preference2 ' => 'preferenceValue2 '
93
120
];
94
-
95
- $ this ->serializerMock ->expects ($ this ->at (0 ))
96
- ->method ('unserialize ' )
97
- ->with ($ configuration ['arguments ' ]['type1 ' ])
98
- ->willReturn ($ expectedArguments ['type1 ' ]);
99
- $ this ->serializerMock ->expects ($ this ->at (1 ))
100
- ->method ('unserialize ' )
101
- ->with ($ configuration ['arguments ' ]['type2 ' ])
102
- ->willReturn ($ expectedArguments ['type2 ' ]);
103
-
104
121
$ this ->compiled ->extend ($ configuration );
105
- foreach ($ expectedArguments as $ type => $ arguments ) {
106
- $ this ->assertEquals ($ arguments , $ this ->compiled ->getArguments ($ type ));
107
- }
108
- $ this ->assertEquals ($ expectedVirtualTypes , $ this ->compiled ->getVirtualTypes ());
109
122
$ this ->assertEquals ($ expectedPreferences , $ this ->compiled ->getPreferences ());
110
123
}
111
124
@@ -114,23 +127,17 @@ public function testExtend()
114
127
*/
115
128
public function testGetArgumentsSerialized ()
116
129
{
117
- $ unserializedArguments = ['unserialized ' , 'arguments ' ];
118
-
119
- // method called twice but after one unserialization, unserialized version should be stored
120
- $ this ->serializerMock ->expects ($ this ->once ())->method ('unserialize ' )
121
- ->with ('serialized arguments ' )
122
- ->willReturn ($ unserializedArguments );
130
+ $ unserializedArguments = 'string arguments ' ;
123
131
124
- $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_serialized ' ));
125
- $ 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 ' ));
126
134
}
127
135
128
136
/**
129
137
* Arguments defined in array, have not previously been unserialized
130
138
*/
131
139
public function testGetArgumentsSerializedEmpty ()
132
140
{
133
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
134
141
$ this ->assertSame ([], $ this ->compiled ->getArguments ('class_with_no_arguments_serialized ' ));
135
142
}
136
143
@@ -140,25 +147,22 @@ public function testGetArgumentsSerializedEmpty()
140
147
public function testGetArgumentsUnserialized ()
141
148
{
142
149
$ unserializedArguments = ['unserialized ' , 'arguments ' ];
143
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
144
- $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_unserialized ' ));
150
+ $ this ->assertSame ($ unserializedArguments , $ this ->compiled ->getArguments ('class_with_arguments_array ' ));
145
151
}
146
152
147
153
/**
148
154
* Arguments are defined but empty
149
155
*/
150
156
public function testGetArgumentsUnserializedEmpty ()
151
157
{
152
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
153
- $ this ->assertSame ([], $ this ->compiled ->getArguments ('class_with_no_arguments_unserialized ' ));
158
+ $ this ->assertSame ([], $ this ->compiled ->getArguments ('class_with_no_arguments_empty_array ' ));
154
159
}
155
160
156
161
/**
157
162
* Arguments not defined in array
158
163
*/
159
164
public function testGetArgumentsNotDefined ()
160
165
{
161
- $ this ->serializerMock ->expects ($ this ->never ())->method ('unserialize ' );
162
166
$ this ->assertSame (null , $ this ->compiled ->getArguments ('class_not_stored_in_config ' ));
163
167
}
164
168
}
0 commit comments