Skip to content

Commit bd37ae9

Browse files
chore!: remove chunky facade and add more test cases
BREAKING CHANGE: chunky no longer has a facade in favor of using DI to resolve it
1 parent 63edd09 commit bd37ae9

File tree

5 files changed

+43
-45
lines changed

5 files changed

+43
-45
lines changed

src/Backuper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Itiden\Backup\Models\Metadata;
1616
use Throwable;
1717

18+
use function Illuminate\Filesystem\join_paths;
19+
1820
final class Backuper
1921
{
2022
public function __construct(
@@ -35,7 +37,7 @@ public function backup(): BackupDto
3537
try {
3638
$this->stateManager->setState(State::BackupInProgress);
3739

38-
$temp_zip_path = config('backup.temp_path') . '/temp.zip';
40+
$temp_zip_path = join_paths(config('backup.temp_path'), 'temp.zip');
3941

4042
$zipper = Zipper::write($temp_zip_path);
4143

src/Http/Controllers/UploadController.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
use Itiden\Backup\DataTransferObjects\ChunkyTestDto;
1212
use Itiden\Backup\DataTransferObjects\ChunkyUploadDto;
1313
use Itiden\Backup\Http\Requests\ChunkyUploadRequest;
14-
use Itiden\Backup\Support\Facades\Chunky;
14+
use Itiden\Backup\Support\Chunky;
1515

1616
final readonly class UploadController
1717
{
18-
public function __invoke(ChunkyUploadRequest $request, BackupRepository $repo, Backuper $backuper): JsonResponse
19-
{
20-
return Chunky::put(ChunkyUploadDto::fromRequest($request), onCompleted: function (string $completeFile) use (
18+
public function __invoke(
19+
ChunkyUploadRequest $request,
20+
Chunky $chunky,
21+
BackupRepository $repo,
22+
Backuper $backuper,
23+
): JsonResponse {
24+
return $chunky->put(ChunkyUploadDto::fromRequest($request), onCompleted: function (string $completeFile) use (
2125
$repo,
2226
$backuper,
2327
): void {
@@ -29,8 +33,8 @@ public function __invoke(ChunkyUploadRequest $request, BackupRepository $repo, B
2933
});
3034
}
3135

32-
public function test(Request $request): JsonResponse
36+
public function test(Request $request, Chunky $chunky): JsonResponse
3337
{
34-
return Chunky::exists(ChunkyTestDto::fromRequest($request));
38+
return $chunky->exists(ChunkyTestDto::fromRequest($request));
3539
}
3640
}

src/Support/Facades/Chunky.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/Feature/UploadControllerTest.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
declare(strict_types=1);
44

5-
use Illuminate\Http\File as HttpFile;
65
use Illuminate\Http\UploadedFile;
76
use Illuminate\Support\Facades\File;
87
use Illuminate\Support\Facades\Storage;
98
use Itiden\Backup\Contracts\Repositories\BackupRepository;
10-
use Itiden\Backup\DataTransferObjects\ChunkyUploadDto;
119
use Itiden\Backup\Facades\Backuper;
12-
use Itiden\Backup\Support\Facades\Chunky;
10+
use Itiden\Backup\Support\Chunky;
1311

1412
use function Illuminate\Filesystem\join_paths;
1513
use function Itiden\Backup\Tests\chunk_file;
@@ -63,7 +61,7 @@
6361
$res->assertSuccessful();
6462
expect(app(BackupRepository::class)->all())->toHaveCount(2);
6563

66-
File::cleanDirectory(Chunky::path());
64+
File::cleanDirectory(app(Chunky::class)->path());
6765
File::cleanDirectory(config('backup.temp_path'));
6866
app(BackupRepository::class)->empty();
6967
});
@@ -109,8 +107,27 @@
109107
$res->assertStatus(200);
110108
});
111109

112-
File::cleanDirectory(Chunky::path());
110+
File::cleanDirectory(app(Chunky::class)->path());
113111
File::cleanDirectory(config('backup.temp_path'));
114112
app(BackupRepository::class)->empty();
115113
});
114+
115+
it('returns correct response when chunk doesnt exist', function () {
116+
File::cleanDirectory(config('backup.temp_path'));
117+
Storage::disk(config('backup.destination.disk'))->deleteDirectory(config('backup.destination.path'));
118+
119+
$user = user();
120+
$user->makeSuper();
121+
$user->save();
122+
123+
actingAs($user);
124+
125+
$res = getJson(cp_route('itiden.backup.chunky.test', [
126+
'resumableIdentifier' => 'test-chunk-identifier',
127+
'resumableFilename' => 'test-file.txt',
128+
'resumableChunkNumber' => 1,
129+
]));
130+
131+
$res->assertStatus(404);
132+
});
116133
});

tests/Unit/ChunkyTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Http\UploadedFile;
77
use Illuminate\Support\Facades\File;
88
use Itiden\Backup\DataTransferObjects\ChunkyUploadDto;
9-
use Itiden\Backup\Support\Facades\Chunky;
9+
use Itiden\Backup\Support\Chunky;
1010

1111
use function Itiden\Backup\Tests\chunk_file;
1212

@@ -16,11 +16,11 @@
1616

1717
$dto = new ChunkyUploadDto('name', 1, 1, 1000, $file->hashName(), $file);
1818

19-
$res = Chunky::put($dto);
19+
$res = app(Chunky::class)->put($dto);
2020

2121
expect($res->getStatusCode())->toBe(201);
2222
expect($res->getData(true))->toHaveAttribute('message');
23-
expect(Chunky::path() . '/' . $file->hashName() . '/' . $dto->filename . '.part1')->toBeFile();
23+
expect(app(Chunky::class)->path() . '/' . $file->hashName() . '/' . $dto->filename . '.part1')->toBeFile();
2424
});
2525

2626
it('can assemble file', function (): void {
@@ -46,7 +46,7 @@
4646
$uploadedFile = null;
4747

4848
$responses = $dtos->map(function (ChunkyUploadDto $r) use (&$uploadedFile): JsonResponse {
49-
return Chunky::put($r, onCompleted: function (string $file) use (&$uploadedFile): void {
49+
return app(Chunky::class)->put($r, onCompleted: function (string $file) use (&$uploadedFile): void {
5050
$uploadedFile = $file;
5151
});
5252
});
@@ -56,15 +56,15 @@
5656
->last()
5757
->getData(true))->toHaveKey('file');
5858

59-
expect(Chunky::path('assembled/homepage.md'))->toBeFile();
59+
expect(app(Chunky::class)->path('assembled/homepage.md'))->toBeFile();
6060

61-
expect(File::get(Chunky::path('/assembled/homepage.md')))->toBe(File::get(
61+
expect(File::get(app(Chunky::class)->path('/assembled/homepage.md')))->toBe(File::get(
6262
__DIR__ . '/../__fixtures__/content/collections/pages/homepage.md',
6363
));
6464

65-
expect($uploadedFile)->toEqual(Chunky::path('/assembled/homepage.md'));
65+
expect($uploadedFile)->toEqual(app(Chunky::class)->path('/assembled/homepage.md'));
6666

67-
File::deleteDirectory(Chunky::path());
67+
File::deleteDirectory(app(Chunky::class)->path());
6868
File::deleteDirectory(config('backup.temp_path') . '/chunks');
6969
});
7070
})->group('chunky');

0 commit comments

Comments
 (0)