@@ -27,6 +27,9 @@ class GeneratorTest extends TestCase
27
27
{
28
28
const CLASS_NAME_WITH_NAMESPACE = GeneratorTest \SourceClassWithNamespace::class;
29
29
const CLASS_NAME_WITH_NESTED_NAMESPACE = GeneratorTest \NestedNamespace \SourceClassWithNestedNamespace::class;
30
+ const EXTENSION_CLASS_NAME_WITH_NAMESPACE = GeneratorTest \SourceClassWithNamespaceExtension::class;
31
+ const EXTENSION_CLASS_NAME_WITH_NESTED_NAMESPACE =
32
+ GeneratorTest \NestedNamespace \SourceClassWithNestedNamespaceExtension::class;
30
33
31
34
/**
32
35
* @var Generator
@@ -62,6 +65,7 @@ protected function setUp(): void
62
65
/** @var Filesystem $filesystem */
63
66
$ filesystem = $ objectManager ->get (Filesystem::class);
64
67
$ this ->generatedDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::GENERATED_CODE );
68
+ $ this ->generatedDirectory ->create ($ this ->testRelativePath );
65
69
$ this ->logDirectory = $ filesystem ->getDirectoryRead (DirectoryList::LOG );
66
70
$ generatedDirectoryAbsolutePath = $ this ->generatedDirectory ->getAbsolutePath ();
67
71
$ this ->_ioObject = new Generator \Io (new Filesystem \Driver \File (), $ generatedDirectoryAbsolutePath );
@@ -101,117 +105,99 @@ protected function _clearDocBlock($classBody)
101
105
}
102
106
103
107
/**
104
- * Generates a new file with Factory class and compares with the sample from the
105
- * SourceClassWithNamespaceFactory.php.sample file.
108
+ * Generates a new class Factory file and compares with the sample.
109
+ *
110
+ * @param $className
111
+ * @param $generateType
112
+ * @param $expectedDataPath
113
+ * @dataProvider generateClassFactoryDataProvider
106
114
*/
107
- public function testGenerateClassFactoryWithNamespace ( )
115
+ public function testGenerateClassFactory ( $ className , $ generateType , $ expectedDataPath )
108
116
{
109
- $ factoryClassName = self :: CLASS_NAME_WITH_NAMESPACE . ' Factory ' ;
117
+ $ factoryClassName = $ className . $ generateType ;
110
118
$ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ factoryClassName ));
111
119
$ factory = Bootstrap::getObjectManager ()->create ($ factoryClassName );
112
- $ this ->assertInstanceOf (self :: CLASS_NAME_WITH_NAMESPACE , $ factory ->create ());
120
+ $ this ->assertInstanceOf ($ className , $ factory ->create ());
113
121
$ content = $ this ->_clearDocBlock (
114
122
file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
115
123
);
116
124
$ expectedContent = $ this ->_clearDocBlock (
117
- file_get_contents (__DIR__ . ' /_expected/SourceClassWithNamespaceFactory.php.sample ' )
125
+ file_get_contents (__DIR__ . $ expectedDataPath )
118
126
);
119
127
$ this ->assertEquals ($ expectedContent , $ content );
120
128
}
121
129
122
130
/**
123
- * Generates a new file with Factory class and compares with the sample from the
124
- * SourceClassWithNestedNamespaceFactory.php.sample file.
131
+ * DataProvider for testGenerateClassFactory
132
+ *
133
+ * @return array
125
134
*/
126
- public function testGenerateClassFactoryWithNestedNamespace ()
135
+ public function generateClassFactoryDataProvider ()
127
136
{
128
- $ factoryClassName = self ::CLASS_NAME_WITH_NESTED_NAMESPACE . 'Factory ' ;
129
- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ factoryClassName ));
130
- $ factory = Bootstrap::getObjectManager ()->create ($ factoryClassName );
131
- $ this ->assertInstanceOf (self ::CLASS_NAME_WITH_NESTED_NAMESPACE , $ factory ->create ());
132
- $ content = $ this ->_clearDocBlock (
133
- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
134
- );
135
- $ expectedContent = $ this ->_clearDocBlock (
136
- file_get_contents (__DIR__ . '/_expected/SourceClassWithNestedNamespaceFactory.php.sample ' )
137
- );
138
- $ this ->assertEquals ($ expectedContent , $ content );
137
+ return [
138
+ 'factory_with_namespace ' => [
139
+ 'className ' => self ::CLASS_NAME_WITH_NAMESPACE ,
140
+ 'generateType ' => 'Factory ' ,
141
+ 'expectedDataPath ' => '/_expected/SourceClassWithNamespaceFactory.php.sample '
142
+ ],
143
+ 'factory_with_nested_namespace ' => [
144
+ 'classToGenerate ' => self ::CLASS_NAME_WITH_NESTED_NAMESPACE ,
145
+ 'generateType ' => 'Factory ' ,
146
+ 'expectedDataPath ' => '/_expected/SourceClassWithNestedNamespaceFactory.php.sample '
147
+ ],
148
+ 'ext_interface_factory_with_namespace ' => [
149
+ 'classToGenerate ' => self ::EXTENSION_CLASS_NAME_WITH_NAMESPACE ,
150
+ 'generateType ' => 'InterfaceFactory ' ,
151
+ 'expectedDataPath ' => '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample '
152
+ ],
153
+ 'ext_interface_factory_with_nested_namespace ' => [
154
+ 'classToGenerate ' => self ::EXTENSION_CLASS_NAME_WITH_NESTED_NAMESPACE ,
155
+ 'generateType ' => 'InterfaceFactory ' ,
156
+ 'expectedDataPath ' => '/_expected/SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample '
157
+ ],
158
+ ];
139
159
}
140
160
141
161
/**
142
- * Generates a new file with ExtensionInterfaceFactory class and compares with the sample from the
143
- * SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample file.
162
+ * @param $className
163
+ * @param $generateType
164
+ * @param $expectedDataPath
165
+ * @dataProvider generateClassDataProvider
144
166
*/
145
- public function testGenerateClassExtensionAttributesInterfaceFactoryWithNestedNamespace ( )
167
+ public function testGenerateClass ( $ className , $ generateType , $ expectedDataPath )
146
168
{
147
- $ factoryClassName = self ::CLASS_NAME_WITH_NESTED_NAMESPACE . 'ExtensionInterfaceFactory ' ;
148
- $ this ->generatedDirectory ->create ($ this ->testRelativePath );
149
- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ factoryClassName ));
150
- $ factory = Bootstrap::getObjectManager ()->create ($ factoryClassName );
151
- $ this ->assertInstanceOf (self ::CLASS_NAME_WITH_NESTED_NAMESPACE . 'Extension ' , $ factory ->create ());
169
+ $ generateClassName = $ className . $ generateType ;
170
+ $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ generateClassName ));
171
+ $ instance = Bootstrap::getObjectManager ()->create ($ generateClassName );
172
+ $ this ->assertInstanceOf ($ className , $ instance );
152
173
$ content = $ this ->_clearDocBlock (
153
- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
174
+ file_get_contents ($ this ->_ioObject ->generateResultFileName ($ generateClassName ))
154
175
);
155
176
$ expectedContent = $ this ->_clearDocBlock (
156
- file_get_contents (__DIR__ . ' /_expected/SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample ' )
177
+ file_get_contents (__DIR__ . $ expectedDataPath )
157
178
);
158
179
$ this ->assertEquals ($ expectedContent , $ content );
159
180
}
160
181
161
182
/**
162
- * Generates a new file with Proxy class and compares with the sample from the
163
- * SourceClassWithNamespaceProxy.php.sample file.
183
+ * DataProvider for testGenerateClass
184
+ *
185
+ * @return array
164
186
*/
165
- public function testGenerateClassProxyWithNamespace ()
187
+ public function generateClassDataProvider ()
166
188
{
167
- $ proxyClassName = self ::CLASS_NAME_WITH_NAMESPACE . '\Proxy ' ;
168
- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ proxyClassName ));
169
- $ proxy = Bootstrap::getObjectManager ()->create ($ proxyClassName );
170
- $ this ->assertInstanceOf (self ::CLASS_NAME_WITH_NAMESPACE , $ proxy );
171
- $ content = $ this ->_clearDocBlock (
172
- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ proxyClassName ))
173
- );
174
- $ expectedContent = $ this ->_clearDocBlock (
175
- file_get_contents (__DIR__ . '/_expected/SourceClassWithNamespaceProxy.php.sample ' )
176
- );
177
- $ this ->assertEquals ($ expectedContent , $ content );
178
- }
179
-
180
- /**
181
- * Generates a new file with Interceptor class and compares with the sample from the
182
- * SourceClassWithNamespaceInterceptor.php.sample file.
183
- */
184
- public function testGenerateClassInterceptorWithNamespace ()
185
- {
186
- $ interceptorClassName = self ::CLASS_NAME_WITH_NAMESPACE . '\Interceptor ' ;
187
- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ interceptorClassName ));
188
- $ content = $ this ->_clearDocBlock (
189
- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ interceptorClassName ))
190
- );
191
- $ expectedContent = $ this ->_clearDocBlock (
192
- file_get_contents (__DIR__ . '/_expected/SourceClassWithNamespaceInterceptor.php.sample ' )
193
- );
194
- $ this ->assertEquals ($ expectedContent , $ content );
195
- }
196
-
197
- /**
198
- * Generates a new file with ExtensionInterfaceFactory class and compares with the sample from the
199
- * SourceClassWithNamespaceExtensionInterfaceFactory.php.sample file.
200
- */
201
- public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace ()
202
- {
203
- $ factoryClassName = self ::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory ' ;
204
- $ this ->generatedDirectory ->create ($ this ->testRelativePath );
205
- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ factoryClassName ));
206
- $ factory = Bootstrap::getObjectManager ()->create ($ factoryClassName );
207
- $ this ->assertInstanceOf (self ::CLASS_NAME_WITH_NAMESPACE . 'Extension ' , $ factory ->create ());
208
- $ content = $ this ->_clearDocBlock (
209
- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
210
- );
211
- $ expectedContent = $ this ->_clearDocBlock (
212
- file_get_contents (__DIR__ . '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample ' )
213
- );
214
- $ this ->assertEquals ($ expectedContent , $ content );
189
+ return [
190
+ 'proxy ' => [
191
+ 'className ' => self ::CLASS_NAME_WITH_NAMESPACE ,
192
+ 'generateType ' => '\Proxy ' ,
193
+ 'expectedDataPath ' => '/_expected/SourceClassWithNamespaceProxy.php.sample '
194
+ ],
195
+ 'interceptor ' => [
196
+ 'className ' => self ::CLASS_NAME_WITH_NAMESPACE ,
197
+ 'generateType ' => '\Interceptor ' ,
198
+ 'expectedDataPath ' => '/_expected/SourceClassWithNamespaceInterceptor.php.sample '
199
+ ]
200
+ ];
215
201
}
216
202
217
203
/**
@@ -225,7 +211,6 @@ public function testGeneratorClassWithErrorSaveClassFile()
225
211
$ regexpMsgPart = preg_quote ($ msgPart );
226
212
$ this ->expectException (\RuntimeException::class);
227
213
$ this ->expectExceptionMessageMatches ("/.* $ regexpMsgPart.*/ " );
228
- $ this ->generatedDirectory ->create ($ this ->testRelativePath );
229
214
$ this ->generatedDirectory ->changePermissionsRecursively ($ this ->testRelativePath , 0555 , 0444 );
230
215
$ generatorResult = $ this ->_generator ->generateClass ($ factoryClassName );
231
216
$ this ->assertFalse ($ generatorResult );
0 commit comments