Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit ab65938

Browse files
committed
add tests for casts
1 parent 5b64f60 commit ab65938

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

tests/AttributeMappingTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

tests/Boilerplate/TestModel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MichaelRubel\ModelMapper\Tests\Boilerplate;
44

5+
use Illuminate\Database\Eloquent\Casts\AsCollection;
56
use Illuminate\Database\Eloquent\Model;
67

78
class 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
}

0 commit comments

Comments
 (0)