Skip to content

Commit

Permalink
Merge pull request #5 from WatheqAlshowaiter/feature/console-command
Browse files Browse the repository at this point in the history
refactor: change now() to $now in tests to prevent some issues in som…
  • Loading branch information
WatheqAlshowaiter authored Jan 29, 2025
2 parents 38710c0 + 9d83bcc commit 8b83f49
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tests/BackupTableCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BackupTableCommandTest extends TestCase
/** @test */
public function it_can_backup_a_table()
{
$now = now();
Schema::create('test_table', function ($table) {
$table->bigIncrements('id');
$table->string('name');
Expand All @@ -24,14 +25,15 @@ public function it_can_backup_a_table()
$this->artisan('backup:tables', ['targets' => 'test_table'])
->assertExitCode(BackupTableCommand::SUCCESS);

$backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s');
$backupTablePattern = 'test_table_backup_'.$now->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($backupTablePattern));
}

/** @test */
public function it_can_backup_a_table_by_classname()
{
$now = now();
Schema::create('test_table', function ($table) {
$table->bigIncrements('id');
$table->string('name');
Expand All @@ -41,7 +43,7 @@ public function it_can_backup_a_table_by_classname()
$this->artisan(BackupTableCommand::class, ['targets' => 'test_table'])
->assertExitCode(BackupTableCommand::SUCCESS);

$backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s');
$backupTablePattern = 'test_table_backup_'.$now->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($backupTablePattern));
}
Expand All @@ -56,6 +58,7 @@ public function it_fails_when_table_does_not_exist()
/** @test */
public function it_can_backup_multiple_tables()
{
$now = now();
$tables = ['test_table_1', 'test_table_2'];

foreach ($tables as $table) {
Expand All @@ -70,7 +73,7 @@ public function it_can_backup_multiple_tables()
->assertExitCode(BackupTableCommand::SUCCESS);

foreach ($tables as $table) {
$backupTablePattern = $table.'_backup_'.now()->format('Y_m_d_H_i_s');
$backupTablePattern = $table.'_backup_'.$now->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($backupTablePattern));
}
Expand All @@ -97,6 +100,8 @@ public function it_can_backup_multiple_models()
/** @test */
public function it_fails_when_any_table_does_not_exist_but_saves_corrected_tables()
{
$now = now();

Schema::create('existing_table', function ($table) {
$table->bigIncrements('id');
$table->timestamps();
Expand All @@ -105,8 +110,8 @@ public function it_fails_when_any_table_does_not_exist_but_saves_corrected_table
$this->artisan('backup:tables', ['targets' => 'existing_table', 'non_existent_table'])
->assertExitCode(BackupTableCommand::SUCCESS);

$backupExistingTablePattern = 'existing_table_backup_'.now()->format('Y_m_d_H_i_s');
$backupNonExistingTablePattern = 'non_existent_table_backup_'.now()->format('Y_m_d_H_i_s');
$backupExistingTablePattern = 'existing_table_backup_'.$now->format('Y_m_d_H_i_s');
$backupNonExistingTablePattern = 'non_existent_table_backup_'.$now->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($backupExistingTablePattern));

Expand Down

0 comments on commit 8b83f49

Please sign in to comment.