1616require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespace.php ' ;
1717require_once __DIR__ . '/GeneratorTest/ParentClassWithNamespace.php ' ;
1818require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespaceExtension.php ' ;
19+ require_once __DIR__ . '/GeneratorTest/NestedNamespace/SourceClassWithNestedNamespace.php ' ;
20+ require_once __DIR__ . '/GeneratorTest/NestedNamespace/SourceClassWithNestedNamespaceExtension.php ' ;
1921
2022/**
2123 * @magentoAppIsolation enabled
2426class GeneratorTest extends TestCase
2527{
2628 const CLASS_NAME_WITH_NAMESPACE = GeneratorTest \SourceClassWithNamespace::class;
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;
2733
2834 /**
2935 * @var Generator
@@ -59,6 +65,7 @@ protected function setUp(): void
5965 /** @var Filesystem $filesystem */
6066 $ filesystem = $ objectManager ->get (Filesystem::class);
6167 $ this ->generatedDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::GENERATED_CODE );
68+ $ this ->generatedDirectory ->create ($ this ->testRelativePath );
6269 $ this ->logDirectory = $ filesystem ->getDirectoryRead (DirectoryList::LOG );
6370 $ generatedDirectoryAbsolutePath = $ this ->generatedDirectory ->getAbsolutePath ();
6471 $ this ->_ioObject = new Generator \Io (new Filesystem \Driver \File (), $ generatedDirectoryAbsolutePath );
@@ -98,78 +105,99 @@ protected function _clearDocBlock($classBody)
98105 }
99106
100107 /**
101- * Generates a new file with Factory class and compares with the sample from the
102- * 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
103114 */
104- public function testGenerateClassFactoryWithNamespace ( )
115+ public function testGenerateClassFactory ( $ className , $ generateType , $ expectedDataPath )
105116 {
106- $ factoryClassName = self :: CLASS_NAME_WITH_NAMESPACE . ' Factory ' ;
117+ $ factoryClassName = $ className . $ generateType ;
107118 $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ factoryClassName ));
108119 $ factory = Bootstrap::getObjectManager ()->create ($ factoryClassName );
109- $ this ->assertInstanceOf (self :: CLASS_NAME_WITH_NAMESPACE , $ factory ->create ());
120+ $ this ->assertInstanceOf ($ className , $ factory ->create ());
110121 $ content = $ this ->_clearDocBlock (
111122 file_get_contents ($ this ->_ioObject ->generateResultFileName ($ factoryClassName ))
112123 );
113124 $ expectedContent = $ this ->_clearDocBlock (
114- file_get_contents (__DIR__ . ' /_expected/SourceClassWithNamespaceFactory.php.sample ' )
125+ file_get_contents (__DIR__ . $ expectedDataPath )
115126 );
116127 $ this ->assertEquals ($ expectedContent , $ content );
117128 }
118129
119130 /**
120- * Generates a new file with Proxy class and compares with the sample from the
121- * SourceClassWithNamespaceProxy.php.sample file.
131+ * DataProvider for testGenerateClassFactory
132+ *
133+ * @return array
122134 */
123- public function testGenerateClassProxyWithNamespace ()
135+ public function generateClassFactoryDataProvider ()
124136 {
125- $ proxyClassName = self ::CLASS_NAME_WITH_NAMESPACE . '\Proxy ' ;
126- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ proxyClassName ));
127- $ proxy = Bootstrap::getObjectManager ()->create ($ proxyClassName );
128- $ this ->assertInstanceOf (self ::CLASS_NAME_WITH_NAMESPACE , $ proxy );
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 );
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+ ];
136159 }
137160
138161 /**
139- * Generates a new file with Interceptor class and compares with the sample from the
140- * SourceClassWithNamespaceInterceptor.php.sample file.
162+ * @param $className
163+ * @param $generateType
164+ * @param $expectedDataPath
165+ * @dataProvider generateClassDataProvider
141166 */
142- public function testGenerateClassInterceptorWithNamespace ( )
167+ public function testGenerateClass ( $ className , $ generateType , $ expectedDataPath )
143168 {
144- $ interceptorClassName = self ::CLASS_NAME_WITH_NAMESPACE . '\Interceptor ' ;
145- $ this ->assertEquals (Generator::GENERATION_SUCCESS , $ this ->_generator ->generateClass ($ interceptorClassName ));
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 );
146173 $ content = $ this ->_clearDocBlock (
147- file_get_contents ($ this ->_ioObject ->generateResultFileName ($ interceptorClassName ))
174+ file_get_contents ($ this ->_ioObject ->generateResultFileName ($ generateClassName ))
148175 );
149176 $ expectedContent = $ this ->_clearDocBlock (
150- file_get_contents (__DIR__ . ' /_expected/SourceClassWithNamespaceInterceptor.php.sample ' )
177+ file_get_contents (__DIR__ . $ expectedDataPath )
151178 );
152179 $ this ->assertEquals ($ expectedContent , $ content );
153180 }
154181
155182 /**
156- * Generates a new file with ExtensionInterfaceFactory class and compares with the sample from the
157- * SourceClassWithNamespaceExtensionInterfaceFactory.php.sample file.
183+ * DataProvider for testGenerateClass
184+ *
185+ * @return array
158186 */
159- public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace ()
187+ public function generateClassDataProvider ()
160188 {
161- $ factoryClassName = self :: CLASS_NAME_WITH_NAMESPACE . ' ExtensionInterfaceFactory ' ;
162- $ this -> generatedDirectory -> create ( $ this -> testRelativePath );
163- $ this -> assertEquals (Generator:: GENERATION_SUCCESS , $ this -> _generator -> generateClass ( $ factoryClassName ));
164- $ factory = Bootstrap:: getObjectManager ()-> create ( $ factoryClassName );
165- $ this -> assertInstanceOf ( self :: CLASS_NAME_WITH_NAMESPACE . ' Extension ' , $ factory -> create ());
166- $ content = $ this -> _clearDocBlock (
167- file_get_contents ( $ this -> _ioObject -> generateResultFileName ( $ factoryClassName ))
168- );
169- $ expectedContent = $ this -> _clearDocBlock (
170- file_get_contents ( __DIR__ . ' /_expected/SourceClassWithNamespaceExtensionInterfaceFactory .php.sample ')
171- );
172- $ 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+ ] ;
173201 }
174202
175203 /**
@@ -183,7 +211,6 @@ public function testGeneratorClassWithErrorSaveClassFile()
183211 $ regexpMsgPart = preg_quote ($ msgPart );
184212 $ this ->expectException (\RuntimeException::class);
185213 $ this ->expectExceptionMessageMatches ("/.* $ regexpMsgPart.*/ " );
186- $ this ->generatedDirectory ->create ($ this ->testRelativePath );
187214 $ this ->generatedDirectory ->changePermissionsRecursively ($ this ->testRelativePath , 0555 , 0444 );
188215 $ generatorResult = $ this ->_generator ->generateClass ($ factoryClassName );
189216 $ this ->assertFalse ($ generatorResult );
0 commit comments