Skip to content

Commit 75684ca

Browse files
committed
Added test coverage for virtual types code generation
1 parent fa079c9 commit 75684ca

File tree

1 file changed

+140
-29
lines changed

1 file changed

+140
-29
lines changed

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

Lines changed: 140 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,14 @@
1721
use Magento\Framework\ObjectManagerInterface;
1822
use Magento\Framework\Code\Generator\EntityAbstract;
1923
use Magento\GeneratedClass\Factory as GeneratedClassFactory;
24+
use RuntimeException;
2025

2126
class GeneratorTest extends TestCase
2227
{
2328
/**
2429
* Class name parameter value
2530
*/
26-
const SOURCE_CLASS = 'testClassName';
31+
private const SOURCE_CLASS = 'testClassName';
2732

2833
/**
2934
* Expected generated entities
@@ -58,13 +63,32 @@ class GeneratorTest extends TestCase
5863
*/
5964
private $loggerMock;
6065

66+
/**
67+
* @var ObjectManagerInterface|MockObject
68+
*/
69+
private $objectManagerMock;
70+
71+
/**
72+
* @var ConfigInterface|MockObject
73+
*/
74+
private $objectManagerConfigMock;
75+
76+
/**
77+
* @inheritDoc
78+
*/
6179
protected function setUp()
6280
{
6381
$this->definedClassesMock = $this->createMock(DefinedClasses::class);
6482
$this->ioObjectMock = $this->getMockBuilder(Io::class)
6583
->disableOriginalConstructor()
6684
->getMock();
6785
$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();
6892

6993
$this->model = new Generator(
7094
$this->ioObjectMock,
@@ -78,7 +102,7 @@ protected function setUp()
78102
);
79103
}
80104

81-
public function testGetGeneratedEntities()
105+
public function testGetGeneratedEntities(): void
82106
{
83107
$this->model = new Generator(
84108
$this->ioObjectMock,
@@ -91,22 +115,58 @@ public function testGetGeneratedEntities()
91115
/**
92116
* @param string $className
93117
* @param string $entityType
94-
* @expectedException \RuntimeException
118+
* @expectedException RuntimeException
95119
* @dataProvider generateValidClassDataProvider
96120
*/
97-
public function testGenerateClass($className, $entityType)
121+
public function testGenerateClass($className, $entityType): void
98122
{
99-
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
100123
$fullClassName = $className . $entityType;
124+
101125
$entityGeneratorMock = $this->getMockBuilder(EntityAbstract::class)
102126
->disableOriginalConstructor()
103127
->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+
);
107148
}
108149

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
110170
{
111171
$this->assertEquals(
112172
Generator::GENERATION_ERROR,
@@ -115,25 +175,42 @@ public function testGenerateClassWithWrongName()
115175
}
116176

117177
/**
118-
* @expectedException \RuntimeException
178+
* @expectedException RuntimeException
119179
*/
120-
public function testGenerateClassWhenClassIsNotGenerationSuccess()
180+
public function testGenerateClassWhenClassIsNotGenerationSuccess(): void
121181
{
122182
$expectedEntities = array_values($this->expectedEntities);
123183
$resultClassName = self::SOURCE_CLASS . ucfirst(array_shift($expectedEntities));
124-
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
184+
125185
$entityGeneratorMock = $this->getMockBuilder(EntityAbstract::class)
126186
->disableOriginalConstructor()
127187
->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+
);
131208
}
132209

133210
/**
134211
* @inheritdoc
135212
*/
136-
public function testGenerateClassWithErrors()
213+
public function testGenerateClassWithErrors(): void
137214
{
138215
$expectedEntities = array_values($this->expectedEntities);
139216
$resultClassName = self::SOURCE_CLASS . ucfirst(array_shift($expectedEntities));
@@ -148,17 +225,15 @@ public function testGenerateClassWithErrors()
148225
. 'directory permission is set to write --- the requested class did not generate properly, then '
149226
. 'you must add the generated class object to the signature of the related construct method, only.';
150227
$FinalErrorMessage = implode(PHP_EOL, $errorMessages) . "\n" . $mainErrorMessage;
151-
$this->expectException(\RuntimeException::class);
228+
$this->expectException(RuntimeException::class);
152229
$this->expectExceptionMessage($FinalErrorMessage);
153230

154-
/** @var ObjectManagerInterface|Mock $objectManagerMock */
155-
$objectManagerMock = $this->createMock(ObjectManagerInterface::class);
156231
/** @var EntityAbstract|Mock $entityGeneratorMock */
157232
$entityGeneratorMock = $this->getMockBuilder(EntityAbstract::class)
158233
->disableOriginalConstructor()
159234
->getMock();
160235

161-
$objectManagerMock->expects($this->once())
236+
$this->objectManagerMock->expects($this->once())
162237
->method('create')
163238
->willReturn($entityGeneratorMock);
164239
$entityGeneratorMock->expects($this->once())
@@ -177,26 +252,62 @@ public function testGenerateClassWithErrors()
177252
$this->loggerMock->expects($this->once())
178253
->method('critical')
179254
->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+
);
182271
}
183272

184273
/**
185274
* @dataProvider trueFalseDataProvider
275+
* @param $fileExists
186276
*/
187-
public function testGenerateClassWithExistName($fileExists)
277+
public function testGenerateClassWithExistName($fileExists): void
188278
{
189279
$this->definedClassesMock->expects($this->any())
190280
->method('isClassLoadableFromDisk')
191281
->willReturn(true);
192282

193283
$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+
196305
$includeFileInvokeCount = $fileExists ? 1 : 0;
197-
$this->ioObjectMock->expects($this->exactly($includeFileInvokeCount))->method('includeFile');
306+
$this->ioObjectMock
307+
->expects($this->exactly($includeFileInvokeCount))
308+
->method('includeFile');
198309

199-
$this->assertEquals(
310+
$this->assertSame(
200311
Generator::GENERATION_SKIP,
201312
$this->model->generateClass(GeneratedClassFactory::class)
202313
);
@@ -205,7 +316,7 @@ public function testGenerateClassWithExistName($fileExists)
205316
/**
206317
* @return array
207318
*/
208-
public function trueFalseDataProvider()
319+
public function trueFalseDataProvider(): array
209320
{
210321
return [[true], [false]];
211322
}
@@ -215,7 +326,7 @@ public function trueFalseDataProvider()
215326
*
216327
* @return array
217328
*/
218-
public function generateValidClassDataProvider()
329+
public function generateValidClassDataProvider(): array
219330
{
220331
$data = [];
221332
foreach ($this->expectedEntities as $generatedEntity) {

0 commit comments

Comments
 (0)