Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JunaidQadirB committed Dec 4, 2021
1 parent 7f48cc7 commit c3aa6f0
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 137 deletions.
86 changes: 36 additions & 50 deletions src/Console/Contracts/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ abstract class GeneratorCommand extends \Illuminate\Console\GeneratorCommand
* @var string
*/
protected $type;
/**
* @var array|string|string[]
*/
public $class;


/**
Expand All @@ -36,16 +40,26 @@ abstract class GeneratorCommand extends \Illuminate\Console\GeneratorCommand
*/
public function handle()
{
// First we need to ensure that the given name is not a reserved word within the PHP
// language and that the class name will actually be valid. If it is not valid we
// can error now and prevent from polluting the filesystem using invalid files.
if ($this->isReservedName($this->getNameInput())) {
$this->error('The name "'.$this->getNameInput().'" is reserved by PHP.');

return false;
}

$name = $this->qualifyClass($this->getNameInput());

$path = $this->getPath($name);
// First we will check to see if the class already exists. If it does, we don't want

// Next, We will check to see if the class already exists. If it does, we don't want
// to create the class and overwrite the user's code. So, we will bail out so the
// code is untouched. Otherwise, we will continue generating this class' files.
if ((!$this->hasOption('force') ||
!$this->option('force')) &&
$this->alreadyExists($this->getNameInput())
) {
$this->error($this->type . ' already exists!');
$this->alreadyExists($this->getNameInput())) {
$this->error($this->type.' already exists!');

return false;
}
Expand All @@ -55,34 +69,19 @@ public function handle()
// stub files so that it gets the correctly formatted namespace and class name.
$this->makeDirectory($path);

$this->files->put($path, $this->buildClass($name));
$this->files->put($path, $this->sortImports($this->buildClass($name)));

$displayPath = str_replace($this->laravel->basePath(), '', $path);

$this->info($this->type . ' created successfully in ' . $displayPath);
$this->info($this->type.' created successfully in '.$displayPath);
}

/**
* Parse the class name and format according to the root namespace.
*
* @param string $name
*
* @return string
*/
protected function qualifyClass($name)
protected function getPath($name)
{
$name = ltrim($name, '\\/');

$rootNamespace = $this->rootNamespace();
if (Str::startsWith($name, $rootNamespace)) {
return $name;
}

$name = str_replace('/', '\\', $name);
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$name = str_replace(array('\\', '\\'), array('/', ''), $name);

return $this->qualifyClass(
$this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name
);
return $this->laravel['path'].'/'.$name.'.php';
}

/**
Expand All @@ -98,13 +97,13 @@ protected function rootNamespace()
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @param string $rootNamespace
*
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace;
return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace;
}

/**
Expand All @@ -117,24 +116,11 @@ protected function getNameInput()
return trim($this->argument('name'));
}

/**
* Get the destination class path.
*
* @param string $name
*
* @return string
*/
protected function getPath($name)
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);

return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php';
}

/**
* Determine if the class already exists.
*
* @param string $rawName
* @param string $rawName
*
* @return bool
*/
Expand All @@ -146,7 +132,7 @@ protected function alreadyExists($rawName)
/**
* Build the directory for the class if necessary.
*
* @param string $path
* @param string $path
*
* @return string
*/
Expand All @@ -162,7 +148,7 @@ protected function makeDirectory($path)
/**
* Build the class with the given name.
*
* @param string $name
* @param string $name
*
* @return string
*/
Expand All @@ -178,22 +164,22 @@ protected function buildClass($name)
/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
* @param string $stub
* @param string $name
*
* @return string
*/
protected function replaceClass($stub, $name)
{
$class = str_replace($this->getNamespace($name) . '\\', '', $name);

$class = str_replace($this->getNamespace($name).'\\', '', $name);
$this->class = $name.'::class';
return str_replace('DummyClass', $class, $stub);
}

/**
* Get the full namespace for a given class, without the class name.
*
* @param string $name
* @param string $name
*
* @return string
*/
Expand All @@ -205,8 +191,8 @@ protected function getNamespace($name)
/**
* Replace the namespace for the given stub.
*
* @param string $stub
* @param string $name
* @param string $stub
* @param string $name
*
* @return $this
*/
Expand Down
73 changes: 54 additions & 19 deletions tests/Feature/ControllerMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public function test_it_throws_exception_when_a_name_is_not_passed()

public function test_it_creates_a_controller_with_the_given_name()
{
$this->assertFileDoesNotExist(app_path('Http/Controllers/PostController.php'));

$this->artisan('cray:controller PostController');
$output = Artisan::output();
$expectedOutput = 'Controller created successfully in /app/Http/Controllers/PostController.php' . PHP_EOL;
$this->assertSame($expectedOutput, $output);

$this->assertFileExists(app_path('Http/Controllers/PostController.php'));
}

public function test_it_gives_an_error_if_controller_exists()
Expand All @@ -35,14 +36,35 @@ public function test_it_gives_an_error_if_controller_exists()
$this->assertSame('Controller already exists!' . PHP_EOL, $output);
}

public function test_generates_a_resource_controller_for_the_given_model()
public function test_generates_a_resource_controller_for_the_given_model_if_models_directory_does_not_exist()
{
$this->removeGeneratedFiles();

$this->assertDirectoryDoesNotExist(app_path('Models'));
$this->assertFileDoesNotExist(app_path('/Http/Controllers/PostController.php'));
$this->assertFileDoesNotExist(app_path('Models/Post.php'));
$this->assertFileDoesNotExist(app_path('Post.php'));

$this->artisan('cray:controller PostController --model=Post');
$output = Artisan::output();
$expected = 'Model created successfully in /app/Post.php' . PHP_EOL
. 'Controller created successfully in /app/Http/Controllers/PostController.php' . PHP_EOL;
$this->assertSame($expected, $output);

$this->assertFileExists(app_path('/Http/Controllers/PostController.php'));
$this->assertFileExists(app_path('Post.php'));
$this->assertDirectoryDoesNotExist(app_path('Models'));
$this->assertFileDoesNotExist(app_path('Models/Post.php'));
}

public function test_generates_a_resource_controller_for_the_given_model_if_models_directory_exists()
{
mkdir(app_path('Models'));

$this->assertDirectoryExists(app_path('Models'));
$this->assertFileDoesNotExist(app_path('/Http/Controllers/PostController.php'));
$this->assertFileDoesNotExist(app_path('Models/Post.php'));

$this->artisan('cray:controller PostController --model=Post');

$this->assertFileExists(app_path('/Http/Controllers/PostController.php'));
$this->assertFileExists(app_path('Models/Post.php'));
}

public function test_it_uses_views_path_specified_in_views_dir_option_scenario1()
Expand All @@ -67,8 +89,6 @@ public function test_it_uses_views_path_specified_in_views_dir_option_scenario2(
$this->assertStringContainsStringIgnoringCase("'blog_posts.posts.edit'", $controllerContents);
$this->assertStringContainsStringIgnoringCase("'blog_posts.posts.show'", $controllerContents);
$this->assertStringContainsStringIgnoringCase("'blog_posts.posts.create'", $controllerContents);

unlink(app_path('Http/Controllers/PostController.php'));
}

public function test_it_uses_views_path_specified_in_views_dir_option_scenario3()
Expand All @@ -86,7 +106,9 @@ public function test_it_uses_views_path_specified_in_views_dir_option_scenario4(
{
//Scenario 3
$this->artisan('cray:controller PostController --model=Models/Post --views-dir=blog/posts');

$controllerContents = file_get_contents(app_path('/Http/Controllers/PostController.php'));

$this->assertStringContainsStringIgnoringCase("return view('blog.posts.index', compact('posts'));", $controllerContents);
$this->assertStringContainsStringIgnoringCase("'blog.posts.edit'", $controllerContents);
$this->assertStringContainsStringIgnoringCase("'blog.posts.show'", $controllerContents);
Expand All @@ -95,24 +117,24 @@ public function test_it_uses_views_path_specified_in_views_dir_option_scenario4(

public function test_it_uses_views_path_specified_in_views_dir_option_scenario5()
{
//Scenario 3

$this->artisan('cray:controller PostController --model=Models/Post --controller-dir=dashboard --views-dir=dashboard/system');

$createBladeView = file_get_contents(resource_path('views/dashboard/system/posts/create.blade.php'));
$postController = file_get_contents(app_path('Http/Controllers/Dashboard/PostController.php'));

$this->assertStringContainsString("@include('dashboard.system.posts._form')", $createBladeView, 'Include path is incorrect');

$this->assertStringContainsString("return view('dashboard.system.posts.index'", $postController, 'View path is incorrect');
}

public function test_it_uses_the_specified_route_or_falls_back_to_model_slug()
{
//Scenario 1
$this->artisan('cray:controller PostController --model=Post --views-dir=posts --route-base=my-posts');
/*$this->artisan('cray:controller PostController --model=Post --views-dir=posts --route-base=my-posts');
$controllerContents = file_get_contents(app_path('/Http/Controllers/PostController.php'));
$this->assertStringContainsStringIgnoringCase("return \$this->success('Post added successfully!', 'my-posts.index');", $controllerContents);
$this->assertStringContainsStringIgnoringCase("return \$this->success('Post added successfully!', 'my-posts.index');", $controllerContents);*/

unlink(app_path('Http/Controllers/PostController.php'));
// unlink(app_path('Http/Controllers/PostController.php'));

//Scenario 1
$this->artisan('cray:controller PostController --model=Post --views-dir=posts');
Expand All @@ -123,11 +145,24 @@ public function test_it_uses_the_specified_route_or_falls_back_to_model_slug()
public function test_it_creates_the_controller_in_the_specified_directory_namespace()
{
$this->removeGeneratedFiles();

$this->assertFileDoesNotExist(app_path('Post.php'));
$this->assertFileDoesNotExist(app_path('Http/Controllers/Dashboard/PostController.php'));

$this->artisan('cray:controller PostController --model=Post --controller-dir=Dashboard');
$output = Artisan::output();
$expected = "Model created successfully in /app/Post.php" . PHP_EOL
. "Controller created successfully in /app/Http/Controllers/Dashboard/PostController.php" . PHP_EOL;
$this->assertSame($expected, $output);

$this->assertFileExists(app_path('Post.php'));
$this->assertFileExists(app_path('Http/Controllers/Dashboard/PostController.php'));

$this->removeGeneratedFiles();

$this->assertFileDoesNotExist(app_path('Models/Post.php'));
$this->assertFileDoesNotExist(app_path('Http/Controllers/Dashboard/PostController.php'));

$this->artisan('cray:controller PostController --model=Models/Post --controller-dir=Dashboard');

$this->assertFileExists(app_path('Models/Post.php'));
$this->assertFileExists(app_path('Http/Controllers/Dashboard/PostController.php'));
}

public function test_it_creates_the_controller_with_the_specified_route_base()
Expand Down
Loading

0 comments on commit c3aa6f0

Please sign in to comment.