From d172529f24969d43f8d252fe07cec7385f05eef9 Mon Sep 17 00:00:00 2001 From: Turtle220 Date: Sun, 27 Mar 2016 01:03:39 +0300 Subject: [PATCH] auto-generate tests for components jadjoubran/laravel5-angular-material-starter#221 --- config/generators.php | 11 +++++++++++ src/Console/Commands/AngularComponent.php | 13 ++++++++++++- src/Console/Commands/AngularService.php | 13 +++++++++++-- .../Stubs/AngularComponent/component.spec.js.stub | 11 +++++++++++ .../Stubs/AngularService/service.spec.js.stub | 11 +++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/Console/Commands/Stubs/AngularComponent/component.spec.js.stub create mode 100644 src/Console/Commands/Stubs/AngularService/service.spec.js.stub diff --git a/config/generators.php b/config/generators.php index 93df19f..d580261 100644 --- a/config/generators.php +++ b/config/generators.php @@ -19,4 +19,15 @@ 'filter' => '.filter.js', 'pageView' => '.page.html', ], + 'tests' => [ + 'enable' => [ + 'components' => true, + 'services' => true, + ], + 'source' => [ + 'root' => 'tests/angular/' + 'components' => 'app/components', + 'services' => 'services', + ], + ] ]; diff --git a/src/Console/Commands/AngularComponent.php b/src/Console/Commands/AngularComponent.php index 68a8ef7..eb439ef 100644 --- a/src/Console/Commands/AngularComponent.php +++ b/src/Console/Commands/AngularComponent.php @@ -12,7 +12,7 @@ class AngularComponent extends Command * * @var string */ - protected $signature = 'ng:component {name}'; + protected $signature = 'ng:component {name} {--no-spec}'; /** * The console command description. @@ -40,14 +40,18 @@ public function handle() { $name = $this->argument('name'); $studly_name = studly_case($name); + $ng_component = str_replace('-', '-', $name); $html = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.html.stub'); $js = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.js.stub'); $less = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.less.stub'); + $spec = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.spec.js.stub'); $js = str_replace('{{StudlyName}}', $studly_name, $js); $js = str_replace('{{name}}', $name, $js); + $spec = str_replace('{{ng-component}}', $ng_component, $spec); + $folder = base_path(config('generators.source.root')).'/'.config('generators.source.components').'/'.$name; if (is_dir($folder)) { $this->info('Folder already exists'); @@ -55,6 +59,8 @@ public function handle() return false; } + $spec_folder = base_path(config('generators.tests.source.root')).'/'.config('generators.tests.source.components'); + //create folder File::makeDirectory($folder, 0775, true); @@ -67,6 +73,11 @@ public function handle() //create less file (.less) File::put($folder.'/'.$name.'.less', $less); + if ( !$this->argument('no-spec') && config('generators.tests.enable.components') ){ + //create spec file (.component.spec.js) + File::put($spec_folder.'/'.$name.'.component.spec.js', $spec); + } + $this->info('Component created successfully.'); } } diff --git a/src/Console/Commands/AngularService.php b/src/Console/Commands/AngularService.php index 317b52a..da970bf 100644 --- a/src/Console/Commands/AngularService.php +++ b/src/Console/Commands/AngularService.php @@ -12,7 +12,7 @@ class AngularService extends Command * * @var string */ - protected $signature = 'ng:service {name}'; + protected $signature = 'ng:service {name} {--no-spec}'; /** * The console command description. @@ -42,14 +42,23 @@ public function handle() $studly_name = studly_case($name); $js = file_get_contents(__DIR__.'/Stubs/AngularService/service.js.stub'); + $spec = file_get_contents(__DIR__.'/Stubs/AngularService/service.spec.js.stub'); $js = str_replace('{{StudlyName}}', $studly_name, $js); + $spec = str_replace('{{StudlyName}}', $studly_name, $spec); $folder = base_path(config('generators.source.root')).'/'.config('generators.source.services'); - //create service (.js) + $spec_folder = base_path(config('generators.tests.source.root')).'/'.config('generators.tests.source.services'); + + //create service (.service.js) File::put($folder.'/'.$name.config('generators.prefix.service'), $js); + if ( !$this->argument('no-spec') && config('generators.tests.enable.services') ){ + //create spec (.service.spec.js) + File::put($spec_folder.'/'.$name.'.service.spec.js', $spec); + } + $this->info('Service created successfully.'); } } diff --git a/src/Console/Commands/Stubs/AngularComponent/component.spec.js.stub b/src/Console/Commands/Stubs/AngularComponent/component.spec.js.stub new file mode 100644 index 0000000..9caf333 --- /dev/null +++ b/src/Console/Commands/Stubs/AngularComponent/component.spec.js.stub @@ -0,0 +1,11 @@ +ngDescribe({ + name: 'Test {{ng-component}} component', + modules: 'app', + element: '<{{ng-component}}>', + tests: function (deps) { + + it('basic test', () => { + // + }); + } +}); diff --git a/src/Console/Commands/Stubs/AngularService/service.spec.js.stub b/src/Console/Commands/Stubs/AngularService/service.spec.js.stub new file mode 100644 index 0000000..990568e --- /dev/null +++ b/src/Console/Commands/Stubs/AngularService/service.spec.js.stub @@ -0,0 +1,11 @@ +ngDescribe({ + name: 'Test {{StudlyName}}Service', + modules: 'app', + inject: '{{StudlyName}}Service', + tests: function (deps) { + + it('basic test', () => { + // + }); + } +});