diff --git a/app/Service/Import/ImportService.php b/app/Service/Import/ImportService.php index 8e371444..a63f0bac 100644 --- a/app/Service/Import/ImportService.php +++ b/app/Service/Import/ImportService.php @@ -9,8 +9,11 @@ use App\Service\Import\Importers\ImporterContract; use App\Service\Import\Importers\ImporterProvider; use App\Service\Import\Importers\ImportException; use App\Service\Import\Importers\ReportDto; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; class ImportService { @@ -22,6 +25,8 @@ class ImportService /** @var ImporterContract $importer */ $importer = app(ImporterProvider::class)->getImporter($importerType); $importer->init($organization); + Storage::disk(config('filesystems.private')) + ->put('import/'.Carbon::now()->toDateString().'-'.$organization->getKey().'-'.Str::uuid(), $data); $lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1); diff --git a/tests/Unit/Service/Import/ImportServiceTest.php b/tests/Unit/Service/Import/ImportServiceTest.php index 40ec8ce4..01040bd9 100644 --- a/tests/Unit/Service/Import/ImportServiceTest.php +++ b/tests/Unit/Service/Import/ImportServiceTest.php @@ -9,6 +9,7 @@ use App\Service\Import\Importers\ImporterProvider; use App\Service\Import\Importers\ImportException; use App\Service\Import\ImportService; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Storage; use PHPUnit\Framework\Attributes\CoversClass; @@ -23,7 +24,7 @@ class ImportServiceTest extends TestCase public function test_import_gets_importer_from_provider_runs_importer_and_returns_report(): void { // Arrange - Storage::fake(config('filesystems.default')); + Storage::fake(config('filesystems.private')); $organization = Organization::factory()->create(); $timezone = 'Europe/Vienna'; $data = Storage::disk('testfiles')->get('toggl_time_entries_import_test_1.csv'); @@ -41,13 +42,16 @@ class ImportServiceTest extends TestCase $this->assertSame(1, $report->usersCreated); $this->assertSame(2, $report->projectsCreated); $this->assertSame(1, $report->clientsCreated); - Storage::disk(config('filesystems.default'))->assertDirectoryEmpty('import'); + $storedFiles = Storage::disk(config('filesystems.private'))->files('import'); + $this->assertCount(1, $storedFiles); + $this->assertStringStartsWith('import/'.Carbon::now()->toDateString().'-'.$organization->getKey().'-', $storedFiles[0]); + $this->assertSame($data, Storage::disk(config('filesystems.private'))->get($storedFiles[0])); } public function test_import_releases_lock_if_an_exception_happens_during_the_import(): void { // Arrange - Storage::fake(config('filesystems.default')); + Storage::fake(config('filesystems.private')); $organization = Organization::factory()->create(); $timezone = 'Europe/Vienna'; $data = 'Invalid CSV data'; @@ -66,7 +70,7 @@ class ImportServiceTest extends TestCase public function test_import_throws_exception_if_import_is_already_in_progress(): void { // Arrange - Storage::fake(config('filesystems.default')); + Storage::fake(config('filesystems.private')); $organization = Organization::factory()->create(); $timezone = 'Europe/Vienna'; $data = Storage::disk('testfiles')->get('toggl_time_entries_import_test_1.csv');