5
5
*/
6
6
namespace Magento \Tools \Di \Compiler \Config ;
7
7
8
+ use Magento \Framework \App \Area ;
9
+ use Magento \Tools \Di \Definition \Collection ;
10
+
8
11
class ReaderTest extends \PHPUnit_Framework_TestCase
9
12
{
10
13
/**
@@ -54,6 +57,9 @@ protected function setUp()
54
57
false
55
58
);
56
59
$ this ->argumentsResolver = $ this ->getMock ('Magento\Tools\Di\Compiler\ArgumentsResolver ' , [], [], '' , false );
60
+ $ this ->argumentsResolverFactory ->expects ($ this ->any ())
61
+ ->method ('create ' )
62
+ ->willReturn ($ this ->argumentsResolver );
57
63
$ this ->classReaderDecorator = $ this ->getMock (
58
64
'Magento\Tools\Di\Code\Reader\ClassReaderDecorator ' ,
59
65
[],
@@ -72,71 +78,182 @@ protected function setUp()
72
78
);
73
79
}
74
80
75
- public function testGenerateCachePerScopeExtends ()
81
+ public function testGenerateCachePerScopeGlobal ()
76
82
{
77
- $ definitionsCollection = $ this ->getMock ('Magento\Tools\Di\Definition\Collection ' , [], [], '' , false );
78
- $ this ->diContainerConfig ->expects ($ this ->once ())
79
- ->method ('extend ' )
80
- ->with ([]);
81
- $ this ->configLoader ->expects ($ this ->once ())
82
- ->method ('load ' )
83
- ->with ('areaCode ' )
84
- ->willReturn ([]);
85
-
86
- $ this ->argumentsResolverFactory ->expects ($ this ->once ())
87
- ->method ('create ' )
88
- ->with ($ this ->diContainerConfig )
89
- ->willReturn ($ this ->argumentsResolver );
90
- $ definitionsCollection ->expects ($ this ->exactly (2 ))
91
- ->method ('getInstancesNamesList ' )
92
- ->willReturn (['instanceType1 ' ], ['instanceType2 ' ]);
93
- $ definitionsCollection ->expects ($ this ->once ())
94
- ->method ('getInstanceArguments ' )
95
- ->willReturnMap ([
96
- ['instanceType1 ' , null ],
97
- ['instanceType2 ' , ['arg1 ' , 'arg2 ' ]],
98
- ]);
99
- $ this ->typeReader ->expects ($ this ->exactly (3 ))
100
- ->method ('isConcrete ' )
101
- ->willReturnMap ([
102
- ['instanceType1 ' , true ],
103
- ['instanceType2 ' , false ],
104
- ['originalType1 ' , true ],
105
- ['originalType2 ' , false ],
106
- ]);
107
- $ this ->argumentsResolver ->expects ($ this ->exactly (2 ))
108
- ->method ('getResolvedConstructorArguments ' )
109
- ->willReturnMap ([
110
- ['instanceType1 ' , 'resolvedConstructor1 ' ],
111
- ['instanceVirtualType1 ' , 'resolvedConstructor2 ' ],
112
- ]);
113
- $ this ->diContainerConfig ->expects ($ this ->exactly (2 ))
83
+ $ definitionCollection = $ this ->getDefinitionsCollection ();
84
+ $ this ->diContainerConfig ->expects ($ this ->any ())
114
85
->method ('getVirtualTypes ' )
115
- ->willReturn (['instanceVirtualType1 ' => 1 , 'instanceVirtualType2 ' => 2 ]);
116
- $ this ->diContainerConfig ->expects ($ this ->exactly (4 ))
86
+ ->willReturn ($ this ->getVirtualTypes ());
87
+ $ this ->diContainerConfig ->expects ($ this ->any ())
88
+ ->method ('getPreferences ' )
89
+ ->willReturn ($ this ->getPreferences ());
90
+
91
+ $ getResolvedConstructorArgumentsMap = $ this ->getResolvedVirtualConstructorArgumentsMap (
92
+ $ definitionCollection ,
93
+ $ this ->getVirtualTypes ()
94
+ );
95
+
96
+ $ this ->diContainerConfig ->expects ($ this ->any ())
117
97
->method ('getInstanceType ' )
118
- ->willReturnMap ([
119
- ['instanceVirtualType1 ' , 'originalType1 ' ],
120
- ['instanceVirtualType2 ' , 'originalType2 ' ],
121
- ]);
122
- $ definitionsCollection ->expects ($ this ->exactly (2 ))
123
- ->method ('hasInstance ' )
124
- ->willReturn ('' );
125
- $ this ->classReaderDecorator ->expects ($ this ->once ())
126
- ->method ('getConstructor ' )
127
- ->willReturn ('constructor ' );
128
- $ this ->diContainerConfig ->expects ($ this ->once ())
98
+ ->willReturnMap ($ this ->getInstanceTypeMap ($ this ->getVirtualTypes ()));
99
+
100
+ $ this ->diContainerConfig ->expects ($ this ->any ())
129
101
->method ('isShared ' )
130
- ->willReturnMap ([
131
- ['instanceType1 ' , true ],
132
- ['instanceType2 ' , false ],
133
- ]);
134
- $ this ->diContainerConfig ->expects ($ this ->once ())
102
+ ->willReturnMap ($ this ->getExpectedNonShared ());
103
+
104
+ $ this ->diContainerConfig ->expects ($ this ->any ())
135
105
->method ('getPreference ' )
136
- ->willReturnMap ([
137
- ['instanceType1 ' , 'instanceType1ss ' ],
138
- ['instanceType2 ' , 'instanceType2 ' ],
139
- ]);
140
- $ this ->model ->generateCachePerScope ($ definitionsCollection , 'areaCode ' );
106
+ ->willReturnMap ($ this ->getPreferencesMap ());
107
+
108
+ $ isConcreteMap = [];
109
+ foreach ($ definitionCollection ->getInstancesNamesList () as $ instanceType ) {
110
+ $ isConcreteMap [] = [$ instanceType , strpos ($ instanceType , 'Interface ' ) === false ];
111
+
112
+ $ getResolvedConstructorArgumentsMap [] = [
113
+ $ instanceType ,
114
+ $ definitionCollection ->getInstanceArguments ($ instanceType ),
115
+ $ this ->getResolvedArguments (
116
+ $ definitionCollection ->getInstanceArguments ($ instanceType )
117
+ )
118
+ ];
119
+ }
120
+
121
+ $ this ->typeReader ->expects ($ this ->any ())
122
+ ->method ('isConcrete ' )
123
+ ->willReturnMap ($ isConcreteMap );
124
+ $ this ->argumentsResolver ->expects ($ this ->any ())
125
+ ->method ('getResolvedConstructorArguments ' )
126
+ ->willReturnMap ($ getResolvedConstructorArgumentsMap );
127
+
128
+ $ this ->assertEquals (
129
+ $ this ->getExpectedGlobalConfig (),
130
+ $ this ->model ->generateCachePerScope ($ definitionCollection , Area::AREA_GLOBAL )
131
+ );
132
+ }
133
+
134
+ /**
135
+ * @return array
136
+ */
137
+ private function getExpectedGlobalConfig ()
138
+ {
139
+ return [
140
+ 'arguments ' => [
141
+ 'ConcreteType1 ' => serialize (['resolved_argument1 ' , 'resolved_argument2 ' ]),
142
+ 'ConcreteType2 ' => serialize (['resolved_argument1 ' , 'resolved_argument2 ' ]),
143
+ 'virtualType1 ' => serialize (['resolved_argument1 ' , 'resolved_argument2 ' ])
144
+ ],
145
+ 'nonShared ' => [
146
+ 'ConcreteType2 ' => true ,
147
+ 'ThirdPartyInterface ' => true
148
+ ],
149
+ 'preferences ' => $ this ->getPreferences (),
150
+ 'instanceTypes ' => $ this ->getVirtualTypes (),
151
+ ];
152
+ }
153
+
154
+ /**
155
+ * @return Collection
156
+ */
157
+ private function getDefinitionsCollection ()
158
+ {
159
+ $ definitionCollection = new Collection ();
160
+ $ definitionCollection ->addDefinition ('ConcreteType1 ' , ['argument1 ' , 'argument2 ' ]);
161
+ $ definitionCollection ->addDefinition ('ConcreteType2 ' , ['argument1 ' , 'argument2 ' ]);
162
+ $ definitionCollection ->addDefinition ('Interface1 ' , [null ]);
163
+
164
+ return $ definitionCollection ;
165
+ }
166
+
167
+ /**
168
+ * @return array
169
+ */
170
+ private function getVirtualTypes ()
171
+ {
172
+ return ['virtualType1 ' => 'ConcreteType1 ' ];
173
+ }
174
+
175
+ /**
176
+ * @return array
177
+ */
178
+ private function getExpectedNonShared ()
179
+ {
180
+ return [
181
+ ['ConcreteType1 ' , true ],
182
+ ['ConcreteType2 ' , false ],
183
+ ['Interface1 ' , true ],
184
+ ['ThirdPartyInterface ' , false ]
185
+ ];
186
+ }
187
+
188
+ /**
189
+ * @return array
190
+ */
191
+ private function getPreferences ()
192
+ {
193
+ return [
194
+ 'Interface1 ' => 'ConcreteType1 ' ,
195
+ 'ThirdPartyInterface ' => 'ConcreteType2 '
196
+ ];
197
+ }
198
+
199
+ /**
200
+ * @return array
201
+ */
202
+ private function getPreferencesMap ()
203
+ {
204
+ return [
205
+ ['ConcreteType1 ' , 'ConcreteType1 ' ],
206
+ ['ConcreteType2 ' , 'ConcreteType2 ' ],
207
+ ['Interface1 ' , 'ConcreteType1 ' ],
208
+ ['ThirdPartyInterface ' , 'ConcreteType2 ' ]
209
+ ];
210
+ }
211
+
212
+ /**
213
+ * @param array $arguments
214
+ * @return array|null
215
+ */
216
+ private function getResolvedArguments ($ arguments )
217
+ {
218
+ return empty ($ arguments ) ? null : array_map (
219
+ function ($ argument ) {
220
+ return 'resolved_ ' . $ argument ;
221
+ },
222
+ $ arguments
223
+ );
224
+ }
225
+
226
+ /**
227
+ * @param array $virtualTypes
228
+ * @return array
229
+ */
230
+ private function getInstanceTypeMap ($ virtualTypes )
231
+ {
232
+ $ getInstanceTypeMap = [];
233
+ foreach ($ virtualTypes as $ virtualType => $ concreteType ) {
234
+ $ getInstanceTypeMap [] = [$ virtualType , $ concreteType ];
235
+ }
236
+
237
+ return $ getInstanceTypeMap ;
238
+ }
239
+
240
+ /**
241
+ * @param Collection $definitionCollection
242
+ * @param array $virtualTypes
243
+ * @return array
244
+ */
245
+ private function getResolvedVirtualConstructorArgumentsMap (Collection $ definitionCollection , array $ virtualTypes )
246
+ {
247
+ $ getResolvedConstructorArgumentsMap = [];
248
+ foreach ($ virtualTypes as $ virtualType => $ concreteType ) {
249
+ $ getResolvedConstructorArgumentsMap [] = [
250
+ $ virtualType ,
251
+ $ definitionCollection ->getInstanceArguments ($ concreteType ),
252
+ $ this ->getResolvedArguments (
253
+ $ definitionCollection ->getInstanceArguments ($ concreteType )
254
+ )
255
+ ];
256
+ }
257
+ return $ getResolvedConstructorArgumentsMap ;
141
258
}
142
259
}
0 commit comments