Skip to content

Commit 2c9392b

Browse files
author
Yevhen Miroshnychenko
committed
MAGETWO-92986: Write Logs for Failed Process of Generating Factories in Extensions
1 parent a005d07 commit 2c9392b

File tree

5 files changed

+143
-163
lines changed

5 files changed

+143
-163
lines changed

dev/tests/integration/testsuite/Magento/Framework/Code/GeneratorTest.php

Lines changed: 78 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66
namespace Magento\Framework\Code;
77

8+
use Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceFactoryGenerator;
89
use Magento\Framework\App\Filesystem\DirectoryList;
910
use Magento\Framework\Filesystem;
1011
use Magento\Framework\Interception\Code\Generator as InterceptionGenerator;
1112
use Magento\Framework\ObjectManager\Code\Generator as DIGenerator;
12-
use Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceFactoryGenerator;
1313
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
1415

1516
require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespace.php';
1617
require_once __DIR__ . '/GeneratorTest/ParentClassWithNamespace.php';
@@ -20,83 +21,74 @@
2021
* @magentoAppIsolation enabled
2122
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2223
*/
23-
class GeneratorTest extends \PHPUnit\Framework\TestCase
24+
class GeneratorTest extends TestCase
2425
{
25-
const CLASS_NAME_WITH_NAMESPACE = \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace::class;
26+
const CLASS_NAME_WITH_NAMESPACE = GeneratorTest\SourceClassWithNamespace::class;
2627

2728
/**
28-
* @var \Magento\Framework\Code\Generator
29+
* @var Generator
2930
*/
3031
protected $_generator;
3132

3233
/**
33-
* @var \Magento\Framework\Code\Generator\Io
34+
* @var Generator/Io
3435
*/
3536
protected $_ioObject;
3637

3738
/**
38-
* @var \Magento\Framework\Filesystem\Directory\Write
39+
* @var Filesystem\Directory\Write
3940
*/
40-
protected $generatedDirectory;
41+
private $generatedDirectory;
4142

4243
/**
43-
* @var \Magento\Framework\Filesystem\Directory\Read
44+
* @var Filesystem\Directory\Read
4445
*/
4546
private $logDirectory;
4647

4748
/**
48-
* @var int
49+
* @var string
4950
*/
50-
private $oldPermissionGenerationDir;
51+
private $testRelativePath = './Magento/Framework/Code/GeneratorTest/';
5152

5253
/**
5354
* @inheritdoc
5455
*/
5556
protected function setUp()
5657
{
57-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
58+
$objectManager = Bootstrap::getObjectManager();
5859
/** @var Filesystem $filesystem */
59-
$filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
60+
$filesystem = $objectManager->get(Filesystem::class);
6061
$this->generatedDirectory = $filesystem->getDirectoryWrite(DirectoryList::GENERATED_CODE);
6162
$this->logDirectory = $filesystem->getDirectoryRead(DirectoryList::LOG);
62-
$generatedDirectory = $this->generatedDirectory->getAbsolutePath();
63-
$this->_ioObject = new \Magento\Framework\Code\Generator\Io(
64-
new \Magento\Framework\Filesystem\Driver\File(),
65-
$generatedDirectory
66-
);
63+
$generatedDirectoryAbsolutePath = $this->generatedDirectory->getAbsolutePath();
64+
$this->_ioObject = new Generator\Io(new Filesystem\Driver\File(), $generatedDirectoryAbsolutePath);
6765
$this->_generator = $objectManager->create(
68-
\Magento\Framework\Code\Generator::class,
66+
Generator::class,
6967
[
7068
'ioObject' => $this->_ioObject,
7169
'generatedEntities' => [
7270
ExtensionAttributesInterfaceFactoryGenerator::ENTITY_TYPE =>
7371
ExtensionAttributesInterfaceFactoryGenerator::class,
74-
DIGenerator\Factory::ENTITY_TYPE => \Magento\Framework\ObjectManager\Code\Generator\Factory::class,
75-
DIGenerator\Proxy::ENTITY_TYPE => \Magento\Framework\ObjectManager\Code\Generator\Proxy::class,
76-
InterceptionGenerator\Interceptor::ENTITY_TYPE =>
77-
\Magento\Framework\Interception\Code\Generator\Interceptor::class,
72+
DIGenerator\Factory::ENTITY_TYPE => DIGenerator\Factory::class,
73+
DIGenerator\Proxy::ENTITY_TYPE => DIGenerator\Proxy::class,
74+
InterceptionGenerator\Interceptor::ENTITY_TYPE => InterceptionGenerator\Interceptor::class,
7875
]
7976
]
8077
);
8178
$this->_generator->setObjectManager($objectManager);
82-
83-
if (file_exists($generatedDirectory)) {
84-
$this->oldPermissionGenerationDir = fileperms($generatedDirectory);
85-
}
8679
}
8780

81+
/**
82+
* @inheritdoc
83+
*/
8884
protected function tearDown()
8985
{
90-
$this->generatedDirectory->changePermissionsRecursively('./', 0777, 0666);
91-
$this->generatedDirectory->delete();
9286
$this->_generator = null;
93-
94-
if ($this->oldPermissionGenerationDir) {
95-
mkdir(
96-
$this->generatedDirectory->getAbsolutePath(),
97-
$this->oldPermissionGenerationDir,
98-
true
99-
);
87+
if ($this->generatedDirectory->isExist($this->testRelativePath)) {
88+
if (!$this->generatedDirectory->isWritable($this->testRelativePath)) {
89+
$this->generatedDirectory->changePermissionsRecursively($this->testRelativePath, 0775, 0664);
90+
}
91+
$this->generatedDirectory->delete($this->testRelativePath);
10092
}
10193
}
10294

@@ -105,81 +97,59 @@ protected function _clearDocBlock($classBody)
10597
return preg_replace('/(\/\*[\w\W]*)\nclass/', 'class', $classBody);
10698
}
10799

100+
/**
101+
* Generates a new file with Factory class and compares with the sample from the
102+
* SourceClassWithNamespaceFactory.php.sample file.
103+
*/
108104
public function testGenerateClassFactoryWithNamespace()
109105
{
110106
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'Factory';
111-
$result = false;
112-
$generatorResult = $this->_generator->generateClass($factoryClassName);
113-
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
114-
$result = true;
115-
}
116-
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
117-
118-
$factory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($factoryClassName);
119-
120-
$object = $factory->create();
121-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $object);
122-
123-
// This test is only valid if the factory created the object if Autoloader did not pick it up automatically
124-
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
125-
$content = $this->_clearDocBlock(
126-
file_get_contents(
127-
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . 'Factory')
128-
)
129-
);
130-
$expectedContent = $this->_clearDocBlock(
131-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceFactory.php.sample')
132-
);
133-
$this->assertEquals($expectedContent, $content);
134-
}
107+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
108+
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
109+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $factory->create());
110+
$content = $this->_clearDocBlock(
111+
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
112+
);
113+
$expectedContent = $this->_clearDocBlock(
114+
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceFactory.php.sample')
115+
);
116+
$this->assertEquals($expectedContent, $content);
135117
}
136118

119+
/**
120+
* Generates a new file with Proxy class and compares with the sample from the
121+
* SourceClassWithNamespaceProxy.php.sample file.
122+
*/
137123
public function testGenerateClassProxyWithNamespace()
138124
{
139125
$proxyClassName = self::CLASS_NAME_WITH_NAMESPACE . '\Proxy';
140-
$result = false;
141-
$generatorResult = $this->_generator->generateClass($proxyClassName);
142-
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
143-
$result = true;
144-
}
145-
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
146-
147-
$proxy = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($proxyClassName);
126+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($proxyClassName));
127+
$proxy = Bootstrap::getObjectManager()->create($proxyClassName);
148128
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $proxy);
149-
150-
// This test is only valid if the factory created the object if Autoloader did not pick it up automatically
151-
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
152-
$content = $this->_clearDocBlock(
153-
file_get_contents($this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . '\Proxy'))
154-
);
155-
$expectedContent = $this->_clearDocBlock(
156-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceProxy.php.sample')
157-
);
158-
$this->assertEquals($expectedContent, $content);
159-
}
129+
$content = $this->_clearDocBlock(
130+
file_get_contents($this->_ioObject->generateResultFileName($proxyClassName))
131+
);
132+
$expectedContent = $this->_clearDocBlock(
133+
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceProxy.php.sample')
134+
);
135+
$this->assertEquals($expectedContent, $content);
160136
}
161137

138+
/**
139+
* Generates a new file with Interceptor class and compares with the sample from the
140+
* SourceClassWithNamespaceInterceptor.php.sample file.
141+
*/
162142
public function testGenerateClassInterceptorWithNamespace()
163143
{
164144
$interceptorClassName = self::CLASS_NAME_WITH_NAMESPACE . '\Interceptor';
165-
$result = false;
166-
$generatorResult = $this->_generator->generateClass($interceptorClassName);
167-
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
168-
$result = true;
169-
}
170-
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
171-
172-
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
173-
$content = $this->_clearDocBlock(
174-
file_get_contents(
175-
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . '\Interceptor')
176-
)
177-
);
178-
$expectedContent = $this->_clearDocBlock(
179-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceInterceptor.php.sample')
180-
);
181-
$this->assertEquals($expectedContent, $content);
182-
}
145+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($interceptorClassName));
146+
$content = $this->_clearDocBlock(
147+
file_get_contents($this->_ioObject->generateResultFileName($interceptorClassName))
148+
);
149+
$expectedContent = $this->_clearDocBlock(
150+
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceInterceptor.php.sample')
151+
);
152+
$this->assertEquals($expectedContent, $content);
183153
}
184154

185155
/**
@@ -189,45 +159,35 @@ public function testGenerateClassInterceptorWithNamespace()
189159
public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace()
190160
{
191161
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory';
192-
$this->generatedDirectory->create(
193-
$this->generatedDirectory->getAbsolutePath() . '/Magento/Framework/Code/GeneratorTest/'
194-
);
195-
196-
$generatorResult = $this->_generator->generateClass($factoryClassName);
197-
162+
$this->generatedDirectory->create($this->testRelativePath);
163+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
198164
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
199-
$object = $factory->create();
200-
201-
$this->assertEquals($generatorResult, Generator::GENERATION_SUCCESS);
202-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE . 'Extension', $object);
203-
165+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE . 'Extension', $factory->create());
204166
$content = $this->_clearDocBlock(
205-
file_get_contents(
206-
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory')
207-
)
167+
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
208168
);
209169
$expectedContent = $this->_clearDocBlock(
210170
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample')
211171
);
212172
$this->assertEquals($expectedContent, $content);
213173
}
214174

175+
/**
176+
* It tries to generate a new class file if the generated directory is read-only
177+
*/
215178
public function testGeneratorClassWithErrorSaveClassFile()
216179
{
217180
$msgPart = 'Error: an object of a generated class may be a dependency for another object, '
218181
. 'but this dependency has not been defined or set correctly in the signature of the related construct '
219182
. 'method';
220-
221183
$this->expectException(\RuntimeException::class);
222-
$this->expectExceptionMessageRegExp("/^$msgPart.*/");
223-
$this->generatedDirectory->changePermissionsRecursively('./', 0555, 0444);
224-
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory';
225-
184+
$this->expectExceptionMessageRegExp("/.*$msgPart.*/");
185+
$this->generatedDirectory->create($this->testRelativePath);
186+
$this->generatedDirectory->changePermissionsRecursively($this->testRelativePath, 0555, 0444);
187+
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'Factory';
226188
$generatorResult = $this->_generator->generateClass($factoryClassName);
227189
$this->assertFalse($generatorResult);
228-
229190
$pathToSystemLog = $this->logDirectory->getAbsolutePath('system.log');
230-
$logs = array_slice(file($pathToSystemLog), -2);
231-
$this->assertContains($msgPart, $logs[0]);
191+
$this->assertContains($msgPart, file_get_contents($pathToSystemLog));
232192
}
233193
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Framework\Api\Code\Generator;
87

98
use Magento\Framework\ObjectManager\Code\Generator\Factory;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Framework\Exception\FileSystemException;
99
use Zend\Code\Generator\ValueGenerator;
10+
use Magento\Framework\Filesystem\Driver\File;
11+
use Magento\Framework\ObjectManagerInterface;
1012

1113
abstract class EntityAbstract
1214
{
@@ -42,7 +44,7 @@ abstract class EntityAbstract
4244
/**
4345
* Class generator object
4446
*
45-
* @var \Magento\Framework\Code\Generator\CodeGeneratorInterface
47+
* @var CodeGeneratorInterface
4648
*/
4749
protected $_classGenerator;
4850

@@ -55,20 +57,20 @@ abstract class EntityAbstract
5557
* @param null|string $sourceClassName
5658
* @param null|string $resultClassName
5759
* @param Io $ioObject
58-
* @param \Magento\Framework\Code\Generator\CodeGeneratorInterface $classGenerator
60+
* @param CodeGeneratorInterface $classGenerator
5961
* @param DefinedClasses $definedClasses
6062
*/
6163
public function __construct(
6264
$sourceClassName = null,
6365
$resultClassName = null,
6466
Io $ioObject = null,
65-
\Magento\Framework\Code\Generator\CodeGeneratorInterface $classGenerator = null,
67+
CodeGeneratorInterface $classGenerator = null,
6668
DefinedClasses $definedClasses = null
6769
) {
6870
if ($ioObject) {
6971
$this->_ioObject = $ioObject;
7072
} else {
71-
$this->_ioObject = new Io(new \Magento\Framework\Filesystem\Driver\File());
73+
$this->_ioObject = new Io(new File());
7274
}
7375
if ($classGenerator) {
7476
$this->_classGenerator = $classGenerator;
@@ -201,7 +203,7 @@ protected function _getClassProperties()
201203
'visibility' => 'protected',
202204
'docblock' => [
203205
'shortDescription' => 'Object Manager instance',
204-
'tags' => [['name' => 'var', 'description' => '\\' . \Magento\Framework\ObjectManagerInterface::class]],
206+
'tags' => [['name' => 'var', 'description' => '\\' . ObjectManagerInterface::class]],
205207
],
206208
];
207209

0 commit comments

Comments
 (0)