diff --git a/src/Console/Commands/QuickStartCommand.php b/src/Console/Commands/QuickStartCommand.php index d09caa5..780e3e5 100644 --- a/src/Console/Commands/QuickStartCommand.php +++ b/src/Console/Commands/QuickStartCommand.php @@ -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 @@ -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 !'); } /** @@ -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, '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, '\\', ])) { diff --git a/tests/Feature/Commands/QuickStartCommandTest.php b/tests/Feature/Commands/QuickStartCommandTest.php new file mode 100644 index 0000000..b2bd07e --- /dev/null +++ b/tests/Feature/Commands/QuickStartCommandTest.php @@ -0,0 +1,82 @@ +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'), '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); + } +}