Skip to content

Commit

Permalink
auto-generate tests for components jadjoubran/laravel5-angular-materi…
Browse files Browse the repository at this point in the history
  • Loading branch information
turtle220 committed Mar 26, 2016
1 parent 597fe82 commit d172529
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
11 changes: 11 additions & 0 deletions config/generators.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]
];
13 changes: 12 additions & 1 deletion src/Console/Commands/AngularComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -40,21 +40,27 @@ 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');

return false;
}

$spec_folder = base_path(config('generators.tests.source.root')).'/'.config('generators.tests.source.components');

//create folder
File::makeDirectory($folder, 0775, true);

Expand All @@ -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.');
}
}
13 changes: 11 additions & 2 deletions src/Console/Commands/AngularService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.');
}
}
11 changes: 11 additions & 0 deletions src/Console/Commands/Stubs/AngularComponent/component.spec.js.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ngDescribe({
name: 'Test {{ng-component}} component',
modules: 'app',
element: '<{{ng-component}}></{{ng-component}}>',
tests: function (deps) {

it('basic test', () => {
//
});
}
});
11 changes: 11 additions & 0 deletions src/Console/Commands/Stubs/AngularService/service.spec.js.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ngDescribe({
name: 'Test {{StudlyName}}Service',
modules: 'app',
inject: '{{StudlyName}}Service',
tests: function (deps) {

it('basic test', () => {
//
});
}
});

0 comments on commit d172529

Please sign in to comment.