Skip to content

Commit

Permalink
Merge pull request #127 from erhanurgun/fix/quickstart-command-duplic…
Browse files Browse the repository at this point in the history
…ate-routes

Fixed duplicate route creation issue in QuickStartCommand
  • Loading branch information
GautierDele authored Jul 10, 2024
2 parents 86683af + f2372df commit c5d525f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 20 deletions.
61 changes: 43 additions & 18 deletions src/Console/Commands/QuickStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lomkit\Rest\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Lomkit\Rest\Console\ResolvesStubPath;

class QuickStartCommand extends Command
Expand Down Expand Up @@ -32,37 +33,41 @@ public function handle()
{
$this->comment('Generating User Resource...');
$this->callSilent('rest:resource', ['name' => 'UserResource']);
copy($this->resolveStubPath('/stubs/user-resource.stub'), app_path('Rest/Resources/UserResource.php'));

if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
app_path('Rest/Resources/UserResource.php'),
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Resources/UserResource.php')))
);
}

$this->comment('Generating User Controller...');
$this->callSilent('rest:controller', ['name' => 'UsersController']);
copy($this->resolveStubPath('/stubs/user-controller.stub'), app_path('Rest/Controllers/UsersController.php'));

$this->updateUserModelNamespace();
$this->setAppNamespace();
$this->updateApiRoutes();

$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
}

/**
* Update the User model namespace in the generated files.
*
* @return void
*/
protected function updateUserModelNamespace()
{
$resource = app_path('Rest/Resources/UserResource.php');

if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
app_path('Rest/Controllers/UsersController.php'),
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Controllers/UsersController.php')))
$resource,
str_replace('App\Models\Model::class', 'App\Models\User::class', file_get_contents($resource))
);
}

$this->setAppNamespace();
$controller = app_path('Rest/Controllers/UsersController.php');

if (file_exists(base_path('routes/api.php'))) {
if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
base_path('routes/api.php'),
file_get_contents(base_path('routes/api.php')).
'\Lomkit\Rest\Facades\Rest::resource(\'users\', \App\Rest\Controllers\UsersController::class);'
$controller,
str_replace('App\Rest\Resources\ModelResource::class', 'App\Rest\Resources\UserResource::class', file_get_contents($controller))
);
}

$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
}

/**
Expand Down Expand Up @@ -94,4 +99,24 @@ protected function setAppNamespaceOn($file, $namespace)
file_get_contents($file)
));
}

/**
* Update the api routes file to include the new resource.
*
* @return void
*/
protected function updateApiRoutes()
{
$routesPath = base_path('routes/api.php');
if (!file_exists($routesPath)) {
file_put_contents($routesPath, '<?php');
}

$routeContent = file_get_contents($routesPath);
$newRoute = "\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);";

if (!Str::contains($routeContent, $newRoute)) {
file_put_contents($routesPath, $routeContent.PHP_EOL.$newRoute);
}
}
}
4 changes: 2 additions & 2 deletions src/Console/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ protected function getPath($name)
*/
protected function buildClass($name)
{
$model = $this->option('model') ?? 'App\\Models\\Model';
$model = $this->option('model');

$modelNamespace = $this->getModelNamespace();

if (is_null($model)) {
$resource = $modelNamespace.str_replace('/', '\\', $this->argument('name'));
$model = $modelNamespace.'Model';
} elseif (!Str::startsWith($model, [
$modelNamespace, '\\',
])) {
Expand Down
82 changes: 82 additions & 0 deletions tests/Feature/Commands/QuickStartCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Lomkit\Rest\Tests\Feature\Commands;

use Illuminate\Support\Facades\File;
use Lomkit\Rest\Tests\Feature\TestCase;

class QuickStartCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

// Ensure api.php exists for tests
if (!File::exists(base_path('routes/api.php'))) {
File::put(base_path('routes/api.php'), '<?php');
}
}

protected function tearDown(): void
{
$this->cleanUp();
parent::tearDown();
}

protected function cleanUp(): void
{
File::deleteDirectory(app_path('Rest'));
File::deleteDirectory(app_path('Models'));
File::delete(base_path('routes/api.php'));
}

public function test_quick_start_command_creates_necessary_files()
{
$this->artisan('rest:quick-start')->assertExitCode(0);

$this->assertFileExists(app_path('Rest/Resources/UserResource.php'));
$this->assertFileExists(app_path('Rest/Controllers/UsersController.php'));
}

public function test_quick_start_command_updates_api_routes()
{
$this->artisan('rest:quick-start')->assertExitCode(0);

$routeContent = File::get(base_path('routes/api.php'));
$this->assertStringContainsString(
"\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);",
$routeContent
);
}

public function test_quick_start_command_does_not_duplicate_routes()
{
$this->artisan('rest:quick-start')->assertExitCode(0);
$this->artisan('rest:quick-start')->assertExitCode(0);

$routeContent = File::get(base_path('routes/api.php'));
$count = substr_count($routeContent, "\Lomkit\Rest\Facades\Rest::resource('users', \App\Rest\Controllers\UsersController::class);");
$this->assertEquals(1, $count, 'The route should only appear once in the file.');
}

public function test_quick_start_command_updates_user_model_namespace()
{
// Simulate the existence of App\Models\User
File::makeDirectory(app_path('Models'), 0755, true);
File::put(app_path('Models/User.php'), '<?php namespace App\Models; class User {}');

$this->artisan('rest:quick-start')->assertExitCode(0);

$this->assertFileExists(app_path('Rest/Resources/UserResource.php'));
$this->assertFileExists(app_path('Rest/Controllers/UsersController.php'));

$resourceContent = File::get(app_path('Rest/Resources/UserResource.php'));
$controllerContent = File::get(app_path('Rest/Controllers/UsersController.php'));

$this->assertStringContainsString('\App\Models\User::class', $resourceContent);

$this->assertStringContainsString('\App\Rest\Resources\UserResource::class', $controllerContent);

$this->assertStringContainsString('public static $model = \App\Models\User::class;', $resourceContent);
}
}

0 comments on commit c5d525f

Please sign in to comment.