Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 6178786

Browse files
authored
Merge pull request #2775 from magento-thunder/MAGETWO-92986
[thunder] MAGETWO-92986: Write Logs for Failed Process of Generating Factories in Extensions
2 parents 0c2fa6f + 0dfe22b commit 6178786

File tree

3 files changed

+258
-160
lines changed

3 files changed

+258
-160
lines changed

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

Lines changed: 105 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -5,155 +5,151 @@
55
*/
66
namespace Magento\Framework\Code;
77

8-
use Magento\Framework\Code\Generator;
8+
use Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceFactoryGenerator;
99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
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';
1718
require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespaceExtension.php';
1819

1920
/**
2021
* @magentoAppIsolation enabled
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2123
*/
22-
class GeneratorTest extends \PHPUnit\Framework\TestCase
24+
class GeneratorTest extends TestCase
2325
{
24-
const CLASS_NAME_WITH_NAMESPACE = \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace::class;
26+
const CLASS_NAME_WITH_NAMESPACE = GeneratorTest\SourceClassWithNamespace::class;
2527

2628
/**
27-
* @var \Magento\Framework\Code\Generator
29+
* @var Generator
2830
*/
2931
protected $_generator;
3032

3133
/**
32-
* @var \Magento\Framework\Code\Generator\Io
34+
* @var Generator/Io
3335
*/
3436
protected $_ioObject;
3537

3638
/**
37-
* @var \Magento\Framework\Filesystem\Directory\Write
39+
* @var Filesystem\Directory\Write
40+
*/
41+
private $generatedDirectory;
42+
43+
/**
44+
* @var Filesystem\Directory\Read
45+
*/
46+
private $logDirectory;
47+
48+
/**
49+
* @var string
3850
*/
39-
protected $varDirectory;
51+
private $testRelativePath = './Magento/Framework/Code/GeneratorTest/';
4052

53+
/**
54+
* @inheritdoc
55+
*/
4156
protected function setUp()
4257
{
43-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
44-
$this->varDirectory = $objectManager->get(
45-
\Magento\Framework\Filesystem::class
46-
)->getDirectoryWrite(
47-
DirectoryList::VAR_DIR
48-
);
49-
$generationDirectory = $this->varDirectory->getAbsolutePath('generation');
50-
$this->_ioObject = new \Magento\Framework\Code\Generator\Io(
51-
new \Magento\Framework\Filesystem\Driver\File(),
52-
$generationDirectory
53-
);
58+
$objectManager = Bootstrap::getObjectManager();
59+
/** @var Filesystem $filesystem */
60+
$filesystem = $objectManager->get(Filesystem::class);
61+
$this->generatedDirectory = $filesystem->getDirectoryWrite(DirectoryList::GENERATED_CODE);
62+
$this->logDirectory = $filesystem->getDirectoryRead(DirectoryList::LOG);
63+
$generatedDirectoryAbsolutePath = $this->generatedDirectory->getAbsolutePath();
64+
$this->_ioObject = new Generator\Io(new Filesystem\Driver\File(), $generatedDirectoryAbsolutePath);
5465
$this->_generator = $objectManager->create(
55-
\Magento\Framework\Code\Generator::class,
66+
Generator::class,
5667
[
5768
'ioObject' => $this->_ioObject,
5869
'generatedEntities' => [
5970
ExtensionAttributesInterfaceFactoryGenerator::ENTITY_TYPE =>
6071
ExtensionAttributesInterfaceFactoryGenerator::class,
61-
DIGenerator\Factory::ENTITY_TYPE => \Magento\Framework\ObjectManager\Code\Generator\Factory::class,
62-
DIGenerator\Proxy::ENTITY_TYPE => \Magento\Framework\ObjectManager\Code\Generator\Proxy::class,
63-
InterceptionGenerator\Interceptor::ENTITY_TYPE =>
64-
\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,
6575
]
6676
]
6777
);
6878
$this->_generator->setObjectManager($objectManager);
6979
}
7080

81+
/**
82+
* @inheritdoc
83+
*/
7184
protected function tearDown()
7285
{
73-
$this->varDirectory->delete('generation');
7486
$this->_generator = null;
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);
92+
}
7593
}
7694

7795
protected function _clearDocBlock($classBody)
7896
{
7997
return preg_replace('/(\/\*[\w\W]*)\nclass/', 'class', $classBody);
8098
}
8199

100+
/**
101+
* Generates a new file with Factory class and compares with the sample from the
102+
* SourceClassWithNamespaceFactory.php.sample file.
103+
*/
82104
public function testGenerateClassFactoryWithNamespace()
83105
{
84106
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'Factory';
85-
$result = false;
86-
$generatorResult = $this->_generator->generateClass($factoryClassName);
87-
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
88-
$result = true;
89-
}
90-
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
91-
92-
$factory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($factoryClassName);
93-
94-
$object = $factory->create();
95-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $object);
96-
97-
// This test is only valid if the factory created the object if Autoloader did not pick it up automatically
98-
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
99-
$content = $this->_clearDocBlock(
100-
file_get_contents(
101-
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . 'Factory')
102-
)
103-
);
104-
$expectedContent = $this->_clearDocBlock(
105-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceFactory.php.sample')
106-
);
107-
$this->assertEquals($expectedContent, $content);
108-
}
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);
109117
}
110118

119+
/**
120+
* Generates a new file with Proxy class and compares with the sample from the
121+
* SourceClassWithNamespaceProxy.php.sample file.
122+
*/
111123
public function testGenerateClassProxyWithNamespace()
112124
{
113125
$proxyClassName = self::CLASS_NAME_WITH_NAMESPACE . '\Proxy';
114-
$result = false;
115-
$generatorResult = $this->_generator->generateClass($proxyClassName);
116-
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
117-
$result = true;
118-
}
119-
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
120-
121-
$proxy = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($proxyClassName);
126+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($proxyClassName));
127+
$proxy = Bootstrap::getObjectManager()->create($proxyClassName);
122128
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $proxy);
123-
124-
// This test is only valid if the factory created the object if Autoloader did not pick it up automatically
125-
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
126-
$content = $this->_clearDocBlock(
127-
file_get_contents($this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . '\Proxy'))
128-
);
129-
$expectedContent = $this->_clearDocBlock(
130-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceProxy.php.sample')
131-
);
132-
$this->assertEquals($expectedContent, $content);
133-
}
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);
134136
}
135137

138+
/**
139+
* Generates a new file with Interceptor class and compares with the sample from the
140+
* SourceClassWithNamespaceInterceptor.php.sample file.
141+
*/
136142
public function testGenerateClassInterceptorWithNamespace()
137143
{
138144
$interceptorClassName = self::CLASS_NAME_WITH_NAMESPACE . '\Interceptor';
139-
$result = false;
140-
$generatorResult = $this->_generator->generateClass($interceptorClassName);
141-
if (\Magento\Framework\Code\Generator::GENERATION_ERROR !== $generatorResult) {
142-
$result = true;
143-
}
144-
$this->assertTrue($result, 'Failed asserting that \'' . (string)$generatorResult . '\' equals \'success\'.');
145-
146-
if (\Magento\Framework\Code\Generator::GENERATION_SUCCESS == $generatorResult) {
147-
$content = $this->_clearDocBlock(
148-
file_get_contents(
149-
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . '\Interceptor')
150-
)
151-
);
152-
$expectedContent = $this->_clearDocBlock(
153-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceInterceptor.php.sample')
154-
);
155-
$this->assertEquals($expectedContent, $content);
156-
}
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);
157153
}
158154

159155
/**
@@ -163,26 +159,35 @@ public function testGenerateClassInterceptorWithNamespace()
163159
public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace()
164160
{
165161
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory';
166-
$this->varDirectory->create(
167-
$this->varDirectory->getAbsolutePath('generation') . '/Magento/Framework/Code/GeneratorTest/'
168-
);
169-
170-
$generatorResult = $this->_generator->generateClass($factoryClassName);
171-
162+
$this->generatedDirectory->create($this->testRelativePath);
163+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
172164
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
173-
$object = $factory->create();
174-
175-
$this->assertEquals($generatorResult, Generator::GENERATION_SUCCESS);
176-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE . 'Extension', $object);
177-
165+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE . 'Extension', $factory->create());
178166
$content = $this->_clearDocBlock(
179-
file_get_contents(
180-
$this->_ioObject->generateResultFileName(self::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory')
181-
)
167+
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
182168
);
183169
$expectedContent = $this->_clearDocBlock(
184170
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample')
185171
);
186172
$this->assertEquals($expectedContent, $content);
187173
}
174+
175+
/**
176+
* It tries to generate a new class file when the generated directory is read-only
177+
*/
178+
public function testGeneratorClassWithErrorSaveClassFile()
179+
{
180+
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'Factory';
181+
$msgPart = 'Class ' . $factoryClassName . ' generation error: The requested class did not generate properly, '
182+
. 'because the \'generated\' directory permission is read-only.';
183+
$regexpMsgPart = preg_quote($msgPart);
184+
$this->expectException(\RuntimeException::class);
185+
$this->expectExceptionMessageRegExp("/.*$regexpMsgPart.*/");
186+
$this->generatedDirectory->create($this->testRelativePath);
187+
$this->generatedDirectory->changePermissionsRecursively($this->testRelativePath, 0555, 0444);
188+
$generatorResult = $this->_generator->generateClass($factoryClassName);
189+
$this->assertFalse($generatorResult);
190+
$pathToSystemLog = $this->logDirectory->getAbsolutePath('system.log');
191+
$this->assertContains($msgPart, file_get_contents($pathToSystemLog));
192+
}
188193
}

0 commit comments

Comments
 (0)