This repository was archived by the owner on Mar 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change @@ -14,8 +14,10 @@ class AttributeMappingTest extends TestCase
1414 public bool $ test ;
1515 public string $ name ;
1616 public object $ files ;
17- public ?\Closure $ default = null ;
18- public ?Collection $ collection = null ;
17+
18+ public ?\Closure $ default = null ;
19+ public ?Collection $ collection = null ;
20+ public ?string $ intAsString = null ;
1921
2022 /** @test */
2123 public function testMapsAttributesToClassPropertiesCorrectly ()
@@ -68,4 +70,21 @@ public function testMappingWorksWithCollections()
6870
6971 $ this ->assertInstanceOf (Collection::class, $ this ->collection );
7072 }
73+
74+ /** @test */
75+ public function testMappingWithCasts ()
76+ {
77+ $ model = new TestModel ([
78+ 'collection ' => [
79+ 0 => true ,
80+ 1 => false ,
81+ ],
82+ 'intAsString ' => 100 ,
83+ ]);
84+
85+ $ this ->mapModelAttributes ($ model );
86+
87+ $ this ->assertInstanceOf (Collection::class, $ this ->collection );
88+ $ this ->assertIsString ($ this ->intAsString );
89+ }
7190}
Original file line number Diff line number Diff line change 22
33namespace MichaelRubel \ModelMapper \Tests \Boilerplate ;
44
5+ use Illuminate \Database \Eloquent \Casts \AsCollection ;
56use Illuminate \Database \Eloquent \Model ;
67
78class TestModel extends Model
@@ -10,4 +11,12 @@ class TestModel extends Model
1011 * @var array
1112 */
1213 protected $ guarded = [];
14+
15+ /**
16+ * @var string[]
17+ */
18+ protected $ casts = [
19+ 'collection ' => AsCollection::class,
20+ 'intAsString ' => 'string ' ,
21+ ];
1322}
You can’t perform that action at this time.
0 commit comments