Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

3.2 KB
Raw Blame
<?php

use App\Events\RestoreJobFinished;
use App\Events\S3RestoreJobFinished;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Process;
use Tests\TestCase;

uses(TestCase::class, RefreshDatabase::class);

/**
 * Tests for RestoreJobFinished and S3RestoreJobFinished events to ensure they handle
 * null server scenarios gracefully (when server is deleted during operation).
 */
describe('RestoreJobFinished null server handling', function () {
    beforeEach(function () {
        Process::fake();
    });

    it('handles null server gracefully in RestoreJobFinished event', function () {
        // Server 999 does not exist (it was deleted during the restore)
        $data = [
            'scriptPath' => '/tmp/script.sh',
            'tmpPath' => '/tmp/backup.sql',
            'container' => 'test-container',
            'serverId' => 999,
        ];

        // Should not throw an error when server is null
        expect(fn () => new RestoreJobFinished($data))->not->toThrow(\Throwable::class);
        Process::assertNothingRan();
    });

    it('handles null server gracefully in S3RestoreJobFinished event', function () {
        // Server 999 does not exist (it was deleted during the restore)
        $data = [
            'containerName' => 'helper-container',
            'serverTmpPath' => '/tmp/downloaded.sql',
            'scriptPath' => '/tmp/script.sh',
            'containerTmpPath' => '/tmp/container-file.sql',
            'container' => 'test-container',
            'serverId' => 999,
        ];

        // Should not throw an error when server is null
        expect(fn () => new S3RestoreJobFinished($data))->not->toThrow(\Throwable::class);
        Process::assertNothingRan();
    });

    it('handles empty serverId in RestoreJobFinished event', function () {
        $data = [
            'scriptPath' => '/tmp/script.sh',
            'tmpPath' => '/tmp/backup.sql',
            'container' => 'test-container',
            'serverId' => null,
        ];

        // Should not throw an error when serverId is null
        expect(fn () => new RestoreJobFinished($data))->not->toThrow(\Throwable::class);
    });

    it('handles empty serverId in S3RestoreJobFinished event', function () {
        $data = [
            'containerName' => 'helper-container',
            'serverTmpPath' => '/tmp/downloaded.sql',
            'scriptPath' => '/tmp/script.sh',
            'containerTmpPath' => '/tmp/container-file.sql',
            'container' => 'test-container',
            'serverId' => null,
        ];

        // Should not throw an error when serverId is null
        expect(fn () => new S3RestoreJobFinished($data))->not->toThrow(\Throwable::class);
    });

    it('handles missing data gracefully in RestoreJobFinished', function () {
        $data = [];

        // Should not throw an error when data is empty
        expect(fn () => new RestoreJobFinished($data))->not->toThrow(\Throwable::class);
    });

    it('handles missing data gracefully in S3RestoreJobFinished', function () {
        $data = [];

        // Should not throw an error when data is empty
        expect(fn () => new S3RestoreJobFinished($data))->not->toThrow(\Throwable::class);
    });
});