@@ -27,6 +27,9 @@ class GeneratorTest extends TestCase
2727{
2828 const CLASS_NAME_WITH_NAMESPACE = GeneratorTest \SourceClassWithNamespace::class;
2929 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;
3033
3134 /**
3235 * @var Generator
@@ -62,6 +65,7 @@ protected function setUp(): void
6265 /** @var Filesystem $filesystem */
6366 $ filesystem = $ objectManager ->get (Filesystem::class);
6467 $ this ->generatedDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::GENERATED_CODE );
68+ $ this ->generatedDirectory ->create ($ this ->testRelativePath );
6569 $ this ->logDirectory = $ filesystem ->getDirectoryRead (DirectoryList::LOG );
6670 $ generatedDirectoryAbsolutePath = $ this ->generatedDirectory ->getAbsolutePath ();
6771 $ this ->_ioObject = new Generator \Io (new Filesystem \Driver \File (), $ generatedDirectoryAbsolutePath );
@@ -101,117 +105,99 @@ protected function _clearDocBlock($classBody)
101105 }
102106
103107 /**
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
106114 */
107- public function testGenerateClassFactoryWithNamespace ( )
115+ public function testGenerateClassFactory ( $ className , $ generateType , $ expectedDataPath )
108116 {
109- $ factoryClassName = self :: CLASS_NAME_WITH_NAMESPACE . ' Factory ' ;
117+ $ factoryClassName = $ className . $ generateType ;
110118 $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ factoryClassName ));
111119 $ factory = Bootstrap::getObjectManager ()->create ($ factoryClassName );
112- $ this ->assertInstanceOf (self :: CLASS_NAME_WITH_NAMESPACE , $ factory ->create ());
120+ $ this ->assertInstanceOf ($ className , $ factory ->create ());
113121 $ content = $ this ->_clearDocBlock (
114122 file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
115123 );
116124 $ expectedContent = $ this ->_clearDocBlock (
117- file_get_contents (__DIR__ . ' /_expected/SourceClassWithNamespaceFactory.php.sample ' )
125+ file_get_contents (__DIR__ . $ expectedDataPath )
118126 );
119127 $ this ->assertEquals ($ expectedContent , $ content );
120128 }
121129
122130 /**
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
125134 */
126- public function testGenerateClassFactoryWithNestedNamespace ()
135+ public function generateClassFactoryDataProvider ()
127136 {
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+ ];
139159 }
140160
141161 /**
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
144166 */
145- public function testGenerateClassExtensionAttributesInterfaceFactoryWithNestedNamespace ( )
167+ public function testGenerateClass ( $ className , $ generateType , $ expectedDataPath )
146168 {
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 );
152173 $ content = $ this ->_clearDocBlock (
153- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
174+ file_get_contents ($ this ->_ioObject ->generateResultFileName ($ generateClassName ))
154175 );
155176 $ expectedContent = $ this ->_clearDocBlock (
156- file_get_contents (__DIR__ . ' /_expected/SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample ' )
177+ file_get_contents (__DIR__ . $ expectedDataPath )
157178 );
158179 $ this ->assertEquals ($ expectedContent , $ content );
159180 }
160181
161182 /**
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
164186 */
165- public function testGenerateClassProxyWithNamespace ()
187+ public function generateClassDataProvider ()
166188 {
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+ ];
215201 }
216202
217203 /**
@@ -225,7 +211,6 @@ public function testGeneratorClassWithErrorSaveClassFile()
225211 $ regexpMsgPart = preg_quote ($ msgPart );
226212 $ this ->expectException (\RuntimeException::class);
227213 $ this ->expectExceptionMessageMatches ("/.* $ regexpMsgPart.*/ " );
228- $ this ->generatedDirectory ->create ($ this ->testRelativePath );
229214 $ this ->generatedDirectory ->changePermissionsRecursively ($ this ->testRelativePath , 0555 , 0444 );
230215 $ generatorResult = $ this ->_generator ->generateClass ($ factoryClassName );
231216 $ this ->assertFalse ($ generatorResult );
0 commit comments