Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^11.5"
"phpunit/phpunit": "^11.5",
"orchestra/testbench": "^10.0"
}
}
71 changes: 71 additions & 0 deletions tests/Feature/Commands/MakeModifyMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Tests\Feature\Commands;

use Tests\TestCase;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\App;

class MakeModifyMigrationTest extends TestCase
{
private string $modulesPath;

protected function setUp(): void
{
parent::setUp();

$this->modulesPath = App::path('Modules');

// Create test module structure
$testModulePath = $this->modulesPath . '/TestModule/Infrastructure/Database/Migrations';
File::makeDirectory($testModulePath, 0755, true);
}

protected function tearDown(): void
{
if (File::exists($this->modulesPath . '/TestModule')) {
File::deleteDirectory($this->modulesPath . '/TestModule');
}

parent::tearDown();
}

/** @test */
public function it_creates_modify_migration_file()
{
$this->artisan('make:modify-migration', [
'name' => 'modify_users_table',
'module' => 'TestModule',
'--table' => 'users'
])->assertExitCode(0);

$migrationPath = $this->modulesPath . '/TestModule/Infrastructure/Database/Migrations';
$files = File::files($migrationPath);

$this->assertCount(1, $files);

$migrationFile = $files[0];
$content = File::get($migrationFile->getPathname());

$this->assertStringContainsString('modify_users_table', $content);
$this->assertStringContainsString('Schema::table', $content);
}

/** @test */
public function it_requires_module_parameter()
{
$this->artisan('make:modify-migration', [
'name' => 'modify_users_table'
])->assertExitCode(1);
}

/** @test */
public function it_validates_module_exists()
{
$this->artisan('make:modify-migration', [
'name' => 'modify_users_table',
'module' => 'NonExistentModule'
])->expectsOutput('Module NonExistentModule does not exist!')
->assertExitCode(1);
}
}
93 changes: 93 additions & 0 deletions tests/Feature/Commands/MakeModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Tests\Feature\Commands;

use Tests\TestCase;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\App;

class MakeModuleTest extends TestCase
{
private string $modulesPath;

protected function setUp(): void
{
parent::setUp();

$this->modulesPath = App::path('Modules');

// Clean up any existing test modules
if (File::exists($this->modulesPath . '/TestModule')) {
File::deleteDirectory($this->modulesPath . '/TestModule');
}
}

protected function tearDown(): void
{
// Clean up test modules
if (File::exists($this->modulesPath . '/TestModule')) {
File::deleteDirectory($this->modulesPath . '/TestModule');
}

parent::tearDown();
}

/** @test */
public function it_creates_basic_module_structure()
{
$this->artisan('make:module', ['name' => 'TestModule'])
->expectsOutput('Module TestModule created successfully!')
->assertExitCode(0);

$modulePath = $this->modulesPath . '/TestModule';

// Check basic directory structure
$this->assertTrue(File::exists($modulePath));
$this->assertTrue(File::exists($modulePath . '/Application'));
$this->assertTrue(File::exists($modulePath . '/Domain'));
$this->assertTrue(File::exists($modulePath . '/Infrastructure'));
$this->assertTrue(File::exists($modulePath . '/Interface'));
}

/** @test */
public function it_creates_module_with_all_options()
{
$this->artisan('make:module', [
'name' => 'TestModule',
'--data' => true,
'--dto' => true,
'--migration' => true,
'--model' => true,
'--repository' => true,
'--service' => true,
'--controller' => true,
'--request' => true,
'--resource' => true,
'--route' => true,
'--seeder' => true,
])->assertExitCode(0);

$modulePath = $this->modulesPath . '/TestModule';

// Check if specific files are created
$this->assertTrue(File::exists($modulePath . '/Domain/Models'));
$this->assertTrue(File::exists($modulePath . '/Infrastructure/Database/Migrations'));
$this->assertTrue(File::exists($modulePath . '/Infrastructure/Repositories'));
$this->assertTrue(File::exists($modulePath . '/Application/Services'));
$this->assertTrue(File::exists($modulePath . '/Interface/Controllers'));
$this->assertTrue(File::exists($modulePath . '/Interface/Routes'));
}

/** @test */
public function it_prevents_creating_duplicate_modules()
{
// Create module first time
$this->artisan('make:module', ['name' => 'TestModule'])
->assertExitCode(0);

// Try to create same module again
$this->artisan('make:module', ['name' => 'TestModule'])
->expectsOutput('Module TestModule already exists!')
->assertExitCode(1);
}
}
101 changes: 101 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Tests;

use Orchestra\Testbench\TestCase as Orchestra;
use Hitech\DDDModularToolkit\Providers\DDDModularToolkitServiceProvider;
use Hitech\DDDModularToolkit\Commands\MakeModule;
use Hitech\DDDModularToolkit\Commands\MakeModifyMigration;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\App;

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

// Ensure modules directory exists
$modulesPath = App::path('Modules');
if (!File::exists($modulesPath)) {
File::makeDirectory($modulesPath, 0755, true);
}

// Manually register commands for testing
$this->artisan('list'); // This forces command registration

// Setup test environment
$this->setUpDatabase();
}

protected function getPackageProviders($app)
{
return [
DDDModularToolkitServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
{
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

// Set up DDD config
$app['config']->set('ddd.blade.is_active', true);
$app['config']->set('ddd.blade.path', 'Blade');
$app['config']->set('ddd.react.is_active', false);
$app['config']->set('ddd.react.path', 'React');
$app['config']->set('ddd.middleware.auth', true);
$app['config']->set('ddd.middleware.api', true);
}

protected function setUpDatabase()
{
// Create basic Laravel tables if needed
}

protected function tearDown(): void
{
// Clean up test modules
$modulesPath = App::path('Modules');
if (File::exists($modulesPath)) {
$directories = File::directories($modulesPath);
foreach ($directories as $directory) {
if (str_contains($directory, 'Test')) {
File::deleteDirectory($directory);
}
}
}

parent::tearDown();
}

/**
* Define additional package providers.
*/
protected function getPackageAliases($app)
{
return [];
}

/**
* Resolve application Console Kernel implementation.
*/
protected function resolveApplicationConsoleKernel($app)
{
$app->singleton('Illuminate\Contracts\Console\Kernel', function ($app) {
$kernel = new \Illuminate\Foundation\Console\Kernel($app, $app['events']);

// Force register our commands
$kernel->registerCommand(new MakeModule());
$kernel->registerCommand(new MakeModifyMigration());

return $kernel;
});
}
}
66 changes: 66 additions & 0 deletions tests/Unit/Helpers/PathHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Unit\Helpers;

use Tests\TestCase;
use Illuminate\Support\Facades\File;

class PathHelperTest extends TestCase
{
private string $testDir;

protected function setUp(): void
{
parent::setUp();

$this->testDir = sys_get_temp_dir() . '/ddd_test_' . uniqid();
File::makeDirectory($this->testDir, 0755, true);

// Create test file structure
File::makeDirectory($this->testDir . '/Module1/Routes', 0755, true);
File::makeDirectory($this->testDir . '/Module2/Routes', 0755, true);
File::put($this->testDir . '/Module1/Routes/web.php', '<?php // test route');
File::put($this->testDir . '/Module2/Routes/web.php', '<?php // test route 2');
}

protected function tearDown(): void
{
if (File::exists($this->testDir)) {
File::deleteDirectory($this->testDir);
}

parent::tearDown();
}

/** @test */
public function glob_recursive_finds_matching_files()
{
$foundFiles = [];

globRecursive(
$this->testDir . '/*/Routes/web.php',
function (string $file) use (&$foundFiles) {
$foundFiles[] = $file;
}
);

$this->assertCount(2, $foundFiles);
$this->assertStringContainsString('Module1/Routes/web.php', $foundFiles[0]);
$this->assertStringContainsString('Module2/Routes/web.php', $foundFiles[1]);
}

/** @test */
public function glob_recursive_handles_no_matches()
{
$foundFiles = [];

globRecursive(
$this->testDir . '/*/NonExistent/*.php',
function (string $file) use (&$foundFiles) {
$foundFiles[] = $file;
}
);

$this->assertCount(0, $foundFiles);
}
}
Loading