|
| 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 | +} |
0 commit comments