diff --git a/tests/_support/Entity/UserWithCasts.php b/tests/_support/Entity/UserWithCasts.php new file mode 100644 index 000000000000..cfe1981fa3fa --- /dev/null +++ b/tests/_support/Entity/UserWithCasts.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Tests\Support\Entity; + +use CodeIgniter\Entity\Entity; + +class UserWithCasts extends Entity +{ + protected $casts = [ + 'email' => 'json', + ]; +} diff --git a/tests/_support/Models/UserEntityWithCastsModel.php b/tests/_support/Models/UserEntityWithCastsModel.php new file mode 100644 index 000000000000..2ea32c88f842 --- /dev/null +++ b/tests/_support/Models/UserEntityWithCastsModel.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Tests\Support\Models; + +use CodeIgniter\Model; +use Tests\Support\Entity\UserWithCasts; + +class UserEntityWithCastsModel extends Model +{ + protected $table = 'user'; + protected $allowedFields = [ + 'name', + 'email', + 'country', + 'deleted_at', + ]; + protected $returnType = UserWithCasts::class; +} diff --git a/tests/system/Models/FindModelTest.php b/tests/system/Models/FindModelTest.php index d27fcb791bb3..24946b4e2965 100644 --- a/tests/system/Models/FindModelTest.php +++ b/tests/system/Models/FindModelTest.php @@ -15,8 +15,10 @@ use CodeIgniter\Database\Exceptions\DataException; use CodeIgniter\Exceptions\ModelException; +use Tests\Support\Entity\UserWithCasts; use Tests\Support\Models\JobModel; use Tests\Support\Models\SecondaryModel; +use Tests\Support\Models\UserEntityWithCastsModel; use Tests\Support\Models\UserModel; /** @@ -32,6 +34,23 @@ public function testFindReturnsRow(): void $this->assertSame('Musician', $this->model->find(4)->name); } + public function testFindReturnsEntityWithCasts(): void + { + $this->createModel(UserEntityWithCastsModel::class); + $this->model->builder()->truncate(); + $user = new UserWithCasts([ + 'name' => 'John Smith', + 'email' => ['foo@example.jp', 'bar@example.com'], + 'country' => 'US', + ]); + $id = $this->model->insert($user, true); + + $user = $this->model->find($id); + + $this->assertSame('John Smith', $user->name); + $this->assertSame(['foo@example.jp', 'bar@example.com'], $user->email); + } + public function testFindReturnsMultipleRows(): void { $this->createModel(JobModel::class);