Skip to content

Commit

Permalink
#77 Set namespace iterates the /app.
Browse files Browse the repository at this point in the history
  • Loading branch information
amostajo committed Feb 15, 2020
1 parent d0d1317 commit 31ae94e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 56 deletions.
42 changes: 15 additions & 27 deletions src/Traits/SetNamespaceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace WPMVC\Commands\Traits;

use Exception;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Ayuco\Exceptions\NoticeException;

/**
Expand All @@ -12,7 +14,7 @@
* @copyright 10Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC\Commands
* @version 1.1.8
* @version 1.1.10
*/
trait SetNamespaceTrait
{
Expand All @@ -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();
Expand Down
27 changes: 11 additions & 16 deletions tests/cases/CreateAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
25 changes: 12 additions & 13 deletions tests/cases/SetDomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@
*/
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
*/
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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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');
}
}
34 changes: 34 additions & 0 deletions tests/cases/SetNamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, '<?php namespace MyApp\Controllers; use MyApp\Models\Model; class TestController { function artu() {return;} }');
file_put_contents($model, '<?php namespace MyApp\Models; use MyApp\Utility\Tool; class Model { function artu() {return;} }');
file_put_contents($utility, '<?php namespace MyApp\Utility; use MyApp\Controllers\TestController; class Tool { function artu() {return;} }');
// Execute
$execution = exec('php '.WPMVC_AYUCO.' set namespace:GlobalApp');
// Assert
$this->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);
}
}

0 comments on commit 31ae94e

Please sign in to comment.