Skip to content
Permalink
Browse files

Merge pull request #748 from artemsky/support/test

Refactor TestMakeCommand / add `--feature` flag
  • Loading branch information...
nWidart committed Apr 11, 2019
2 parents 4cd29bf + cbd6fdb commit 04afa51e01e3b7e4d7c5c0c2b6d90ddccb3a32fd
@@ -110,7 +110,8 @@
'assets' => ['path' => 'Resources/assets', 'generate' => true],
'lang' => ['path' => 'Resources/lang', 'generate' => true],
'views' => ['path' => 'Resources/views', 'generate' => true],
'test' => ['path' => 'Tests', 'generate' => true],
'test' => ['path' => 'Tests/Unit', 'generate' => true],
'test-feature' => ['path' => 'Tests/Feature', 'generate' => true],
'repository' => ['path' => 'Repositories', 'generate' => false],
'event' => ['path' => 'Events', 'generate' => false],
'listener' => ['path' => 'Listeners', 'generate' => false],
@@ -7,6 +7,7 @@
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class TestMakeCommand extends GeneratorCommand
{
@@ -18,7 +19,10 @@ class TestMakeCommand extends GeneratorCommand
public function getDefaultNamespace() : string
{
return $this->laravel['modules']->config('paths.generator.test.path', 'Tests');
if ($this->option('feature')) {
return $this->laravel['modules']->config('paths.generator.test-feature.path', 'Tests/Feature');
}
return $this->laravel['modules']->config('paths.generator.test.path', 'Tests/Unit');
}
/**
@@ -34,14 +38,30 @@ protected function getArguments()
];
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['feature', false, InputOption::VALUE_NONE, 'Create a feature test.'],
];
}
/**
* @return mixed
*/
protected function getTemplateContents()
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());
$stub = '/unit-test.stub';
return (new Stub('/unit-test.stub', [
if ($this->option('feature')) {
$stub = '/feature-test.stub';
}
return (new Stub($stub, [
'NAMESPACE' => $this->getClassNamespace($module),
'CLASS' => $this->getClass(),
]))->render();
@@ -54,7 +74,11 @@ protected function getDestinationFilePath()
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());
$testPath = GenerateConfigReader::read('test');
if ($this->option('feature')) {
$testPath = GenerateConfigReader::read('test-feature');
} else {
$testPath = GenerateConfigReader::read('test');
}
return $path . $testPath->getPath() . '/' . $this->getFileName() . '.php';
}
@@ -0,0 +1,22 @@
<?php

namespace $NAMESPACE$;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class $CLASS$ extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function testExample()
{
$response = $this->get('/');

$response->assertStatus(200);
}
}
@@ -3,12 +3,13 @@
namespace $NAMESPACE$;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class $CLASS$ extends TestCase
{
/**
* A basic test example.
* A basic unit test example.
*
* @return void
*/
@@ -68,7 +68,8 @@ protected function getEnvironmentSetUp($app)
'views' => ['path' => 'Resources/views', 'generate' => true],
'policies' => ['path' => 'Policies', 'generate' => true],
'rules' => ['path' => 'Rules', 'generate' => true],
'test' => ['path' => 'Tests', 'generate' => true],
'test-feature' => ['path' => 'Tests/Feature', 'generate' => true],
'test' => ['path' => 'Tests/Unit', 'generate' => true],
'jobs' => ['path' => 'Jobs', 'generate' => true],
'emails' => ['path' => 'Emails', 'generate' => true],
'notifications' => ['path' => 'Notifications', 'generate' => true],
@@ -35,28 +35,52 @@ public function tearDown(): void
public function it_generates_a_new_test_class()
{
$this->artisan('module:make-test', ['name' => 'EloquentPostRepositoryTest', 'module' => 'Blog']);
$this->artisan('module:make-test', ['name' => 'EloquentPostRepositoryTest', 'module' => 'Blog', '--feature' => true]);
$this->assertTrue(is_file($this->modulePath . '/Tests/EloquentPostRepositoryTest.php'));
$this->assertTrue(is_file($this->modulePath . '/Tests/Unit/EloquentPostRepositoryTest.php'));
$this->assertTrue(is_file($this->modulePath . '/Tests/Feature/EloquentPostRepositoryTest.php'));
}
/** @test */
public function it_generated_correct_file_with_content()
public function it_generated_correct_unit_file_with_content()
{
$this->artisan('module:make-test', ['name' => 'EloquentPostRepositoryTest', 'module' => 'Blog']);
$file = $this->finder->get($this->modulePath . '/Tests/EloquentPostRepositoryTest.php');
$file = $this->finder->get($this->modulePath . '/Tests/Unit/EloquentPostRepositoryTest.php');
$this->assertMatchesSnapshot($file);
}
/** @test */
public function it_can_change_the_default_namespace()
public function it_generated_correct_feature_file_with_content()
{
$this->app['config']->set('modules.paths.generator.test.path', 'SuperTests');
$this->artisan('module:make-test', ['name' => 'EloquentPostRepositoryTest', 'module' => 'Blog', '--feature' => true]);
$file = $this->finder->get($this->modulePath . '/Tests/Feature/EloquentPostRepositoryTest.php');
$this->assertMatchesSnapshot($file);
}
/** @test */
public function it_can_change_the_default_unit_namespace()
{
$this->app['config']->set('modules.paths.generator.test.path', 'SuperTests/Unit');
$this->artisan('module:make-test', ['name' => 'EloquentPostRepositoryTest', 'module' => 'Blog']);
$file = $this->finder->get($this->modulePath . '/SuperTests/EloquentPostRepositoryTest.php');
$file = $this->finder->get($this->modulePath . '/SuperTests/Unit/EloquentPostRepositoryTest.php');
$this->assertMatchesSnapshot($file);
}
/** @test */
public function it_can_change_the_default_feature_namespace()
{
$this->app['config']->set('modules.paths.generator.test-feature.path', 'SuperTests/Feature');
$this->artisan('module:make-test', ['name' => 'EloquentPostRepositoryTest', 'module' => 'Blog', '--feature' => true]);
$file = $this->finder->get($this->modulePath . '/SuperTests/Feature/EloquentPostRepositoryTest.php');
$this->assertMatchesSnapshot($file);
}
@@ -0,0 +1,23 @@
<?php return '<?php
namespace Modules\\Blog\\SuperTests\\Feature;
use Tests\\TestCase;
use Illuminate\\Foundation\\Testing\\WithFaker;
use Illuminate\\Foundation\\Testing\\RefreshDatabase;
class EloquentPostRepositoryTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function testExample()
{
$response = $this->get(\'/\');
$response->assertStatus(200);
}
}
';
@@ -1,14 +1,15 @@
<?php return '<?php
namespace Modules\\Blog\\Tests;
namespace Modules\\Blog\\SuperTests\\Unit;
use Tests\\TestCase;
use Illuminate\\Foundation\\Testing\\WithFaker;
use Illuminate\\Foundation\\Testing\\RefreshDatabase;
class EloquentPostRepositoryTest extends TestCase
{
/**
* A basic test example.
* A basic unit test example.
*
* @return void
*/
@@ -0,0 +1,23 @@
<?php return '<?php
namespace Modules\\Blog\\Tests\\Feature;
use Tests\\TestCase;
use Illuminate\\Foundation\\Testing\\WithFaker;
use Illuminate\\Foundation\\Testing\\RefreshDatabase;
class EloquentPostRepositoryTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function testExample()
{
$response = $this->get(\'/\');
$response->assertStatus(200);
}
}
';
@@ -1,14 +1,15 @@
<?php return '<?php
namespace Modules\\Blog\\SuperTests;
namespace Modules\\Blog\\Tests\\Unit;
use Tests\\TestCase;
use Illuminate\\Foundation\\Testing\\WithFaker;
use Illuminate\\Foundation\\Testing\\RefreshDatabase;
class EloquentPostRepositoryTest extends TestCase
{
/**
* A basic test example.
* A basic unit test example.
*
* @return void
*/

0 comments on commit 04afa51

Please sign in to comment.
You can’t perform that action at this time.