PhyrePanel-mirror/web/tests/Unit/ZeroBackupTest.php

132 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2024-04-25 11:00:29 +00:00
<?php
2024-05-14 22:01:19 +00:00
namespace Tests\Unit;
2024-04-25 11:00:29 +00:00
2024-04-25 12:11:44 +00:00
use App\Filament\Enums\BackupStatus;
use App\Helpers;
use App\Models\Backup;
use App\Models\Customer;
use App\Models\HostingPlan;
use App\Models\HostingSubscription;
2024-04-25 11:00:29 +00:00
use Faker\Factory;
2024-04-25 18:57:47 +00:00
use Illuminate\Support\Facades\Artisan;
2024-04-30 22:55:07 +00:00
use Illuminate\Support\Facades\Storage;
2024-04-25 11:00:29 +00:00
use Tests\Feature\Api\ActionTestCase;
2024-05-14 12:33:21 +00:00
class ZeroBackupTest extends ActionTestCase
2024-04-25 11:00:29 +00:00
{
2024-04-25 18:57:47 +00:00
public function testFullBackup()
2024-04-25 11:00:29 +00:00
{
2024-04-29 21:50:50 +00:00
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0);
2024-04-30 22:50:08 +00:00
Artisan::call('phyre:create-daily-full-backup');
2024-04-25 18:57:47 +00:00
$findLastBackup = Backup::orderBy('id', 'asc')->first();
$this->assertNotEmpty($findLastBackup);
$this->assertNotEmpty($findLastBackup->id);
$this->assertNotEmpty($findLastBackup->created_at);
$this->assertSame($findLastBackup->backup_type, 'full');
2024-05-02 19:44:30 +00:00
2024-04-25 18:57:47 +00:00
$backupFinished = false;
2024-05-02 20:06:52 +00:00
for ($i = 0; $i < 100; $i++) {
2024-05-02 19:44:30 +00:00
Artisan::call('phyre:run-backup-checks');
$findLastBackup = Backup::where('id', $findLastBackup->id)->first();
2024-04-25 18:57:47 +00:00
if ($findLastBackup->status == BackupStatus::Completed) {
$backupFinished = true;
break;
}
2024-05-03 14:55:14 +00:00
if ($findLastBackup->status == BackupStatus::Failed) {
$this->fail('Backup failed: '.$findLastBackup->backup_log);
break;
}
2024-04-25 18:57:47 +00:00
sleep(1);
}
2024-05-02 19:44:30 +00:00
2024-05-03 14:55:14 +00:00
if (!$backupFinished) {
$findLastBackup = Backup::where('id', $findLastBackup->id)->first();
$this->fail('Backup not completed: '.$findLastBackup->backup_log);
}
2024-05-02 19:44:30 +00:00
2024-04-25 18:57:47 +00:00
$this->assertTrue($backupFinished);
$this->assertSame($findLastBackup->status, BackupStatus::Completed);
2024-05-02 16:33:47 +00:00
$this->assertNotEmpty($findLastBackup->file_path);
$this->assertTrue(file_exists($findLastBackup->file_path));
2024-04-25 18:57:47 +00:00
2024-04-25 12:38:39 +00:00
$backup = new Backup();
$checkCronJob = $backup->checkCronJob();
$this->assertTrue($checkCronJob);
2024-04-25 12:11:44 +00:00
$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();
2024-04-25 18:57:47 +00:00
$backup->backup_type = 'full';
2024-04-25 12:11:44 +00:00
$backup->save();
$backupId = $backup->id;
$findBackup = false;
$backupCompleted = false;
2024-05-02 20:06:52 +00:00
for ($i = 0; $i < 100; $i++) {
2024-05-02 19:44:30 +00:00
Artisan::call('phyre:run-backup-checks');
2024-04-25 12:11:44 +00:00
$findBackup = Backup::where('id', $backupId)->first();
if ($findBackup->status == BackupStatus::Completed) {
$backupCompleted = true;
break;
}
2024-05-02 20:07:26 +00:00
if ($findBackup->status == BackupStatus::Failed) {
$this->fail('Backup failed: '.$findBackup->backup_log);
2024-05-02 20:22:17 +00:00
break;
2024-05-02 20:07:26 +00:00
}
2024-05-02 19:44:30 +00:00
2024-04-25 12:11:44 +00:00
sleep(1);
}
2024-05-02 20:06:52 +00:00
if (!$backupCompleted) {
$findBackup = Backup::where('id', $backupId)->first();
$this->fail('Backup not completed: '.$findBackup->backup_log);
}
2024-04-25 12:11:44 +00:00
$this->assertTrue($backupCompleted);
2024-05-02 16:33:47 +00:00
$this->assertNotEmpty($findBackup->file_path);
$this->assertTrue(file_exists($findBackup->file_path));
2024-04-25 12:11:44 +00:00
2024-05-02 16:33:47 +00:00
$getFilesize = filesize($findBackup->file_path);
2024-04-25 12:11:44 +00:00
$this->assertGreaterThan(0, $getFilesize);
2024-05-02 19:44:30 +00:00
$this->assertSame($getFilesize, intval($findBackup->size));
2024-04-25 11:00:29 +00:00
2024-05-02 18:15:48 +00:00
Helpers::extractZip($findBackup->file_path, $findBackup->path . '/unit-test');
2024-04-25 12:26:18 +00:00
2024-04-25 12:38:39 +00:00
2024-04-25 11:00:29 +00:00
}
2024-04-25 12:11:44 +00:00
2024-04-25 11:00:29 +00:00
}