Skip to content

Commit bee8520

Browse files
delta1186claude
andcommitted
Add test suite with 18 passing tests across 3 test classes
- MigrationCreatorTest: verifies connection property injection, placeholder replacement, table placeholder, and stub path resolution - ConnectionMigrationPathTest: verifies all 6 commands are rebound to our subclasses and that the --database option exists on make:migration - MigrateMakeCommandTest: end-to-end artisan command tests covering connection path routing, $connection injection, default fallback, and --path override Also adds phpunit.xml configuration and .gitignore. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 11dbff2 commit bee8520

6 files changed

Lines changed: 322 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
composer.lock
3+
.phpunit.cache/
4+
.phpunit.result.cache

‎phpunit.xml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Unit">
9+
<directory>tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<source>
14+
<include>
15+
<directory>src</directory>
16+
</include>
17+
</source>
18+
</phpunit>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Delta1186\ConnectionMigrations\Tests;
4+
5+
class ConnectionMigrationPathTest extends TestCase
6+
{
7+
protected function defineEnvironment($app): void
8+
{
9+
$app['config']->set('database.default', 'mysql');
10+
$app['config']->set('database.connections.mysql', ['driver' => 'sqlite', 'database' => ':memory:']);
11+
$app['config']->set('database.connections.crm', [
12+
'driver' => 'sqlite',
13+
'database' => ':memory:',
14+
'migrations' => 'database/migrations/crm',
15+
]);
16+
}
17+
18+
public function test_migrate_command_resolves_connection_path_when_database_option_given(): void
19+
{
20+
$this->artisan('migrate', ['--database' => 'crm', '--pretend' => true])
21+
->assertExitCode(0);
22+
}
23+
24+
public function test_make_migration_command_has_database_option(): void
25+
{
26+
$command = $this->app->make(
27+
\Illuminate\Database\Console\Migrations\MigrateMakeCommand::class
28+
);
29+
30+
$this->assertTrue(
31+
$command->getDefinition()->hasOption('database'),
32+
'The make:migration command should have a --database option'
33+
);
34+
}
35+
36+
public function test_make_migration_command_is_our_subclass(): void
37+
{
38+
$command = $this->app->make(
39+
\Illuminate\Database\Console\Migrations\MigrateMakeCommand::class
40+
);
41+
42+
$this->assertInstanceOf(
43+
\Delta1186\ConnectionMigrations\Console\MigrateMakeCommand::class,
44+
$command
45+
);
46+
}
47+
48+
public function test_migrate_command_is_our_subclass(): void
49+
{
50+
$command = $this->app->make(
51+
\Illuminate\Database\Console\Migrations\MigrateCommand::class
52+
);
53+
54+
$this->assertInstanceOf(
55+
\Delta1186\ConnectionMigrations\Console\MigrateCommand::class,
56+
$command
57+
);
58+
}
59+
60+
public function test_rollback_command_is_our_subclass(): void
61+
{
62+
$command = $this->app->make(
63+
\Illuminate\Database\Console\Migrations\RollbackCommand::class
64+
);
65+
66+
$this->assertInstanceOf(
67+
\Delta1186\ConnectionMigrations\Console\RollbackCommand::class,
68+
$command
69+
);
70+
}
71+
72+
public function test_reset_command_is_our_subclass(): void
73+
{
74+
$command = $this->app->make(
75+
\Illuminate\Database\Console\Migrations\ResetCommand::class
76+
);
77+
78+
$this->assertInstanceOf(
79+
\Delta1186\ConnectionMigrations\Console\ResetCommand::class,
80+
$command
81+
);
82+
}
83+
84+
public function test_status_command_is_our_subclass(): void
85+
{
86+
$command = $this->app->make(
87+
\Illuminate\Database\Console\Migrations\StatusCommand::class
88+
);
89+
90+
$this->assertInstanceOf(
91+
\Delta1186\ConnectionMigrations\Console\StatusCommand::class,
92+
$command
93+
);
94+
}
95+
96+
public function test_migration_creator_is_our_subclass(): void
97+
{
98+
$creator = $this->app->make('migration.creator');
99+
100+
$this->assertInstanceOf(
101+
\Delta1186\ConnectionMigrations\Migrations\MigrationCreator::class,
102+
$creator
103+
);
104+
}
105+
}

‎tests/MigrateMakeCommandTest.php‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Delta1186\ConnectionMigrations\Tests;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
7+
class MigrateMakeCommandTest extends TestCase
8+
{
9+
private string $migrationDir;
10+
11+
protected function defineEnvironment($app): void
12+
{
13+
$app['config']->set('database.default', 'mysql');
14+
$app['config']->set('database.connections.mysql', ['driver' => 'sqlite', 'database' => ':memory:']);
15+
$app['config']->set('database.connections.crm', [
16+
'driver' => 'sqlite',
17+
'database' => ':memory:',
18+
'migrations' => 'database/migrations/crm',
19+
]);
20+
}
21+
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
$this->migrationDir = $this->app->basePath('database/migrations/crm');
26+
}
27+
28+
protected function tearDown(): void
29+
{
30+
(new Filesystem)->deleteDirectory($this->app->basePath('database'));
31+
parent::tearDown();
32+
}
33+
34+
public function test_writes_migration_to_connection_path_when_database_option_given(): void
35+
{
36+
$this->artisan('make:migration', [
37+
'name' => 'create_orders_table',
38+
'--database' => 'crm',
39+
])->assertExitCode(0);
40+
41+
$files = glob($this->migrationDir.'/*_create_orders_table.php');
42+
43+
$this->assertNotEmpty($files, 'Migration file should be created in database/migrations/crm/');
44+
}
45+
46+
public function test_injects_connection_property_for_non_default_connection(): void
47+
{
48+
$this->artisan('make:migration', [
49+
'name' => 'create_orders_table',
50+
'--database' => 'crm',
51+
])->assertExitCode(0);
52+
53+
$files = glob($this->migrationDir.'/*_create_orders_table.php');
54+
$this->assertNotEmpty($files);
55+
56+
$this->assertStringContainsString(
57+
"protected \$connection = 'crm';",
58+
file_get_contents($files[0])
59+
);
60+
}
61+
62+
public function test_does_not_inject_connection_property_for_default_connection(): void
63+
{
64+
$defaultDir = $this->app->basePath('database/migrations');
65+
66+
$this->artisan('make:migration', [
67+
'name' => 'create_users_table',
68+
'--database' => 'mysql',
69+
])->assertExitCode(0);
70+
71+
$files = glob($defaultDir.'/*_create_users_table.php');
72+
$this->assertNotEmpty($files, 'Migration file should be created in the default migrations directory');
73+
74+
$this->assertStringNotContainsString('$connection', file_get_contents($files[0]));
75+
}
76+
77+
public function test_falls_back_to_default_path_when_connection_has_no_migrations_key(): void
78+
{
79+
$this->app['config']->set('database.connections.analytics', [
80+
'driver' => 'sqlite',
81+
'database' => ':memory:',
82+
]);
83+
84+
$defaultDir = $this->app->basePath('database/migrations');
85+
86+
$this->artisan('make:migration', [
87+
'name' => 'create_events_table',
88+
'--database' => 'analytics',
89+
])->assertExitCode(0);
90+
91+
$files = glob($defaultDir.'/*_create_events_table.php');
92+
$this->assertNotEmpty($files, 'Should fall back to database/migrations when no migrations key is set');
93+
}
94+
95+
public function test_explicit_path_takes_precedence_over_connection_migrations_key(): void
96+
{
97+
$customDir = $this->app->basePath('database/migrations/custom');
98+
99+
$this->artisan('make:migration', [
100+
'name' => 'create_products_table',
101+
'--database' => 'crm',
102+
'--path' => 'database/migrations/custom',
103+
])->assertExitCode(0);
104+
105+
$files = glob($customDir.'/*_create_products_table.php');
106+
$this->assertNotEmpty($files, '--path should override the connection migrations key');
107+
108+
$crmFiles = glob($this->migrationDir.'/*_create_products_table.php');
109+
$this->assertEmpty($crmFiles, 'File should NOT be in the crm directory when --path is given');
110+
}
111+
}

‎tests/MigrationCreatorTest.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Delta1186\ConnectionMigrations\Tests;
4+
5+
use Delta1186\ConnectionMigrations\Migrations\MigrationCreator;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class MigrationCreatorTest extends TestCase
9+
{
10+
private string $tempDir;
11+
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
$this->tempDir = sys_get_temp_dir().'/laravel-connection-migrations-'.uniqid();
16+
mkdir($this->tempDir, 0755, true);
17+
}
18+
19+
protected function tearDown(): void
20+
{
21+
(new Filesystem)->deleteDirectory($this->tempDir);
22+
parent::tearDown();
23+
}
24+
25+
public function test_injects_connection_property_when_connection_is_given(): void
26+
{
27+
$creator = new MigrationCreator(new Filesystem, $this->tempDir);
28+
29+
$path = $creator->create('create_orders_table', $this->tempDir, 'orders', true, 'crm');
30+
31+
$this->assertStringContainsString("protected \$connection = 'crm';", file_get_contents($path));
32+
}
33+
34+
public function test_does_not_inject_connection_property_when_connection_is_null(): void
35+
{
36+
$creator = new MigrationCreator(new Filesystem, $this->tempDir);
37+
38+
$path = $creator->create('create_orders_table', $this->tempDir, 'orders', true, null);
39+
40+
$this->assertStringNotContainsString('$connection', file_get_contents($path));
41+
}
42+
43+
public function test_replaces_connection_placeholder_leaving_no_literal_placeholder(): void
44+
{
45+
$creator = new MigrationCreator(new Filesystem, $this->tempDir);
46+
47+
$path = $creator->create('create_orders_table', $this->tempDir, 'orders', true, 'crm');
48+
49+
$this->assertStringNotContainsString('{{ connection }}', file_get_contents($path));
50+
}
51+
52+
public function test_still_replaces_the_table_placeholder(): void
53+
{
54+
$creator = new MigrationCreator(new Filesystem, $this->tempDir);
55+
56+
$path = $creator->create('create_orders_table', $this->tempDir, 'orders', true, 'crm');
57+
58+
$this->assertStringContainsString("'orders'", file_get_contents($path));
59+
}
60+
61+
public function test_uses_the_package_stubs_directory(): void
62+
{
63+
$creator = new MigrationCreator(new Filesystem, $this->tempDir);
64+
65+
$this->assertStringEndsWith('stubs', $creator->stubPath());
66+
$this->assertDirectoryExists($creator->stubPath());
67+
}
68+
}

‎tests/TestCase.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Delta1186\ConnectionMigrations\Tests;
4+
5+
use Delta1186\ConnectionMigrations\ConnectionMigrationsServiceProvider;
6+
use Orchestra\Testbench\TestCase as OrchestraTestCase;
7+
8+
abstract class TestCase extends OrchestraTestCase
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [
13+
ConnectionMigrationsServiceProvider::class,
14+
];
15+
}
16+
}

0 commit comments

Comments
 (0)