|
| 1 | +<?php |
| 2 | + |
| 3 | +use DirectoryTree\Metrics\Commands\MakeMetricsTable; |
| 4 | +use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles; |
| 5 | + |
| 6 | +uses(InteractsWithPublishedFiles::class); |
| 7 | + |
| 8 | +it('creates a migration file with the correct name', function () { |
| 9 | + $this->artisan(MakeMetricsTable::class, ['name' => 'test_metrics']) |
| 10 | + ->assertSuccessful(); |
| 11 | + |
| 12 | + $this->assertMigrationFileExists('*_create_test_metrics_table.php'); |
| 13 | +}); |
| 14 | + |
| 15 | +it('displays success message with migration name', function () { |
| 16 | + $this->artisan(MakeMetricsTable::class, ['name' => 'user_metrics']) |
| 17 | + ->expectsOutputToContain('created successfully') |
| 18 | + ->assertSuccessful(); |
| 19 | +}); |
| 20 | + |
| 21 | +it('generates migration with correct table name in schema', function () { |
| 22 | + $this->artisan(MakeMetricsTable::class, ['name' => 'custom_metrics']) |
| 23 | + ->assertSuccessful(); |
| 24 | + |
| 25 | + $this->assertMigrationFileContains([ |
| 26 | + "Schema::create('custom_metrics'", |
| 27 | + "Schema::dropIfExists('custom_metrics'", |
| 28 | + ], '*_create_custom_metrics_table.php'); |
| 29 | +}); |
| 30 | + |
| 31 | +it('generates migration with all required columns', function () { |
| 32 | + $this->artisan(MakeMetricsTable::class, ['name' => 'test_metrics']) |
| 33 | + ->assertSuccessful(); |
| 34 | + |
| 35 | + $this->assertMigrationFileContains([ |
| 36 | + '$table->id()', |
| 37 | + '$table->string(\'name\')->index()', |
| 38 | + '$table->string(\'category\')->nullable()->index()', |
| 39 | + '$table->nullableMorphs(\'measurable\')', |
| 40 | + '$table->unsignedSmallInteger(\'year\')', |
| 41 | + '$table->unsignedTinyInteger(\'month\')', |
| 42 | + '$table->unsignedTinyInteger(\'day\')', |
| 43 | + '$table->unsignedTinyInteger(\'hour\')->nullable()', |
| 44 | + '$table->unsignedInteger(\'value\')', |
| 45 | + '$table->timestamps()', |
| 46 | + ], '*_create_test_metrics_table.php'); |
| 47 | +}); |
| 48 | + |
| 49 | +it('generates migration with correct index', function () { |
| 50 | + $this->artisan(MakeMetricsTable::class, ['name' => 'test_metrics']) |
| 51 | + ->assertSuccessful(); |
| 52 | + |
| 53 | + $this->assertMigrationFileContains([ |
| 54 | + "\$table->index(['year', 'month', 'day', 'hour'])", |
| 55 | + ], '*_create_test_metrics_table.php'); |
| 56 | +}); |
| 57 | + |
| 58 | +it('generates migration with up and down methods', function () { |
| 59 | + $this->artisan(MakeMetricsTable::class, ['name' => 'test_metrics']) |
| 60 | + ->assertSuccessful(); |
| 61 | + |
| 62 | + $this->assertMigrationFileContains([ |
| 63 | + 'public function up(): void', |
| 64 | + 'public function down(): void', |
| 65 | + ], '*_create_test_metrics_table.php'); |
| 66 | +}); |
| 67 | + |
| 68 | +it('creates migration file with timestamp prefix', function () { |
| 69 | + $this->artisan(MakeMetricsTable::class, ['name' => 'test_metrics']) |
| 70 | + ->assertSuccessful(); |
| 71 | + |
| 72 | + $this->assertMigrationFileExists('*_create_test_metrics_table.php'); |
| 73 | +}); |
| 74 | + |
| 75 | +it('can generate multiple different metrics tables', function () { |
| 76 | + $this->artisan(MakeMetricsTable::class, ['name' => 'user_metrics']) |
| 77 | + ->assertSuccessful(); |
| 78 | + |
| 79 | + sleep(1); // Ensure different timestamp |
| 80 | + |
| 81 | + $this->artisan(MakeMetricsTable::class, ['name' => 'custom_metrics']) |
| 82 | + ->assertSuccessful(); |
| 83 | + |
| 84 | + $this->assertMigrationFileExists('*_create_user_metrics_table.php'); |
| 85 | + $this->assertMigrationFileExists('*_create_custom_metrics_table.php'); |
| 86 | + |
| 87 | + $this->assertMigrationFileContains([ |
| 88 | + "Schema::create('user_metrics'", |
| 89 | + ], '*_create_user_metrics_table.php'); |
| 90 | + |
| 91 | + $this->assertMigrationFileContains([ |
| 92 | + "Schema::create('custom_metrics'", |
| 93 | + ], '*_create_custom_metrics_table.php'); |
| 94 | +}); |
| 95 | + |
| 96 | +it('generates valid php migration file', function () { |
| 97 | + $this->artisan(MakeMetricsTable::class, ['name' => 'test_metrics']) |
| 98 | + ->assertSuccessful(); |
| 99 | + |
| 100 | + $this->assertMigrationFileContains([ |
| 101 | + '<?php', |
| 102 | + 'use Illuminate\Database\Migrations\Migration', |
| 103 | + 'use Illuminate\Database\Schema\Blueprint', |
| 104 | + 'use Illuminate\Support\Facades\Schema', |
| 105 | + 'return new class extends Migration', |
| 106 | + ], '*_create_test_metrics_table.php'); |
| 107 | +}); |
0 commit comments