|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Http\File as HttpFile; |
| 6 | +use Illuminate\Http\UploadedFile; |
| 7 | +use Illuminate\Support\Facades\File; |
| 8 | +use Illuminate\Support\Facades\Storage; |
| 9 | +use Itiden\Backup\Contracts\Repositories\BackupRepository; |
| 10 | +use Itiden\Backup\DataTransferObjects\ChunkyUploadDto; |
| 11 | +use Itiden\Backup\Facades\Backuper; |
| 12 | +use Itiden\Backup\Support\Facades\Chunky; |
| 13 | + |
| 14 | +use function Illuminate\Filesystem\join_paths; |
| 15 | +use function Itiden\Backup\Tests\chunk_file; |
| 16 | +use function Itiden\Backup\Tests\user; |
| 17 | +use function Pest\Laravel\actingAs; |
| 18 | +use function Pest\Laravel\getJson; |
| 19 | +use function Pest\Laravel\postJson; |
| 20 | + |
| 21 | +describe('api:upload', function (): void { |
| 22 | + it('can upload chunks and backup is added to repository', function (): void { |
| 23 | + File::cleanDirectory(config('backup.temp_path')); |
| 24 | + Storage::disk(config('backup.destination.disk'))->deleteDirectory(config('backup.destination.path')); |
| 25 | + |
| 26 | + $backup = Backuper::backup(); |
| 27 | + |
| 28 | + $chunks = chunk_file( |
| 29 | + file: Storage::disk(config('backup.destination.disk'))->path(join_paths($backup->path)), |
| 30 | + path: config('backup.temp_path') . '/test-chunks/', |
| 31 | + buffer: 512, |
| 32 | + ); |
| 33 | + |
| 34 | + $totalSize = Storage::disk(config('backup.destination.disk'))->size(join_paths($backup->path)); |
| 35 | + |
| 36 | + $bodies = $chunks->map(fn(string $chunk, int $index): array => [ |
| 37 | + 'resumableIdentifier' => 'test-chunk-identifier', |
| 38 | + 'resumableFilename' => $backup->name, |
| 39 | + 'resumableTotalChunks' => $chunks->count(), |
| 40 | + 'resumableChunkNumber' => $index + 1, |
| 41 | + 'resumableTotalSize' => $totalSize, |
| 42 | + 'file' => UploadedFile::fake()->createWithContent(basename($chunk), file_get_contents($chunk)), |
| 43 | + ]); |
| 44 | + |
| 45 | + $user = user(); |
| 46 | + $user->makeSuper(); |
| 47 | + $user->save(); |
| 48 | + |
| 49 | + actingAs($user); |
| 50 | + |
| 51 | + $bodies |
| 52 | + ->take($chunks->count() - 1) |
| 53 | + ->each(function (array $values): void { |
| 54 | + $res = postJson(cp_route('itiden.backup.chunky.upload'), $values); |
| 55 | + $res->assertStatus(201); |
| 56 | + $res->assertJsonStructure(['message']); |
| 57 | + }); |
| 58 | + |
| 59 | + expect(app(BackupRepository::class)->all())->toHaveCount(1); |
| 60 | + |
| 61 | + $res = postJson(cp_route('itiden.backup.chunky.upload'), $bodies->last()); |
| 62 | + |
| 63 | + $res->assertSuccessful(); |
| 64 | + expect(app(BackupRepository::class)->all())->toHaveCount(2); |
| 65 | + |
| 66 | + File::cleanDirectory(Chunky::path()); |
| 67 | + File::cleanDirectory(config('backup.temp_path')); |
| 68 | + app(BackupRepository::class)->empty(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('can test if a chunk exists', function (): void { |
| 72 | + File::cleanDirectory(config('backup.temp_path')); |
| 73 | + Storage::disk(config('backup.destination.disk'))->deleteDirectory(config('backup.destination.path')); |
| 74 | + |
| 75 | + $backup = Backuper::backup(); |
| 76 | + |
| 77 | + $chunks = chunk_file( |
| 78 | + file: Storage::disk(config('backup.destination.disk'))->path(join_paths($backup->path)), |
| 79 | + path: config('backup.temp_path') . '/test-chunks/', |
| 80 | + buffer: 512, |
| 81 | + ); |
| 82 | + |
| 83 | + $totalSize = Storage::disk(config('backup.destination.disk'))->size(join_paths($backup->path)); |
| 84 | + |
| 85 | + $bodies = $chunks->map(fn(string $chunk, int $index): array => [ |
| 86 | + 'resumableIdentifier' => 'test-chunk-identifier', |
| 87 | + 'resumableFilename' => $backup->name, |
| 88 | + 'resumableTotalChunks' => $chunks->count(), |
| 89 | + 'resumableChunkNumber' => $index + 1, |
| 90 | + 'resumableTotalSize' => $totalSize, |
| 91 | + 'file' => UploadedFile::fake()->createWithContent(basename($chunk), file_get_contents($chunk)), |
| 92 | + ]); |
| 93 | + |
| 94 | + $user = user(); |
| 95 | + $user->makeSuper(); |
| 96 | + $user->save(); |
| 97 | + |
| 98 | + actingAs($user); |
| 99 | + |
| 100 | + $chunksToTest = $bodies->take($chunks->count() - 1); |
| 101 | + |
| 102 | + $chunksToTest->each(function (array $values): void { |
| 103 | + $res = postJson(cp_route('itiden.backup.chunky.upload'), $values); |
| 104 | + $res->assertStatus(201); |
| 105 | + }); |
| 106 | + |
| 107 | + $chunksToTest->each(function (array $values): void { |
| 108 | + $res = getJson(cp_route('itiden.backup.chunky.test', $values)); |
| 109 | + $res->assertStatus(200); |
| 110 | + }); |
| 111 | + |
| 112 | + File::cleanDirectory(Chunky::path()); |
| 113 | + File::cleanDirectory(config('backup.temp_path')); |
| 114 | + app(BackupRepository::class)->empty(); |
| 115 | + }); |
| 116 | +}); |
0 commit comments