Skip to content

Commit

Permalink
Merge pull request #12 from fnsc/feat/upload-many
Browse files Browse the repository at this point in the history
feat(uploadMany): add upload many feature
  • Loading branch information
fnsc authored Jul 4, 2023
2 parents 08df2b5 + 3bb1223 commit 72434b2
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/web.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?php

/**
* Examples
*/

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use LaravelGoogleDrive\GoogleDrive;

/**
* Upload single file example
*/
Route::post(
'/',
function (Request $request, GoogleDrive $service): JsonResponse {
Expand All @@ -19,6 +26,31 @@ function (Request $request, GoogleDrive $service): JsonResponse {
}
);

/**
* Upload many example
*/
Route::post(
'/upload-many',
function (Request $request, GoogleDrive $service): JsonResponse {
$uploadedFiles = $request->file('files');
$result = $service->uploadMany($uploadedFiles);

$payload = [];

foreach ($result as $fileData) {
$payload[] = [
'folder_id' => $fileData->getFolderId(),
'file_id' => $fileData->getFileId(),
];
}

return new JsonResponse($payload);
}
);

/**
* Download single file example
*/
Route::get('/download', function (GoogleDrive $service): Response {
$file = $service->get('file.txt', '1ch20VwBUDffhnD3qzr0_DZk4Dk5pOKO321');

Expand Down
9 changes: 9 additions & 0 deletions src/Domain/Exceptions/InvalidDataProvidedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace LaravelGoogleDrive\Domain\Exceptions;

use Exception;

class InvalidDataProvidedException extends Exception
{
}
24 changes: 24 additions & 0 deletions src/Infra/Handlers/GoogleDrive.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LaravelGoogleDrive\Application\Uploader;
use LaravelGoogleDrive\Domain\Entities\GoogleDriveFile;
use LaravelGoogleDrive\Domain\Entities\GoogleDriveFileData;
use LaravelGoogleDrive\Domain\Exceptions\InvalidDataProvidedException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class GoogleDrive
Expand All @@ -27,6 +28,29 @@ public function upload(UploadedFile $uploadedFile, string $folderId = ''): Googl
return $this->uploader->upload($file, $folderId);
}

/**
* @param UploadedFile[] $uploadedFiles
* @param string $folderId
* @return GoogleDriveFileData[]
* @throws InvalidDataProvidedException
*/
public function uploadMany(array $uploadedFiles, string $folderId = ''): array
{
$result = [];

foreach ($uploadedFiles as $uploadedFile) {
if (!($uploadedFile instanceof UploadedFile)) {
throw new InvalidDataProvidedException(
'Invalid data type. The provided input is not an instance of UploadedFile.'
);
}

$result[] = $this->upload($uploadedFile, $folderId);
}

return $result;
}

public function get(string $fileName, string $fileId): GoogleDriveFile
{
return $this->getter->get($fileName, $fileId);
Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/src/Infra/Handlers/GoogleDriveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use LaravelGoogleDrive\Application\Uploader;
use LaravelGoogleDrive\Domain\Entities\GoogleDriveFile;
use LaravelGoogleDrive\Domain\Entities\GoogleDriveFileData;
use LaravelGoogleDrive\Domain\Exceptions\InvalidDataProvidedException;
use Mockery as m;
use Tests\LeanTestCase;

Expand Down Expand Up @@ -50,6 +51,86 @@ public function testShouldUploadTheGivenFile(): void
$this->assertSame('63ab4f34fecd335a6c043105', $result->getFolderId());
}

public function testShouldUploadMoreThanOneFile(): void
{
// Set
$uploader = $this->createMock(Uploader::class);
$getter = m::mock(Getter::class);
/** @phpstan-ignore-next-line */
$handler = new GoogleDrive($uploader, $getter);

$uploadedFile1 = new UploadedFile(
$this->getFixture('file.txt'),
'file.txt',
'text/plain'
);

$uploadedFile2 = new UploadedFile(
$this->getFixture('test.jpeg'),
'test.jpeg',
'image/jpeg'
);

$uploadedFiles = [
$uploadedFile1,
$uploadedFile2,
];

$fileData1 = new GoogleDriveFileData(
fileId: '63ab4f34fecd335a6c043104',
folderId: '63ab4f34fecd335a6c043105'
);

$fileData2 = new GoogleDriveFileData(
fileId: '64a3816f4c60b3fa83089850',
folderId: '63ab4f34fecd335a6c043105'
);

// Expectations
$uploader->expects($this->exactly(2))
->method('upload')
->willReturnOnConsecutiveCalls($fileData1, $fileData2);

// Action
$result = $handler->uploadMany($uploadedFiles);

// Assertions
$this->assertInstanceOf(GoogleDriveFileData::class, $result[0]);
$this->assertSame('63ab4f34fecd335a6c043104', $result[0]->getFileId());
$this->assertSame(
'63ab4f34fecd335a6c043105',
$result[0]->getFolderId()
);
$this->assertSame('64a3816f4c60b3fa83089850', $result[1]->getFileId());
$this->assertSame(
'63ab4f34fecd335a6c043105',
$result[1]->getFolderId()
);
}

public function testShouldThrowAnExceptionWhenTheGivenDataIsInvalid(): void
{
// Set
$uploader = m::mock(Uploader::class);
$getter = m::mock(Getter::class);
/** @phpstan-ignore-next-line */
$handler = new GoogleDrive($uploader, $getter);

$uploadedFiles = [
'invalid data',
];

// Expectations
$this->expectException(InvalidDataProvidedException::class);
$this->expectExceptionMessage(
'Invalid data type. The provided input is not an instance of UploadedFile.'
);

// Action
/** @phpstan-ignore-next-line */
$handler->uploadMany($uploadedFiles);
}

public function testShouldGetTheRequestedFile(): void
{
// Set
Expand Down

0 comments on commit 72434b2

Please sign in to comment.