5
5
*/
6
6
namespace Magento \Framework \Code ;
7
7
8
+ use Magento \Framework \Api \Code \Generator \ExtensionAttributesInterfaceFactoryGenerator ;
8
9
use Magento \Framework \App \Filesystem \DirectoryList ;
9
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 ' ;
20
21
* @magentoAppIsolation enabled
21
22
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
23
*/
23
- class GeneratorTest extends \ PHPUnit \ Framework \ TestCase
24
+ class GeneratorTest extends TestCase
24
25
{
25
- const CLASS_NAME_WITH_NAMESPACE = \ Magento \ Framework \ Code \ GeneratorTest \SourceClassWithNamespace::class;
26
+ const CLASS_NAME_WITH_NAMESPACE = GeneratorTest \SourceClassWithNamespace::class;
26
27
27
28
/**
28
- * @var \Magento\Framework\Code\ Generator
29
+ * @var Generator
29
30
*/
30
31
protected $ _generator ;
31
32
32
33
/**
33
- * @var \Magento\Framework\Code\ Generator\ Io
34
+ * @var Generator/ Io
34
35
*/
35
36
protected $ _ioObject ;
36
37
37
38
/**
38
- * @var \Magento\Framework\ Filesystem\Directory\Write
39
+ * @var Filesystem\Directory\Write
39
40
*/
40
- protected $ generatedDirectory ;
41
+ private $ generatedDirectory ;
41
42
42
43
/**
43
- * @var \Magento\Framework\ Filesystem\Directory\Read
44
+ * @var Filesystem\Directory\Read
44
45
*/
45
46
private $ logDirectory ;
46
47
47
48
/**
48
- * @var int
49
+ * @var string
49
50
*/
50
- private $ oldPermissionGenerationDir ;
51
+ private $ testRelativePath = ' ./Magento/Framework/Code/GeneratorTest/ ' ;
51
52
52
53
/**
53
54
* @inheritdoc
54
55
*/
55
56
protected function setUp ()
56
57
{
57
- $ objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
58
+ $ objectManager = Bootstrap::getObjectManager ();
58
59
/** @var Filesystem $filesystem */
59
- $ filesystem = $ objectManager ->get (\ Magento \ Framework \ Filesystem::class);
60
+ $ filesystem = $ objectManager ->get (Filesystem::class);
60
61
$ this ->generatedDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::GENERATED_CODE );
61
62
$ 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 );
67
65
$ this ->_generator = $ objectManager ->create (
68
- \ Magento \ Framework \ Code \ Generator::class,
66
+ Generator::class,
69
67
[
70
68
'ioObject ' => $ this ->_ioObject ,
71
69
'generatedEntities ' => [
72
70
ExtensionAttributesInterfaceFactoryGenerator::ENTITY_TYPE =>
73
71
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,
78
75
]
79
76
]
80
77
);
81
78
$ this ->_generator ->setObjectManager ($ objectManager );
82
-
83
- if (file_exists ($ generatedDirectory )) {
84
- $ this ->oldPermissionGenerationDir = fileperms ($ generatedDirectory );
85
- }
86
79
}
87
80
81
+ /**
82
+ * @inheritdoc
83
+ */
88
84
protected function tearDown ()
89
85
{
90
- $ this ->generatedDirectory ->changePermissionsRecursively ('./ ' , 0777 , 0666 );
91
- $ this ->generatedDirectory ->delete ();
92
86
$ 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 );
100
92
}
101
93
}
102
94
@@ -105,81 +97,59 @@ protected function _clearDocBlock($classBody)
105
97
return preg_replace ('/(\/\*[\w\W]*)\nclass/ ' , 'class ' , $ classBody );
106
98
}
107
99
100
+ /**
101
+ * Generates a new file with Factory class and compares with the sample from the
102
+ * SourceClassWithNamespaceFactory.php.sample file.
103
+ */
108
104
public function testGenerateClassFactoryWithNamespace ()
109
105
{
110
106
$ 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 );
135
117
}
136
118
119
+ /**
120
+ * Generates a new file with Proxy class and compares with the sample from the
121
+ * SourceClassWithNamespaceProxy.php.sample file.
122
+ */
137
123
public function testGenerateClassProxyWithNamespace ()
138
124
{
139
125
$ 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 );
148
128
$ 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 );
160
136
}
161
137
138
+ /**
139
+ * Generates a new file with Interceptor class and compares with the sample from the
140
+ * SourceClassWithNamespaceInterceptor.php.sample file.
141
+ */
162
142
public function testGenerateClassInterceptorWithNamespace ()
163
143
{
164
144
$ 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 );
183
153
}
184
154
185
155
/**
@@ -189,45 +159,35 @@ public function testGenerateClassInterceptorWithNamespace()
189
159
public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace ()
190
160
{
191
161
$ 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 ));
198
164
$ 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 ());
204
166
$ 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 ))
208
168
);
209
169
$ expectedContent = $ this ->_clearDocBlock (
210
170
file_get_contents (__DIR__ . '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample ' )
211
171
);
212
172
$ this ->assertEquals ($ expectedContent , $ content );
213
173
}
214
174
175
+ /**
176
+ * It tries to generate a new class file if the generated directory is read-only
177
+ */
215
178
public function testGeneratorClassWithErrorSaveClassFile ()
216
179
{
217
180
$ msgPart = 'Error: an object of a generated class may be a dependency for another object, '
218
181
. 'but this dependency has not been defined or set correctly in the signature of the related construct '
219
182
. 'method ' ;
220
-
221
183
$ 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 ' ;
226
188
$ generatorResult = $ this ->_generator ->generateClass ($ factoryClassName );
227
189
$ this ->assertFalse ($ generatorResult );
228
-
229
190
$ 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 ));
232
192
}
233
193
}
0 commit comments