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