toBe('my-fancy-file.PNG') ->and($sanitized)->toBe('inv-lid-name.jpg'); }); it('extracts href/src/url files from html', function (): void { // GIVEN $html = <<<'HTML'
A
HTML; // WHEN $files = FileStorageHelper::findFilesInHtml($html); // THEN expect($files)->toHaveCount(3) ->and($files[0])->toContain('file-a.pdf') ->and($files[1])->toContain('file-b.png') ->and($files[2])->toContain('file-c.jpg'); }); it('filters uploaded files that exist on public disk', function (): void { // GIVEN Storage::fake('public'); Storage::disk('public')->put('modelfiles/orders/26/03/04/aa/file-1.png', 'ok'); $urls = [ '/storage/modelfiles/orders/26/03/04/aa/file-1.png', '/storage/modelfiles/orders/26/03/04/aa/file-missing.png', 'https://other.test/storage/modelfiles/orders/26/03/04/aa/file-1.png', '/uploads/file.jpg', ]; // WHEN $filtered = FileStorageHelper::onlyUploadedFilesWithFileStorageHelper($urls); // THEN expect($filtered)->toHaveCount(2) ->and($filtered)->toContain('/storage/modelfiles/orders/26/03/04/aa/file-1.png') ->and($filtered)->toContain('https://other.test/storage/modelfiles/orders/26/03/04/aa/file-1.png'); }); it('removes old files that are no longer referenced', function (): void { // GIVEN Storage::fake('public'); $keptPath = 'modelfiles/orders/26/03/04/aa/keep.png'; $removedPath = 'modelfiles/orders/26/03/04/aa/remove.png'; Storage::disk('public')->put($keptPath, 'keep'); Storage::disk('public')->put($removedPath, 'remove'); $oldFiles = [ '/storage/' . $keptPath, '/storage/' . $removedPath, ]; $newFiles = [ '/storage/' . $keptPath, ]; // WHEN FileStorageHelper::removeOldFiles($oldFiles, $newFiles); // THEN expect(Storage::disk('public')->exists($keptPath))->toBeTrue() ->and(Storage::disk('public')->exists($removedPath))->toBeFalse(); }); it('returns string input as-is when storing file with string path', function (): void { // GIVEN /** @var Model $model */ $model = new Product(); $existingPath = '/storage/modelfiles/orders/26/03/04/ab/existing.pdf'; // WHEN $stored = FileStorageHelper::storeFile($model, $existingPath); // THEN expect($stored)->toBe($existingPath); }); it('removes file from model attribute when prefixed by storage', function (): void { // GIVEN Storage::fake('public'); $path = 'modelfiles/orders/26/03/04/aa/model-file.png'; Storage::disk('public')->put($path, 'file-content'); $product = new Product(); $product->picture = '/storage/' . $path; // WHEN FileStorageHelper::removeFile($product, 'picture'); // THEN expect(Storage::disk('public')->exists($path))->toBeFalse(); }); it('removes old model file only when attribute changed', function (): void { // GIVEN Storage::fake('public'); $oldPath = 'modelfiles/orders/26/03/04/aa/old.png'; $newPath = 'modelfiles/orders/26/03/04/aa/new.png'; Storage::disk('public')->put($oldPath, 'old-content'); $product = new Product(); $product->setRawAttributes([ 'picture' => '/storage/' . $oldPath, ], true); $product->picture = '/storage/' . $newPath; // WHEN FileStorageHelper::removeOldFile($product, 'picture'); // THEN expect(Storage::disk('public')->exists($oldPath))->toBeFalse(); });