Skip to content

Commit

Permalink
verify that the methods cleanupFileUsing and `generateFilePathUsing…
Browse files Browse the repository at this point in the history
…` works as expected, added mockery as dev-dependancy.
  • Loading branch information
HelgeSverre committed Jan 4, 2024
1 parent c27f978 commit 911f66f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"require-dev": {
"laravel/pint": "^1.0",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^7.8",
"orchestra/testbench": "^8.8",
"pestphp/pest": "^2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Text/Loaders/Textract/TextractUsingS3Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getFilePath(): string
return $this->defaultFilePathGenerator();
}

/** @noinspection PhpInconsistentReturnPointsInspection */
public function cleanup(string $path)
{
if (static::$cleanupFileUsing) {
Expand Down Expand Up @@ -95,6 +96,5 @@ public function load(mixed $data): ?TextContent
$this->cleanup($path);

return new TextContent($result);

}
}
47 changes: 47 additions & 0 deletions tests/Text/TextLoaderTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

use HelgeSverre\Extractor\Facades\Text;
use HelgeSverre\Extractor\Text\Loaders\Textract\TextractService;
use HelgeSverre\Extractor\Text\Loaders\Textract\TextractUsingS3Upload;
use HelgeSverre\Extractor\Text\TextContent;
use Illuminate\Support\Facades\Storage;

it('Can load Text', function () {
$text = Text::text(file_get_contents(__DIR__.'/../samples/wolt-pizza-norwegian.txt'));
Expand Down Expand Up @@ -94,3 +97,47 @@
'Termination of the Agreement',
);
});

it('removes the file from S3 using the provided cleanup callback', function () {
Storage::fake('s3');

$mock = Mockery::mock(TextractService::class);
$mock->shouldReceive('s3ObjectToText');

$textractUsingS3Upload = new TextractUsingS3Upload($mock);

$testFilePath = 'extractor/test-file.pdf';

Storage::disk('s3')->put($testFilePath, 'Test content');

Storage::disk('s3')->assertExists($testFilePath);

TextractUsingS3Upload::cleanupFileUsing(function ($path) {
Storage::disk('s3')->delete($path);
});

$textractUsingS3Upload->cleanup($testFilePath);

Storage::disk('s3')->assertMissing($testFilePath);
});

it('overrides the default file path generation with a custom callback', function () {
Storage::fake('s3');

$mock = Mockery::mock(TextractService::class);
$mock->shouldReceive('s3ObjectToText');

$textractUsingS3Upload = new TextractUsingS3Upload($mock);

$customFilePath = 'custom-path/custom-file.pdf';

TextractUsingS3Upload::generateFilePathUsing(function () use ($customFilePath) {
return $customFilePath;
});

expect($textractUsingS3Upload->getFilePath())->toBe($customFilePath);

Storage::disk('s3')->put($textractUsingS3Upload->getFilePath(), 'Test content');

Storage::disk('s3')->assertExists($customFilePath);
});

0 comments on commit 911f66f

Please sign in to comment.