From 31ae94e9d8665445f333d6842783c5191a164375 Mon Sep 17 00:00:00 2001 From: Alejandro Mostajo Date: Sat, 15 Feb 2020 01:01:15 -0600 Subject: [PATCH] #77 Set namespace iterates the /app. --- src/Traits/SetNamespaceTrait.php | 42 ++++++++++++-------------------- tests/cases/CreateAssetTest.php | 27 +++++++++----------- tests/cases/SetDomainTest.php | 25 +++++++++---------- tests/cases/SetNamespaceTest.php | 34 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/Traits/SetNamespaceTrait.php b/src/Traits/SetNamespaceTrait.php index 3ba7242..80fc5fb 100644 --- a/src/Traits/SetNamespaceTrait.php +++ b/src/Traits/SetNamespaceTrait.php @@ -3,6 +3,8 @@ namespace WPMVC\Commands\Traits; use Exception; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use Ayuco\Exceptions\NoticeException; /** @@ -12,7 +14,7 @@ * @copyright 10Quality * @license MIT * @package WPMVC\Commands - * @version 1.1.8 + * @version 1.1.10 */ trait SetNamespaceTrait { @@ -35,36 +37,22 @@ public function setNamespace($namespace) $this->config = include $this->configFilename; // Update Namespace in composer.json $this->replaceInFile($currentnamespace, $namespace, $this->rootPath.'/composer.json'); - // Update Namespace in Main app file - if (file_exists($this->rootPath . '/app/Main.php')) + + $dir = new RecursiveDirectoryIterator($this->rootPath . '/app', RecursiveDirectoryIterator::SKIP_DOTS); + foreach (new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST) as $filename => $item) { + if ($item->isDir()) + continue; $this->replaceInFile( 'namespace ' . $currentnamespace, 'namespace ' . $namespace, - $this->rootPath.'/app/Main.php' + $filename + ); + $this->replaceInFile( + 'use ' . $currentnamespace, + 'use ' . $namespace, + $filename ); - // Update Namespace in Model files - if (is_dir($this->rootPath.'/app/Models')) - foreach (scandir($this->rootPath.'/app/Models') as $filename) { - $this->replaceInFile( - 'namespace ' . $currentnamespace, - 'namespace ' . $namespace, - $this->rootPath.'/app/Models/' . $filename - ); - } - // Update Namespace in Controller files - if (is_dir($this->config['paths']['controllers'])) - foreach (scandir($this->config['paths']['controllers']) as $filename) { - $this->replaceInFile( - 'namespace ' . $currentnamespace, - 'namespace ' . $namespace, - $this->config['paths']['controllers'] . $filename - ); - $this->replaceInFile( - 'use ' . $currentnamespace, - 'use ' . $namespace, - $this->config['paths']['controllers'] . $filename - ); - } + } // Print end $this->_print('Namespace updated!'); $this->_lineBreak(); diff --git a/tests/cases/CreateAssetTest.php b/tests/cases/CreateAssetTest.php index cd48e63..dd9115e 100644 --- a/tests/cases/CreateAssetTest.php +++ b/tests/cases/CreateAssetTest.php @@ -21,6 +21,17 @@ class CreateAssetTest extends WpmvcAyucoTestCase FRAMEWORK_PATH.'/environment/assets/raw', FRAMEWORK_PATH.'/environment/assets', ]; + /** + * Retore to default namespace. + * @since 1.1.8 + */ + public function tearDown(): void + { + parent::tearDown(); + $filename = FRAMEWORK_PATH.'/environment/.gitignore'; + if (file_exists($filename)) + unlink($filename); + } /** * Tests javascript asset creation. * @group assets @@ -211,20 +222,4 @@ public function testScssMasterAndParts() $this->assertPregMatchContents('/\@import(|\s)\\\'parts\/header\\\'\;/', $masterfile); $this->assertPregMatchContents('/\@import(|\s)\\\'parts\/footer\\\'\;/', $masterfile); } - /** - * Tests sass gitignore update. - * @group assets - * @group scss - */ - public function testScssGitignore() - { - // Prepare - $filename = FRAMEWORK_PATH.'/environment/.gitignore'; - // Execute - exec('php '.WPMVC_AYUCO.' create scss:theme'); - // Assert - $this->assertFileExists($filename); - $this->assertPregMatchContents('/\# SASS COMPILATION/', $filename); - unlink($filename); - } } diff --git a/tests/cases/SetDomainTest.php b/tests/cases/SetDomainTest.php index ee3dbc7..70c9c67 100644 --- a/tests/cases/SetDomainTest.php +++ b/tests/cases/SetDomainTest.php @@ -10,6 +10,15 @@ */ class SetDomainTest extends WpmvcAyucoTestCase { + /** + * Retore to default namespace. + * @since 1.1.10 + */ + public function tearDown(): void + { + parent::tearDown(); + exec('php '.WPMVC_AYUCO.' set domain:my-app'); + } /** * Test resulting message. * @group domain @@ -17,10 +26,8 @@ class SetDomainTest extends WpmvcAyucoTestCase public function testResultMessage() { $execution = exec('php '.WPMVC_AYUCO.' set domain:phpunit'); - + // Assert $this->assertEquals($execution, 'Text domain updated!'); - // Down test - exec('php '.WPMVC_AYUCO.' set domain:my-app'); } /** * Test package.json. @@ -31,10 +38,8 @@ public function testPackageDomainValue() // Run $execution = exec('php '.WPMVC_AYUCO.' set domain:domain-value'); $json = json_decode(file_get_contents(FRAMEWORK_PATH.'/environment/package.json')); - // Asset + // Assert $this->assertEquals('domain-value', $json->name); - // Down test - exec('php '.WPMVC_AYUCO.' set domain:my-app'); } /** * Test composer.json. @@ -45,10 +50,8 @@ public function testComposerDomainValue() // Run $execution = exec('php '.WPMVC_AYUCO.' set domain:domain-value'); $json = json_decode(file_get_contents(FRAMEWORK_PATH.'/environment/composer.json')); - // Asset + // Assert $this->assertEquals('wpmvc/domain-value', $json->name); - // Down test - exec('php '.WPMVC_AYUCO.' set domain:my-app'); } /** * Test style.css. @@ -64,8 +67,6 @@ public function testThemeDomainValue() $matches ); $this->assertEquals(1, preg_match('/special\-domain/', $matches[0])); - // Down test - exec('php '.WPMVC_AYUCO.' set domain:my-app'); } /** * Test missing domain. @@ -93,7 +94,5 @@ public function testNamespacePreservation() // Assert $this->assertStringMatchContents('textdomain\' => \'namespace\'', $filename); $this->assertStringMatchContents('namespace\' => \'Preserve\'', $filename); - // Down test - exec('php '.WPMVC_AYUCO.' set domain:my-app'); } } diff --git a/tests/cases/SetNamespaceTest.php b/tests/cases/SetNamespaceTest.php index c4e7bb0..e14ac91 100644 --- a/tests/cases/SetNamespaceTest.php +++ b/tests/cases/SetNamespaceTest.php @@ -10,6 +10,14 @@ */ class SetNamespaceTest extends WpmvcAyucoTestCase { + /** + * Tests path. + */ + protected $path = [ + TESTING_PATH.'/app/Controllers/', + TESTING_PATH.'/app/Models/', + TESTING_PATH.'/app/Utility/', + ]; /** * Retore to default namespace. * @since 1.1.8 @@ -67,4 +75,30 @@ public function testMissingNamespace() // Run $this->assertEquals('Command "set": Expecting a namespace.', $execution); } + /** + * Test composer file updated. + * @group namespace + */ + public function testGlobalAppChange() + { + // Prepare + $controller = TESTING_PATH.'/app/Controllers/TestController.php'; + $model = TESTING_PATH.'/app/Models/Model.php'; + $utility = TESTING_PATH.'/app/Utility/Tool.php'; + mkdir(TESTING_PATH.'/app/Controllers/', 0777, true); + mkdir(TESTING_PATH.'/app/Models/', 0777, true); + mkdir(TESTING_PATH.'/app/Utility/', 0777, true); + file_put_contents($controller, 'assertStringMatchContents('namespace GlobalApp\Controllers', $controller); + $this->assertStringMatchContents('use GlobalApp\Models\Model', $controller); + $this->assertStringMatchContents('namespace GlobalApp\Models', $model); + $this->assertStringMatchContents('use GlobalApp\Utility\Tool', $model); + $this->assertStringMatchContents('namespace GlobalApp\Utility', $utility); + $this->assertStringMatchContents('use GlobalApp\Controllers\TestController', $utility); + } } \ No newline at end of file