3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Framework \Code \Test \Unit ;
7
9
8
10
use Magento \Framework \Code \Generator ;
9
11
use Magento \Framework \Code \Generator \DefinedClasses ;
10
12
use Magento \Framework \Code \Generator \Io ;
13
+ use Magento \Framework \ObjectManager \ConfigInterface ;
14
+ use PHPUnit \Framework \MockObject \MockObject ;
11
15
use Psr \Log \LoggerInterface ;
12
16
use Magento \Framework \ObjectManager \Code \Generator \Factory ;
13
17
use Magento \Framework \ObjectManager \Code \Generator \Proxy ;
17
21
use Magento \Framework \ObjectManagerInterface ;
18
22
use Magento \Framework \Code \Generator \EntityAbstract ;
19
23
use Magento \GeneratedClass \Factory as GeneratedClassFactory ;
24
+ use RuntimeException ;
20
25
21
26
class GeneratorTest extends TestCase
22
27
{
23
28
/**
24
29
* Class name parameter value
25
30
*/
26
- const SOURCE_CLASS = 'testClassName ' ;
31
+ private const SOURCE_CLASS = 'testClassName ' ;
27
32
28
33
/**
29
34
* Expected generated entities
@@ -58,13 +63,32 @@ class GeneratorTest extends TestCase
58
63
*/
59
64
private $ loggerMock ;
60
65
66
+ /**
67
+ * @var ObjectManagerInterface|MockObject
68
+ */
69
+ private $ objectManagerMock ;
70
+
71
+ /**
72
+ * @var ConfigInterface|MockObject
73
+ */
74
+ private $ objectManagerConfigMock ;
75
+
76
+ /**
77
+ * @inheritDoc
78
+ */
61
79
protected function setUp ()
62
80
{
63
81
$ this ->definedClassesMock = $ this ->createMock (DefinedClasses::class);
64
82
$ this ->ioObjectMock = $ this ->getMockBuilder (Io::class)
65
83
->disableOriginalConstructor ()
66
84
->getMock ();
67
85
$ this ->loggerMock = $ this ->getMockForAbstractClass (LoggerInterface::class);
86
+ $ this ->objectManagerMock = $ this ->getMockBuilder (ObjectManagerInterface::class)
87
+ ->disableOriginalConstructor ()
88
+ ->getMock ();
89
+ $ this ->objectManagerConfigMock = $ this ->getMockBuilder (ConfigInterface::class)
90
+ ->disableOriginalConstructor ()
91
+ ->getMock ();
68
92
69
93
$ this ->model = new Generator (
70
94
$ this ->ioObjectMock ,
@@ -78,7 +102,7 @@ protected function setUp()
78
102
);
79
103
}
80
104
81
- public function testGetGeneratedEntities ()
105
+ public function testGetGeneratedEntities (): void
82
106
{
83
107
$ this ->model = new Generator (
84
108
$ this ->ioObjectMock ,
@@ -91,22 +115,58 @@ public function testGetGeneratedEntities()
91
115
/**
92
116
* @param string $className
93
117
* @param string $entityType
94
- * @expectedException \ RuntimeException
118
+ * @expectedException RuntimeException
95
119
* @dataProvider generateValidClassDataProvider
96
120
*/
97
- public function testGenerateClass ($ className , $ entityType )
121
+ public function testGenerateClass ($ className , $ entityType ): void
98
122
{
99
- $ objectManagerMock = $ this ->createMock (ObjectManagerInterface::class);
100
123
$ fullClassName = $ className . $ entityType ;
124
+
101
125
$ entityGeneratorMock = $ this ->getMockBuilder (EntityAbstract::class)
102
126
->disableOriginalConstructor ()
103
127
->getMock ();
104
- $ objectManagerMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ entityGeneratorMock );
105
- $ this ->model ->setObjectManager ($ objectManagerMock );
106
- $ this ->model ->generateClass ($ fullClassName );
128
+ $ this ->objectManagerMock
129
+ ->expects ($ this ->once ())
130
+ ->method ('create ' )
131
+ ->willReturn ($ entityGeneratorMock );
132
+
133
+ $ this ->objectManagerConfigMock
134
+ ->expects ($ this ->once ())
135
+ ->method ('getVirtualTypes ' )
136
+ ->willReturn ([]);
137
+ $ this ->objectManagerMock
138
+ ->expects ($ this ->once ())
139
+ ->method ('get ' )
140
+ ->with (ConfigInterface::class)
141
+ ->willReturn ($ this ->objectManagerConfigMock );
142
+ $ this ->model ->setObjectManager ($ this ->objectManagerMock );
143
+
144
+ $ this ->assertSame (
145
+ Generator::GENERATION_SUCCESS ,
146
+ $ this ->model ->generateClass (GeneratedClassFactory::class)
147
+ );
107
148
}
108
149
109
- public function testGenerateClassWithWrongName ()
150
+ public function testShouldNotGenerateVirtualType (): void
151
+ {
152
+ $ this ->objectManagerConfigMock
153
+ ->expects ($ this ->once ())
154
+ ->method ('getVirtualTypes ' )
155
+ ->willReturn ([GeneratedClassFactory::class => GeneratedClassFactory::class]);
156
+ $ this ->objectManagerMock
157
+ ->expects ($ this ->once ())
158
+ ->method ('get ' )
159
+ ->with (ConfigInterface::class)
160
+ ->willReturn ($ this ->objectManagerConfigMock );
161
+ $ this ->model ->setObjectManager ($ this ->objectManagerMock );
162
+
163
+ $ this ->assertSame (
164
+ Generator::GENERATION_SKIP ,
165
+ $ this ->model ->generateClass (GeneratedClassFactory::class)
166
+ );
167
+ }
168
+
169
+ public function testGenerateClassWithWrongName (): void
110
170
{
111
171
$ this ->assertEquals (
112
172
Generator::GENERATION_ERROR ,
@@ -115,25 +175,42 @@ public function testGenerateClassWithWrongName()
115
175
}
116
176
117
177
/**
118
- * @expectedException \ RuntimeException
178
+ * @expectedException RuntimeException
119
179
*/
120
- public function testGenerateClassWhenClassIsNotGenerationSuccess ()
180
+ public function testGenerateClassWhenClassIsNotGenerationSuccess (): void
121
181
{
122
182
$ expectedEntities = array_values ($ this ->expectedEntities );
123
183
$ resultClassName = self ::SOURCE_CLASS . ucfirst (array_shift ($ expectedEntities ));
124
- $ objectManagerMock = $ this -> createMock (ObjectManagerInterface::class);
184
+
125
185
$ entityGeneratorMock = $ this ->getMockBuilder (EntityAbstract::class)
126
186
->disableOriginalConstructor ()
127
187
->getMock ();
128
- $ objectManagerMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ entityGeneratorMock );
129
- $ this ->model ->setObjectManager ($ objectManagerMock );
130
- $ this ->model ->generateClass ($ resultClassName );
188
+ $ this ->objectManagerMock
189
+ ->expects ($ this ->once ())
190
+ ->method ('create ' )
191
+ ->willReturn ($ entityGeneratorMock );
192
+
193
+ $ this ->objectManagerConfigMock
194
+ ->expects ($ this ->once ())
195
+ ->method ('getVirtualTypes ' )
196
+ ->willReturn ([]);
197
+ $ this ->objectManagerMock
198
+ ->expects ($ this ->once ())
199
+ ->method ('get ' )
200
+ ->with (ConfigInterface::class)
201
+ ->willReturn ($ this ->objectManagerConfigMock );
202
+ $ this ->model ->setObjectManager ($ this ->objectManagerMock );
203
+
204
+ $ this ->assertSame (
205
+ Generator::GENERATION_SUCCESS ,
206
+ $ this ->model ->generateClass ($ resultClassName )
207
+ );
131
208
}
132
209
133
210
/**
134
211
* @inheritdoc
135
212
*/
136
- public function testGenerateClassWithErrors ()
213
+ public function testGenerateClassWithErrors (): void
137
214
{
138
215
$ expectedEntities = array_values ($ this ->expectedEntities );
139
216
$ resultClassName = self ::SOURCE_CLASS . ucfirst (array_shift ($ expectedEntities ));
@@ -148,17 +225,15 @@ public function testGenerateClassWithErrors()
148
225
. 'directory permission is set to write --- the requested class did not generate properly, then '
149
226
. 'you must add the generated class object to the signature of the related construct method, only. ' ;
150
227
$ FinalErrorMessage = implode (PHP_EOL , $ errorMessages ) . "\n" . $ mainErrorMessage ;
151
- $ this ->expectException (\ RuntimeException::class);
228
+ $ this ->expectException (RuntimeException::class);
152
229
$ this ->expectExceptionMessage ($ FinalErrorMessage );
153
230
154
- /** @var ObjectManagerInterface|Mock $objectManagerMock */
155
- $ objectManagerMock = $ this ->createMock (ObjectManagerInterface::class);
156
231
/** @var EntityAbstract|Mock $entityGeneratorMock */
157
232
$ entityGeneratorMock = $ this ->getMockBuilder (EntityAbstract::class)
158
233
->disableOriginalConstructor ()
159
234
->getMock ();
160
235
161
- $ objectManagerMock ->expects ($ this ->once ())
236
+ $ this -> objectManagerMock ->expects ($ this ->once ())
162
237
->method ('create ' )
163
238
->willReturn ($ entityGeneratorMock );
164
239
$ entityGeneratorMock ->expects ($ this ->once ())
@@ -177,26 +252,62 @@ public function testGenerateClassWithErrors()
177
252
$ this ->loggerMock ->expects ($ this ->once ())
178
253
->method ('critical ' )
179
254
->with ($ FinalErrorMessage );
180
- $ this ->model ->setObjectManager ($ objectManagerMock );
181
- $ this ->model ->generateClass ($ resultClassName );
255
+
256
+ $ this ->objectManagerConfigMock
257
+ ->expects ($ this ->once ())
258
+ ->method ('getVirtualTypes ' )
259
+ ->willReturn ([]);
260
+ $ this ->objectManagerMock
261
+ ->expects ($ this ->once ())
262
+ ->method ('get ' )
263
+ ->with (ConfigInterface::class)
264
+ ->willReturn ($ this ->objectManagerConfigMock );
265
+ $ this ->model ->setObjectManager ($ this ->objectManagerMock );
266
+
267
+ $ this ->assertSame (
268
+ Generator::GENERATION_SUCCESS ,
269
+ $ this ->model ->generateClass ($ resultClassName )
270
+ );
182
271
}
183
272
184
273
/**
185
274
* @dataProvider trueFalseDataProvider
275
+ * @param $fileExists
186
276
*/
187
- public function testGenerateClassWithExistName ($ fileExists )
277
+ public function testGenerateClassWithExistName ($ fileExists ): void
188
278
{
189
279
$ this ->definedClassesMock ->expects ($ this ->any ())
190
280
->method ('isClassLoadableFromDisk ' )
191
281
->willReturn (true );
192
282
193
283
$ resultClassFileName = '/Magento/Path/To/Class.php ' ;
194
- $ this ->ioObjectMock ->expects ($ this ->once ())->method ('generateResultFileName ' )->willReturn ($ resultClassFileName );
195
- $ this ->ioObjectMock ->expects ($ this ->once ())->method ('fileExists ' )->willReturn ($ fileExists );
284
+
285
+ $ this ->objectManagerConfigMock
286
+ ->expects ($ this ->once ())
287
+ ->method ('getVirtualTypes ' )
288
+ ->willReturn ([]);
289
+ $ this ->objectManagerMock
290
+ ->expects ($ this ->once ())
291
+ ->method ('get ' )
292
+ ->with (ConfigInterface::class)
293
+ ->willReturn ($ this ->objectManagerConfigMock );
294
+ $ this ->model ->setObjectManager ($ this ->objectManagerMock );
295
+
296
+ $ this ->ioObjectMock
297
+ ->expects ($ this ->once ())
298
+ ->method ('generateResultFileName ' )
299
+ ->willReturn ($ resultClassFileName );
300
+ $ this ->ioObjectMock
301
+ ->expects ($ this ->once ())
302
+ ->method ('fileExists ' )
303
+ ->willReturn ($ fileExists );
304
+
196
305
$ includeFileInvokeCount = $ fileExists ? 1 : 0 ;
197
- $ this ->ioObjectMock ->expects ($ this ->exactly ($ includeFileInvokeCount ))->method ('includeFile ' );
306
+ $ this ->ioObjectMock
307
+ ->expects ($ this ->exactly ($ includeFileInvokeCount ))
308
+ ->method ('includeFile ' );
198
309
199
- $ this ->assertEquals (
310
+ $ this ->assertSame (
200
311
Generator::GENERATION_SKIP ,
201
312
$ this ->model ->generateClass (GeneratedClassFactory::class)
202
313
);
@@ -205,7 +316,7 @@ public function testGenerateClassWithExistName($fileExists)
205
316
/**
206
317
* @return array
207
318
*/
208
- public function trueFalseDataProvider ()
319
+ public function trueFalseDataProvider (): array
209
320
{
210
321
return [[true ], [false ]];
211
322
}
@@ -215,7 +326,7 @@ public function trueFalseDataProvider()
215
326
*
216
327
* @return array
217
328
*/
218
- public function generateValidClassDataProvider ()
329
+ public function generateValidClassDataProvider (): array
219
330
{
220
331
$ data = [];
221
332
foreach ($ this ->expectedEntities as $ generatedEntity ) {
0 commit comments