@@ -58,6 +58,8 @@ protected function setUp(): void
5858
5959 Storage::disk ('test ' )->put ('f.jpg ' , '' );
6060 Facades \Asset::make ()->container ('test ' )->path ('f.jpg ' )->save ();
61+
62+ Storage::disk ('test ' )->put ('test-folder/test.jpg ' , '' );
6163 }
6264
6365 #[Test]
@@ -70,7 +72,7 @@ public function it_loads_from_asset_model()
7072 'basename ' => 'test.jpg ' ,
7173 'filename ' => 'test ' ,
7274 'extension ' => 'jpg ' ,
73- 'meta ' => ['width ' => 100 , 'height ' => 100 , 'data ' => []],
75+ 'meta ' => ['width ' => 100 , 'height ' => 100 , 'data ' => [' focus ' => ' 50-50-1 ' ]],
7476 ]);
7577
7678 $ asset = (new Asset )->fromModel ($ model );
@@ -81,7 +83,33 @@ public function it_loads_from_asset_model()
8183 $ this ->assertSame ('test.jpg ' , $ asset ->basename ());
8284 $ this ->assertSame ('test ' , $ asset ->filename ());
8385 $ this ->assertSame ('jpg ' , $ asset ->extension ());
84- $ this ->assertSame (['width ' => 100 , 'height ' => 100 , 'data ' => []], $ asset ->meta ());
86+ $ this ->assertSame (['width ' => 100 , 'height ' => 100 , 'data ' => ['focus ' => '50-50-1 ' ]], $ asset ->meta ());
87+ }
88+
89+ #[Test]
90+ public function it_loads_from_an_existing_model_outside_the_query_builder ()
91+ {
92+ $ model = new AssetModel ([
93+ 'container ' => 'test ' ,
94+ 'path ' => 'test-folder/test.jpg ' ,
95+ 'folder ' => 'test-folder ' ,
96+ 'basename ' => 'test.jpg ' ,
97+ 'filename ' => 'test ' ,
98+ 'extension ' => 'jpg ' ,
99+ 'meta ' => ['width ' => 100 , 'height ' => 100 , 'data ' => ['focus ' => '50-50-1 ' ]],
100+ ]);
101+
102+ $ model ->save ();
103+
104+ $ asset = $ this ->container ->asset ($ model ->path );
105+
106+ $ this ->assertSame ($ model ->getKey (), $ asset ->model ()->getKey ());
107+ $ this ->assertSame ('test-folder/test.jpg ' , $ asset ->path ());
108+ $ this ->assertSame ('test-folder ' , $ asset ->folder ());
109+ $ this ->assertSame ('test.jpg ' , $ asset ->basename ());
110+ $ this ->assertSame ('test ' , $ asset ->filename ());
111+ $ this ->assertSame ('jpg ' , $ asset ->extension ());
112+ $ this ->assertSame (['width ' => 100 , 'height ' => 100 , 'data ' => ['focus ' => '50-50-1 ' ]], $ asset ->meta ());
85113 }
86114
87115 #[Test]
@@ -216,11 +244,13 @@ public function can_save_an_asset_made_on_the_container()
216244
217245 $ asset = $ this ->container ->makeAsset ('a.jpg ' );
218246
219- $ this ->assertNull ($ asset ->model ());
247+ $ model = $ asset ->model (); // it should find the existing model meta
248+ $ this ->assertNotNull ($ model );
220249
221250 $ asset ->save ();
222251
223252 $ this ->assertNotNull ($ asset ->model ());
253+ $ this ->assertSame ($ model , $ asset ->model ());
224254
225255 Event::assertDispatched (AssetSaved::class, fn ($ event ) => $ event ->asset === $ asset );
226256
0 commit comments