Skip to content

Commit d89b08f

Browse files
authored
ENGCOM-4620: [WIP] FIX for issue #21916 - Elasticsearch6 generation does not exist #22046
2 parents 637e6a4 + 0f5f4c4 commit d89b08f

File tree

2 files changed

+168
-31
lines changed

2 files changed

+168
-31
lines changed

lib/internal/Magento/Framework/Code/Generator.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
use Magento\Framework\Code\Generator\DefinedClasses;
99
use Magento\Framework\Code\Generator\EntityAbstract;
1010
use Magento\Framework\Code\Generator\Io;
11+
use Magento\Framework\ObjectManager\ConfigInterface;
1112
use Magento\Framework\ObjectManagerInterface;
1213
use Magento\Framework\Phrase;
1314
use Magento\Framework\Filesystem\Driver\File;
1415
use Psr\Log\LoggerInterface;
1516

17+
/**
18+
* Class code generator.
19+
*/
1620
class Generator
1721
{
1822
const GENERATION_SUCCESS = 'success';
@@ -232,7 +236,21 @@ protected function shouldSkipGeneration($resultEntityType, $sourceClassName, $re
232236
{
233237
if (!$resultEntityType || !$sourceClassName) {
234238
return self::GENERATION_ERROR;
235-
} elseif ($this->definedClasses->isClassLoadableFromDisk($resultClass)) {
239+
}
240+
241+
/** @var ConfigInterface $omConfig */
242+
$omConfig = $this->objectManager->get(ConfigInterface::class);
243+
$virtualTypes = $omConfig->getVirtualTypes();
244+
245+
/**
246+
* Do not try to autogenerate virtual types
247+
* For example virtual types with names overlapping autogenerated suffixes
248+
*/
249+
if (isset($virtualTypes[$resultClass])) {
250+
return self::GENERATION_SKIP;
251+
}
252+
253+
if ($this->definedClasses->isClassLoadableFromDisk($resultClass)) {
236254
$generatedFileName = $this->_ioObject->generateResultFileName($resultClass);
237255
/**
238256
* Must handle two edge cases: a competing process has generated the class and written it to disc already,
@@ -244,9 +262,12 @@ protected function shouldSkipGeneration($resultEntityType, $sourceClassName, $re
244262
$this->_ioObject->includeFile($generatedFileName);
245263
}
246264
return self::GENERATION_SKIP;
247-
} elseif (!isset($this->_generatedEntities[$resultEntityType])) {
265+
}
266+
267+
if (!isset($this->_generatedEntities[$resultEntityType])) {
248268
throw new \InvalidArgumentException('Unknown generation entity.');
249269
}
270+
250271
return false;
251272
}
252273
}

lib/internal/Magento/Framework/Code/Test/Unit/GeneratorTest.php

Lines changed: 145 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\Code\Test\Unit;
79

810
use Magento\Framework\Code\Generator;
911
use Magento\Framework\Code\Generator\DefinedClasses;
1012
use Magento\Framework\Code\Generator\Io;
13+
use Magento\Framework\ObjectManager\ConfigInterface;
14+
use PHPUnit\Framework\MockObject\MockObject;
1115
use Psr\Log\LoggerInterface;
1216
use Magento\Framework\ObjectManager\Code\Generator\Factory;
1317
use Magento\Framework\ObjectManager\Code\Generator\Proxy;
@@ -17,13 +21,19 @@
1721
use Magento\Framework\ObjectManagerInterface;
1822
use Magento\Framework\Code\Generator\EntityAbstract;
1923
use Magento\GeneratedClass\Factory as GeneratedClassFactory;
24+
use RuntimeException;
2025

26+
/**
27+
* Tests for code generator.
28+
*
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+
*/
2131
class GeneratorTest extends TestCase
2232
{
2333
/**
2434
* Class name parameter value
2535
*/
26-
const SOURCE_CLASS = 'testClassName';
36+
private const SOURCE_CLASS = 'testClassName';
2737

2838
/**
2939
* Expected generated entities
@@ -58,13 +68,32 @@ class GeneratorTest extends TestCase
5868
*/
5969
private $loggerMock;
6070

71+
/**
72+
* @var ObjectManagerInterface|MockObject
73+
*/
74+
private $objectManagerMock;
75+
76+
/**
77+
* @var ConfigInterface|MockObject
78+
*/
79+
private $objectManagerConfigMock;
80+
81+
/**
82+
* @inheritDoc
83+
*/
6184
protected function setUp()
6285
{
6386
$this->definedClassesMock = $this->createMock(DefinedClasses::class);
6487
$this->ioObjectMock = $this->getMockBuilder(Io::class)
6588
->disableOriginalConstructor()
6689
->getMock();
6790
$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();
6897

6998
$this->model = new Generator(
7099
$this->ioObjectMock,
@@ -78,7 +107,7 @@ protected function setUp()
78107
);
79108
}
80109

81-
public function testGetGeneratedEntities()
110+
public function testGetGeneratedEntities(): void
82111
{
83112
$this->model = new Generator(
84113
$this->ioObjectMock,
@@ -91,22 +120,58 @@ public function testGetGeneratedEntities()
91120
/**
92121
* @param string $className
93122
* @param string $entityType
94-
* @expectedException \RuntimeException
123+
* @expectedException RuntimeException
95124
* @dataProvider generateValidClassDataProvider
96125
*/
97-
public function testGenerateClass($className, $entityType)
126+
public function testGenerateClass($className, $entityType): void
98127
{
99-
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
100128
$fullClassName = $className . $entityType;
129+
101130
$entityGeneratorMock = $this->getMockBuilder(EntityAbstract::class)
102131
->disableOriginalConstructor()
103132
->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+
);
107153
}
108154

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
110175
{
111176
$this->assertEquals(
112177
Generator::GENERATION_ERROR,
@@ -115,25 +180,42 @@ public function testGenerateClassWithWrongName()
115180
}
116181

117182
/**
118-
* @expectedException \RuntimeException
183+
* @expectedException RuntimeException
119184
*/
120-
public function testGenerateClassWhenClassIsNotGenerationSuccess()
185+
public function testGenerateClassWhenClassIsNotGenerationSuccess(): void
121186
{
122187
$expectedEntities = array_values($this->expectedEntities);
123188
$resultClassName = self::SOURCE_CLASS . ucfirst(array_shift($expectedEntities));
124-
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
189+
125190
$entityGeneratorMock = $this->getMockBuilder(EntityAbstract::class)
126191
->disableOriginalConstructor()
127192
->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+
);
131213
}
132214

133215
/**
134216
* @inheritdoc
135217
*/
136-
public function testGenerateClassWithErrors()
218+
public function testGenerateClassWithErrors(): void
137219
{
138220
$expectedEntities = array_values($this->expectedEntities);
139221
$resultClassName = self::SOURCE_CLASS . ucfirst(array_shift($expectedEntities));
@@ -148,17 +230,15 @@ public function testGenerateClassWithErrors()
148230
. 'directory permission is set to write --- the requested class did not generate properly, then '
149231
. 'you must add the generated class object to the signature of the related construct method, only.';
150232
$FinalErrorMessage = implode(PHP_EOL, $errorMessages) . "\n" . $mainErrorMessage;
151-
$this->expectException(\RuntimeException::class);
233+
$this->expectException(RuntimeException::class);
152234
$this->expectExceptionMessage($FinalErrorMessage);
153235

154-
/** @var ObjectManagerInterface|Mock $objectManagerMock */
155-
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
156236
/** @var EntityAbstract|Mock $entityGeneratorMock */
157237
$entityGeneratorMock = $this->getMockBuilder(EntityAbstract::class)
158238
->disableOriginalConstructor()
159239
->getMock();
160240

161-
$objectManagerMock->expects($this->once())
241+
$this->objectManagerMock->expects($this->once())
162242
->method('create')
163243
->willReturn($entityGeneratorMock);
164244
$entityGeneratorMock->expects($this->once())
@@ -177,26 +257,62 @@ public function testGenerateClassWithErrors()
177257
$this->loggerMock->expects($this->once())
178258
->method('critical')
179259
->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+
);
182276
}
183277

184278
/**
185279
* @dataProvider trueFalseDataProvider
280+
* @param $fileExists
186281
*/
187-
public function testGenerateClassWithExistName($fileExists)
282+
public function testGenerateClassWithExistName($fileExists): void
188283
{
189284
$this->definedClassesMock->expects($this->any())
190285
->method('isClassLoadableFromDisk')
191286
->willReturn(true);
192287

193288
$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+
196310
$includeFileInvokeCount = $fileExists ? 1 : 0;
197-
$this->ioObjectMock->expects($this->exactly($includeFileInvokeCount))->method('includeFile');
311+
$this->ioObjectMock
312+
->expects($this->exactly($includeFileInvokeCount))
313+
->method('includeFile');
198314

199-
$this->assertEquals(
315+
$this->assertSame(
200316
Generator::GENERATION_SKIP,
201317
$this->model->generateClass(GeneratedClassFactory::class)
202318
);
@@ -205,7 +321,7 @@ public function testGenerateClassWithExistName($fileExists)
205321
/**
206322
* @return array
207323
*/
208-
public function trueFalseDataProvider()
324+
public function trueFalseDataProvider(): array
209325
{
210326
return [[true], [false]];
211327
}
@@ -215,7 +331,7 @@ public function trueFalseDataProvider()
215331
*
216332
* @return array
217333
*/
218-
public function generateValidClassDataProvider()
334+
public function generateValidClassDataProvider(): array
219335
{
220336
$data = [];
221337
foreach ($this->expectedEntities as $generatedEntity) {

0 commit comments

Comments
 (0)