|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WatheqAlshowaiter\BackupTables\Tests; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 6 | +use Illuminate\Support\Facades\Schema; |
| 7 | +use WatheqAlshowaiter\BackupTables\Commands\BackupTableCommand; |
| 8 | +use WatheqAlshowaiter\BackupTables\Tests\Models\Father; |
| 9 | +use WatheqAlshowaiter\BackupTables\Tests\Models\Mother; |
| 10 | + |
| 11 | +class BackupTableCommandTest extends TestCase |
| 12 | +{ |
| 13 | + use RefreshDatabase; |
| 14 | + |
| 15 | + /** @test */ |
| 16 | + public function it_can_backup_a_table() |
| 17 | + { |
| 18 | + Schema::create('test_table', function ($table) { |
| 19 | + $table->id(); |
| 20 | + $table->string('name'); |
| 21 | + $table->timestamps(); |
| 22 | + }); |
| 23 | + |
| 24 | + $this->artisan('backup:tables', ['targets' => 'test_table']) |
| 25 | + ->assertSuccessful(); |
| 26 | + |
| 27 | + $backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s'); |
| 28 | + |
| 29 | + $this->assertTrue( |
| 30 | + Schema::hasTable($backupTablePattern), |
| 31 | + "Backup table was not created" |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** @test */ |
| 36 | + public function it_can_backup_a_table_by_classname() |
| 37 | + { |
| 38 | + Schema::create('test_table', function ($table) { |
| 39 | + $table->id(); |
| 40 | + $table->string('name'); |
| 41 | + $table->timestamps(); |
| 42 | + }); |
| 43 | + |
| 44 | + $this->artisan(BackupTableCommand::class, ['targets' => 'test_table']) |
| 45 | + ->assertSuccessful(); |
| 46 | + |
| 47 | + $backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s'); |
| 48 | + |
| 49 | + $this->assertTrue( |
| 50 | + Schema::hasTable($backupTablePattern), |
| 51 | + "Backup table was not created" |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** @test */ |
| 56 | + public function it_fails_when_table_does_not_exist() |
| 57 | + { |
| 58 | + $this->artisan('backup:tables', ['targets' => 'non_existent_table']) |
| 59 | + ->assertFailed(); |
| 60 | + } |
| 61 | + |
| 62 | + /** @test */ |
| 63 | + public function it_can_backup_multiple_tables() |
| 64 | + { |
| 65 | + $tables = ['test_table_1', 'test_table_2']; |
| 66 | + |
| 67 | + foreach ($tables as $table) { |
| 68 | + Schema::create($table, function ($table) { |
| 69 | + $table->id(); |
| 70 | + $table->string('name'); |
| 71 | + $table->timestamps(); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + $this->artisan('backup:tables', ['targets' => implode(' ', $tables)]) |
| 76 | + ->assertSuccessful(); |
| 77 | + |
| 78 | + foreach ($tables as $table) { |
| 79 | + $backupTablePattern = $table.'_backup_'.now()->format('Y_m_d_H_i_s'); |
| 80 | + |
| 81 | + $this->assertTrue( |
| 82 | + Schema::hasTable($backupTablePattern), |
| 83 | + "Backup table was not created" |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + /** @test */ |
| 90 | + public function it_can_backup_multiple_models() |
| 91 | + { |
| 92 | + $models = [Father::class, Mother::class]; |
| 93 | + |
| 94 | + $this->artisan('backup:tables', ['targets' => implode(' ', $models)]) |
| 95 | + ->assertSuccessful(); |
| 96 | + |
| 97 | + $backupTablePattern1 = 'fathers_backup_'.now()->format('Y_m_d_H_i_s'); |
| 98 | + $backupTablePattern2 = 'mothers_backup_'.now()->format('Y_m_d_H_i_s'); |
| 99 | + |
| 100 | + $this->assertTrue( |
| 101 | + Schema::hasTable($backupTablePattern1), |
| 102 | + "Backup table was not created" |
| 103 | + ); |
| 104 | + |
| 105 | + $this->assertTrue( |
| 106 | + Schema::hasTable($backupTablePattern2), |
| 107 | + "Backup table was not created" |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** @test */ |
| 112 | + public function it_fails_when_any_table_does_not_exist() |
| 113 | + { |
| 114 | + Schema::create('existing_table', function ($table) { |
| 115 | + $table->id(); |
| 116 | + $table->timestamps(); |
| 117 | + }); |
| 118 | + |
| 119 | + $this->artisan('backup:tables', ['targets' => 'existing_table non_existent_table']) |
| 120 | + ->assertSuccessful(); |
| 121 | + } |
| 122 | +} |
0 commit comments