/
AssetRepositoryTest.php
81 lines (64 loc) · 3.2 KB
/
AssetRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Tests\Assets;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Statamic\Assets\AssetRepository;
use Statamic\Facades\Asset;
use Statamic\Facades\AssetContainer;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class AssetRepositoryTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
/** @test */
public function it_saves_the_meta_file_to_disk()
{
$disk = Storage::fake('test');
$file = UploadedFile::fake()->image('image.jpg', 30, 60); // creates a 723 byte image
Storage::disk('test')->putFileAs('foo', $file, 'image.jpg');
$realFilePath = Storage::disk('test')->getAdapter()->getPathPrefix().'foo/image.jpg';
touch($realFilePath, $timestamp = Carbon::now()->subMinutes(3)->timestamp);
$container = tap(AssetContainer::make('test')->disk('test'))->save();
$asset = $container->makeAsset('foo/image.jpg');
$disk->assertMissing('foo/.meta/image.jpg.yaml');
(new AssetRepository)->save($asset);
$disk->assertExists($path = 'foo/.meta/image.jpg.yaml');
$contents = <<<EOT
data: { }
size: 723
last_modified: $timestamp
width: 30
height: 60
mime_type: image/jpeg
duration: null
EOT;
$this->assertEquals($contents, $disk->get($path));
}
/** @test */
public function it_resolves_the_correct_disk_from_similar_names()
{
Storage::fake('disk_long', ['url' => 'test_long_url_same_beginning']);
Storage::fake('disk_short', ['url' => 'test']);
$assetRepository = new AssetRepository;
$file = UploadedFile::fake()->image('image.jpg', 30, 60); // creates a 723 byte image
Storage::disk('disk_short')->putFileAs('foo', $file, 'image_in_short.jpg');
$realFilePath = Storage::disk('disk_short')->getAdapter()->getPathPrefix().'foo/image_in_short.jpg';
touch($realFilePath, $timestamp = Carbon::now()->subMinutes(3)->timestamp);
$containerShortUrl = tap(AssetContainer::make('container_short')->disk('disk_short'))->save();
$assetShortUrl = $containerShortUrl->makeAsset('foo/image_in_short.jpg');
$assetRepository->save($assetShortUrl);
Storage::disk('disk_long')->putFileAs('foo', $file, 'image_in_long.jpg');
$realFilePath = Storage::disk('disk_long')->getAdapter()->getPathPrefix().'foo/image_in_long.jpg';
touch($realFilePath, $timestamp = Carbon::now()->subMinutes(3)->timestamp);
$containerLongUrl = tap(AssetContainer::make('container_long')->disk('disk_long'))->save();
$assetLongUrl = $containerLongUrl->makeAsset('foo/image_in_long.jpg');
$assetRepository->save($assetLongUrl);
$foundAssetShortUrl = Asset::findByUrl($assetShortUrl->url());
$this->assertInstanceOf(\Statamic\Contracts\Assets\Asset::class, $foundAssetShortUrl);
$this->assertEquals('test/foo/image_in_short.jpg', $foundAssetShortUrl->url());
$foundAssetLongUrl = Asset::findByUrl($assetLongUrl->url());
$this->assertInstanceOf(\Statamic\Contracts\Assets\Asset::class, $foundAssetLongUrl);
$this->assertEquals('test_long_url_same_beginning/foo/image_in_long.jpg', $foundAssetLongUrl->url());
}
}