Skip to content

Commit

Permalink
test: add test for Model returns Entity with Casts
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Apr 23, 2024
1 parent 971b0da commit dde1de7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/_support/Entity/UserWithCasts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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',
];
}
29 changes: 29 additions & 0 deletions tests/_support/Models/UserEntityWithCastsModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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;
}
19 changes: 19 additions & 0 deletions tests/system/Models/FindModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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' => ['[email protected]', '[email protected]'],
'country' => 'US',
]);
$id = $this->model->insert($user, true);

$user = $this->model->find($id);

$this->assertSame('John Smith', $user->name);
$this->assertSame(['[email protected]', '[email protected]'], $user->email);
}

public function testFindReturnsMultipleRows(): void
{
$this->createModel(JobModel::class);
Expand Down

0 comments on commit dde1de7

Please sign in to comment.