Skip to content

Commit

Permalink
add controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Jul 7, 2024
1 parent 692a5d4 commit 0af36a0
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/Console/Commands/ModuleMakeControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public function handle()
$this->files->put($path, $this->buildClass($name));

$this->logFileCreated($name);

return true;
}

protected function getFolderPath(): string
Expand Down
134 changes: 134 additions & 0 deletions tests/Commands/MakeControllerCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace NorbyBaru\Modularize\Tests\Commands;

use NorbyBaru\Modularize\Tests\MakeCommandTestCase;

class MakeControllerCommandTest extends MakeCommandTestCase
{
public function test_it_create_controller()
{
$this->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();
}
}
15 changes: 3 additions & 12 deletions tests/Commands/MakeModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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');
}

Expand Down
34 changes: 34 additions & 0 deletions tests/MakeCommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 0af36a0

Please sign in to comment.