-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8823 from kenjis/test-model-returns-entity-with-c…
…asts test: add test for Model returns Entity with Casts
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' => ['[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); | ||
|