|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Assets; |
| 4 | + |
| 5 | +use Illuminate\Http\UploadedFile; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Statamic\Facades\AssetContainer; |
| 8 | +use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +class AssetContainerContentsTest extends TestCase |
| 12 | +{ |
| 13 | + use PreventsSavingStacheItemsToDisk; |
| 14 | + |
| 15 | + public function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + |
| 19 | + config(['filesystems.disks.test' => [ |
| 20 | + 'driver' => 'local', |
| 21 | + 'root' => __DIR__.'/tmp', |
| 22 | + ]]); |
| 23 | + } |
| 24 | + |
| 25 | + public function tearDown(): void |
| 26 | + { |
| 27 | + app('files')->deleteDirectory(__DIR__.'/tmp'); |
| 28 | + |
| 29 | + parent::tearDown(); |
| 30 | + } |
| 31 | + |
| 32 | + #[Test] |
| 33 | + public function it_gets_a_folder_listing() |
| 34 | + { |
| 35 | + $container = tap(AssetContainer::make('test')->disk('test'))->save(); |
| 36 | + $container->makeAsset('one/one.txt')->upload(UploadedFile::fake()->create('one.txt')); |
| 37 | + $container->makeAsset('two/two.txt')->upload(UploadedFile::fake()->create('two.txt')); |
| 38 | + |
| 39 | + $this->assertSame([ |
| 40 | + [ |
| 41 | + 'path' => 'one', |
| 42 | + 'type' => 'dir', |
| 43 | + ], |
| 44 | + [ |
| 45 | + 'path' => 'two', |
| 46 | + 'type' => 'dir', |
| 47 | + ], |
| 48 | + ], $container->contents()->directories()->all()); |
| 49 | + } |
| 50 | + |
| 51 | + #[Test] |
| 52 | + public function it_adds_to_a_folder_listing() |
| 53 | + { |
| 54 | + $container = tap(AssetContainer::make('test')->disk('test'))->save(); |
| 55 | + $container->makeAsset('one/one.txt')->upload(UploadedFile::fake()->create('one.txt')); |
| 56 | + $container->makeAsset('two/two.txt')->upload(UploadedFile::fake()->create('two.txt')); |
| 57 | + |
| 58 | + $this->assertCount(2, $container->contents()->directories()->all()); |
| 59 | + |
| 60 | + $container->contents()->add('three'); |
| 61 | + |
| 62 | + $this->assertCount(3, $container->contents()->directories()->all()); |
| 63 | + } |
| 64 | + |
| 65 | + #[Test] |
| 66 | + public function it_forgets_a_folder_listing() |
| 67 | + { |
| 68 | + $container = tap(AssetContainer::make('test')->disk('test'))->save(); |
| 69 | + $container->makeAsset('one/one.txt')->upload(UploadedFile::fake()->create('one.txt')); |
| 70 | + $container->makeAsset('two/two.txt')->upload(UploadedFile::fake()->create('two.txt')); |
| 71 | + |
| 72 | + $this->assertCount(2, $container->contents()->directories()->all()); |
| 73 | + |
| 74 | + $container->contents()->forget('one'); |
| 75 | + |
| 76 | + $this->assertCount(1, $container->contents()->directories()->all()); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + public function it_creates_parent_folders_where_they_dont_exist() |
| 81 | + { |
| 82 | + $container = tap(AssetContainer::make('test')->disk('test'))->save(); |
| 83 | + $container->makeAsset('one/two/three/file.txt')->upload(UploadedFile::fake()->create('one.txt')); |
| 84 | + |
| 85 | + $this->assertCount(3, $container->contents()->filteredDirectoriesIn('', true)); |
| 86 | + } |
| 87 | +} |
0 commit comments