-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from erhanurgun/fix/quickstart-command-duplic…
…ate-routes Fixed duplicate route creation issue in QuickStartCommand
- Loading branch information
Showing
3 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |