diff --git a/web/app/Helpers.php b/web/app/Helpers.php new file mode 100644 index 0000000..c7b16c5 --- /dev/null +++ b/web/app/Helpers.php @@ -0,0 +1,23 @@ +checkCronJob(); }); + static::created(function ($model) { + $model->startBackup(); + }); + static::deleting(function ($model) { if (is_dir($model->path)) { shell_exec('rm -rf ' . $model->path); @@ -82,7 +87,7 @@ class Backup extends Model $backupDoneFile = $this->path.'/backup.done'; if (file_exists($backupDoneFile)) { - $this->size = $this->checkPathSize($this->path); + $this->size = Helpers::checkPathSize($this->path); $this->status = 'completed'; $this->completed = true; $this->completed_at = now(); @@ -96,7 +101,7 @@ class Backup extends Model $checkProcess = shell_exec('ps -p ' . $this->process_id . ' | grep ' . $this->process_id); if (Str::contains($checkProcess, $this->process_id)) { - $this->size = $this->checkPathSize($this->path); + $this->size = Helpers::checkPathSize($this->path); $this->save(); return [ @@ -197,19 +202,4 @@ class Backup extends Model } } - - private function checkPathSize($path) - { - // Check path size - $pathSize = shell_exec('du -sh ' . $this->path); - $pathSize = trim($pathSize); - $pathSize = explode("\t", $pathSize); - - if (isset($pathSize[0])) { - $pathSize = $pathSize[0]; - return $pathSize; - } - - return 0; - } } diff --git a/web/tests/Unit/BackupTest.php b/web/tests/Unit/BackupTest.php index e6f7aba..9a1c080 100644 --- a/web/tests/Unit/BackupTest.php +++ b/web/tests/Unit/BackupTest.php @@ -2,6 +2,12 @@ namespace tests\Unit; +use App\Filament\Enums\BackupStatus; +use App\Helpers; +use App\Models\Backup; +use App\Models\Customer; +use App\Models\HostingPlan; +use App\Models\HostingSubscription; use Faker\Factory; use Tests\Feature\Api\ActionTestCase; @@ -9,7 +15,59 @@ class BackupTest extends ActionTestCase { public function testBackup() { - + $customer = new Customer(); + $customer->name = 'UnitBackupTest' . time(); + $customer->email = 'UnitBackupTest' . time() . '@unit-test.com'; + $customer->save(); + + $hostingPlan = new HostingPlan(); + $hostingPlan->name = 'UnitBackupTest' . time(); + $hostingPlan->description = 'Unit Backup Test'; + $hostingPlan->disk_space = 1000; + $hostingPlan->bandwidth = 1000; + $hostingPlan->databases = 1; + $hostingPlan->additional_services = ['daily_backups']; + $hostingPlan->features = []; + $hostingPlan->default_server_application_type = 'apache_php'; + $hostingPlan->default_server_application_settings = [ + 'php_version' => '8.3', + ]; + $hostingPlan->save(); + + $hostingSubscription = new HostingSubscription(); + $hostingSubscription->customer_id = $customer->id; + $hostingSubscription->hosting_plan_id = $hostingPlan->id; + $hostingSubscription->domain = 'unit-backup-test' . time() . '.com'; + $hostingSubscription->save(); + + $backup = new Backup(); + $backup->backup_type = 'hosting_subscription'; + $backup->hosting_subscription_id = $hostingSubscription->id; + $backup->save(); + + $backupId = $backup->id; + + $findBackup = false; + $backupCompleted = false; + for ($i = 0; $i < 50; $i++) { + $findBackup = Backup::where('id', $backupId)->first(); + $findBackup->checkBackup(); + if ($findBackup->status == BackupStatus::Completed) { + $backupCompleted = true; + break; + } + sleep(1); + } + + $this->assertTrue($backupCompleted); + $this->assertNotEmpty($findBackup->filepath); + $this->assertTrue(file_exists($findBackup->filepath)); + + $getFilesize = filesize($findBackup->filepath); + $this->assertGreaterThan(0, $getFilesize); + + $this->assertSame(Helpers::checkPathSize($findBackup->path), $findBackup->size); } + }