5
5
*/
6
6
namespace Magento \Framework \Code ;
7
7
8
- use Magento \Framework \Code \Generator ;
8
+ use Magento \Framework \Api \ Code \Generator \ ExtensionAttributesInterfaceFactoryGenerator ;
9
9
use Magento \Framework \App \Filesystem \DirectoryList ;
10
+ use Magento \Framework \Filesystem ;
10
11
use Magento \Framework \Interception \Code \Generator as InterceptionGenerator ;
11
12
use Magento \Framework \ObjectManager \Code \Generator as DIGenerator ;
12
- use Magento \Framework \Api \Code \Generator \ExtensionAttributesInterfaceFactoryGenerator ;
13
13
use Magento \TestFramework \Helper \Bootstrap ;
14
+ use PHPUnit \Framework \TestCase ;
14
15
15
16
require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespace.php ' ;
16
17
require_once __DIR__ . '/GeneratorTest/ParentClassWithNamespace.php ' ;
17
18
require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespaceExtension.php ' ;
18
19
19
20
/**
20
21
* @magentoAppIsolation enabled
22
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
23
*/
22
- class GeneratorTest extends \ PHPUnit \ Framework \ TestCase
24
+ class GeneratorTest extends TestCase
23
25
{
24
- const CLASS_NAME_WITH_NAMESPACE = \ Magento \ Framework \ Code \ GeneratorTest \SourceClassWithNamespace::class;
26
+ const CLASS_NAME_WITH_NAMESPACE = GeneratorTest \SourceClassWithNamespace::class;
25
27
26
28
/**
27
- * @var \Magento\Framework\Code\ Generator
29
+ * @var Generator
28
30
*/
29
31
protected $ _generator ;
30
32
31
33
/**
32
- * @var \Magento\Framework\Code\ Generator\ Io
34
+ * @var Generator/ Io
33
35
*/
34
36
protected $ _ioObject ;
35
37
36
38
/**
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
38
50
*/
39
- protected $ varDirectory ;
51
+ private $ testRelativePath = ' ./Magento/Framework/Code/GeneratorTest/ ' ;
40
52
53
+ /**
54
+ * @inheritdoc
55
+ */
41
56
protected function setUp ()
42
57
{
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 );
54
65
$ this ->_generator = $ objectManager ->create (
55
- \ Magento \ Framework \ Code \ Generator::class,
66
+ Generator::class,
56
67
[
57
68
'ioObject ' => $ this ->_ioObject ,
58
69
'generatedEntities ' => [
59
70
ExtensionAttributesInterfaceFactoryGenerator::ENTITY_TYPE =>
60
71
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,
65
75
]
66
76
]
67
77
);
68
78
$ this ->_generator ->setObjectManager ($ objectManager );
69
79
}
70
80
81
+ /**
82
+ * @inheritdoc
83
+ */
71
84
protected function tearDown ()
72
85
{
73
- $ this ->varDirectory ->delete ('generation ' );
74
86
$ 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
+ }
75
93
}
76
94
77
95
protected function _clearDocBlock ($ classBody )
78
96
{
79
97
return preg_replace ('/(\/\*[\w\W]*)\nclass/ ' , 'class ' , $ classBody );
80
98
}
81
99
100
+ /**
101
+ * Generates a new file with Factory class and compares with the sample from the
102
+ * SourceClassWithNamespaceFactory.php.sample file.
103
+ */
82
104
public function testGenerateClassFactoryWithNamespace ()
83
105
{
84
106
$ 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 );
109
117
}
110
118
119
+ /**
120
+ * Generates a new file with Proxy class and compares with the sample from the
121
+ * SourceClassWithNamespaceProxy.php.sample file.
122
+ */
111
123
public function testGenerateClassProxyWithNamespace ()
112
124
{
113
125
$ 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 );
122
128
$ 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 );
134
136
}
135
137
138
+ /**
139
+ * Generates a new file with Interceptor class and compares with the sample from the
140
+ * SourceClassWithNamespaceInterceptor.php.sample file.
141
+ */
136
142
public function testGenerateClassInterceptorWithNamespace ()
137
143
{
138
144
$ 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 );
157
153
}
158
154
159
155
/**
@@ -163,26 +159,35 @@ public function testGenerateClassInterceptorWithNamespace()
163
159
public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace ()
164
160
{
165
161
$ 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 ));
172
164
$ 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 ());
178
166
$ 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 ))
182
168
);
183
169
$ expectedContent = $ this ->_clearDocBlock (
184
170
file_get_contents (__DIR__ . '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample ' )
185
171
);
186
172
$ this ->assertEquals ($ expectedContent , $ content );
187
173
}
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
+ }
188
193
}
0 commit comments