From 0af36a023bf37a65b5da9ea4d484d41130f8cedf Mon Sep 17 00:00:00 2001 From: Norby Baruani Date: Sun, 7 Jul 2024 21:54:14 +0200 Subject: [PATCH] add controller test --- .../Commands/ModuleMakeControllerCommand.php | 2 - tests/Commands/MakeControllerCommandTest.php | 134 ++++++++++++++++++ tests/Commands/MakeModelCommandTest.php | 15 +- tests/MakeCommandTestCase.php | 34 +++++ 4 files changed, 171 insertions(+), 14 deletions(-) create mode 100644 tests/Commands/MakeControllerCommandTest.php diff --git a/src/Console/Commands/ModuleMakeControllerCommand.php b/src/Console/Commands/ModuleMakeControllerCommand.php index e8f99b3..1ff3e7b 100644 --- a/src/Console/Commands/ModuleMakeControllerCommand.php +++ b/src/Console/Commands/ModuleMakeControllerCommand.php @@ -67,8 +67,6 @@ public function handle() $this->files->put($path, $this->buildClass($name)); $this->logFileCreated($name); - - return true; } protected function getFolderPath(): string diff --git a/tests/Commands/MakeControllerCommandTest.php b/tests/Commands/MakeControllerCommandTest.php new file mode 100644 index 0000000..bea554e --- /dev/null +++ b/tests/Commands/MakeControllerCommandTest.php @@ -0,0 +1,134 @@ +artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'PostController', + '--module' => $this->moduleName, + ] + ) + ->assertSuccessful(); + + $this->assertFileExists(filename: $this->getModulePath().'/Controllers/PostController.php'); + } + + public function test_it_should_create_api_controller() + { + $this->artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'PostController', + '--module' => $this->moduleName, + '--api' => true, + ] + ) + ->assertSuccessful(); + + $this->assertFileExists(filename: $this->getModulePath().'/Controllers/PostController.php'); + + $controller = $this->files->get(path: $this->getModulePath().'/Controllers/PostController.php'); + + $methods = $this->getGeneratedClassMethods($controller); + + $this->assertStringContainsString(needle: 'function index()', haystack: $methods[0]); + $this->assertStringContainsString(needle: 'function show(string $id)', haystack: $methods[1]); + $this->assertStringContainsString(needle: 'function store(Request $request)', haystack: $methods[2]); + $this->assertStringContainsString(needle: 'function update(Request $request, $id)', haystack: $methods[3]); + $this->assertStringContainsString(needle: 'function destroy($id)', haystack: $methods[4]); + } + + public function test_it_should_create_invokable_controller() + { + $this->artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'PostController', + '--module' => $this->moduleName, + '--invokable' => true, + ] + ) + ->assertSuccessful(); + + $this->assertFileExists(filename: $this->getModulePath().'/Controllers/PostController.php'); + + $methods = $this->getGeneratedClassMethods( + subjectFile: $this->files + ->get(path: $this->getModulePath().'/Controllers/PostController.php') + ); + + $this->assertStringContainsString(needle: 'function __invoke(Request $request)', haystack: $methods[0]); + } + + public function test_it_should_create_resourceful_controller() + { + $this->artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'PostController', + '--module' => $this->moduleName, + '--resource' => true, + ] + ) + ->assertSuccessful(); + + $this->assertFileExists(filename: $this->getModulePath().'/Controllers/PostController.php'); + + $methods = $this->getGeneratedClassMethods( + subjectFile: $this->files + ->get(path: $this->getModulePath().'/Controllers/PostController.php') + ); + + $this->assertStringContainsString(needle: 'function index()', haystack: $methods[0]); + $this->assertStringContainsString(needle: 'function create()', haystack: $methods[1]); + $this->assertStringContainsString(needle: 'function store(Request $request)', haystack: $methods[2]); + $this->assertStringContainsString(needle: 'function show($id)', haystack: $methods[3]); + $this->assertStringContainsString(needle: 'function edit($id)', haystack: $methods[4]); + $this->assertStringContainsString(needle: 'function update(Request $request, $id)', haystack: $methods[5]); + $this->assertStringContainsString(needle: 'function destroy($id)', haystack: $methods[6]); + } + + public function test_it_should_create_controller_in_sub_directory() + { + $this->artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'Api/CreatePostController', + '--module' => $this->moduleName, + ] + ) + ->assertSuccessful(); + + $this->assertFileExists(filename: $this->getModulePath().'/Controllers/Api/CreatePostController.php'); + } + + public function test_it_should_not_create_controller_with_duplicate_name() + { + $this->artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'PostController', + '--module' => $this->moduleName, + ] + ) + ->assertSuccessful(); + + $this->assertFileExists(filename: $this->getModulePath().'/Controllers/PostController.php'); + + $this->artisan( + command: 'module:make:controller', + parameters: [ + 'name' => 'PostController', + '--module' => $this->moduleName, + ] + ) + ->assertFailed(); + } +} diff --git a/tests/Commands/MakeModelCommandTest.php b/tests/Commands/MakeModelCommandTest.php index d0e2287..28600e3 100644 --- a/tests/Commands/MakeModelCommandTest.php +++ b/tests/Commands/MakeModelCommandTest.php @@ -33,13 +33,9 @@ public function test_it_creates_a_model_with_migration() ) ->assertExitCode(exitCode: 0); - $this->assertDirectoryExists( - directory: $this->getModulePath($module).'/Database/migrations' - ); - $this->assertFileExists(filename: $this->getModulePath($module).'/Models/Video.php'); + $this->assertMigrationFile(module: $module, migrationFilename: 'create_videos_table.php'); - // TODO: Fix timestamp issue in test - // $this->assertFileExists(filename: $this->getModulePath().'/Database/migration/'.$datetime.'_create_posts_table.php'); + $this->assertFileExists(filename: $this->getModulePath($module).'/Models/Video.php'); } public function test_it_creates_a_model_with_factory() @@ -72,18 +68,13 @@ public function test_it_creates_a_model_with_migration_and_factory() ) ->assertExitCode(exitCode: 0); - $this->assertDirectoryExists( - directory: $this->getModulePath($module).'/Database/migrations' - ); + $this->assertMigrationFile(module: $module, migrationFilename: 'create_listings_table.php'); $this->assertFileExists( filename: $this->getModulePath($module).'/Models/Listing.php' ); // TODO: Fix timestamp issue in test - // $this->assertFileExists( - // filename: $this->getModulePath().'/Database/migration/'.$datetime.'_create_posts_table.php' - // ); //$this->assertFileExists($this->getModulePath($this->moduleName).'/Database/factories/PostFactory.php'); } diff --git a/tests/MakeCommandTestCase.php b/tests/MakeCommandTestCase.php index 626f1bb..a0abc29 100644 --- a/tests/MakeCommandTestCase.php +++ b/tests/MakeCommandTestCase.php @@ -40,4 +40,38 @@ public function cleanUp(): void { $this->files->deleteDirectory(base_path(config('modularize.root_path'))); } + + protected function assertMigrationFile(string $module, string $migrationFilename): void + { + $migrations = $this->files->allFiles(directory: $this->getModulePath($module).'/Database/migrations'); + $this->assertNotEmpty(actual: $migrations); + $this->assertEquals( + expected: 1, + actual: count($migrations) + ); + + /** @var \Symfony\Component\Finder\SplFileInfo */ + $migrationFile = $migrations[0]; + $this->assertStringContainsString( + needle: $migrationFilename, + haystack: $migrationFile->getFilename() + ); + } + + protected function getGeneratedClassMethods(string $subjectFile): array + { + // Define the regex pattern to match the entire function signature, excluding the opening { + $pattern = '/function\s+([a-zA-Z_]\w*)\s*\(([^)]*)\)\s*(?=\{)/'; + preg_match_all(pattern: $pattern, subject: $subjectFile, matches: $match); + + if (empty($match[0])) { + $this->fail('No function found in controller'); + } + + foreach ($match[0] as $function) { + $methods[] = trim($function); + } + + return $methods; + } }