diff --git a/composer.json b/composer.json index 4cf88ac..58026a5 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Text/Loaders/Textract/TextractUsingS3Upload.php b/src/Text/Loaders/Textract/TextractUsingS3Upload.php index 4e4b841..679e744 100644 --- a/src/Text/Loaders/Textract/TextractUsingS3Upload.php +++ b/src/Text/Loaders/Textract/TextractUsingS3Upload.php @@ -48,6 +48,7 @@ public function getFilePath(): string return $this->defaultFilePathGenerator(); } + /** @noinspection PhpInconsistentReturnPointsInspection */ public function cleanup(string $path) { if (static::$cleanupFileUsing) { @@ -95,6 +96,5 @@ public function load(mixed $data): ?TextContent $this->cleanup($path); return new TextContent($result); - } } diff --git a/tests/Text/TextLoaderTest.php b/tests/Text/TextLoaderTest.php index 703ee1a..ce72190 100644 --- a/tests/Text/TextLoaderTest.php +++ b/tests/Text/TextLoaderTest.php @@ -1,7 +1,10 @@ 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); +});