|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Entity; |
| 13 | + |
| 14 | +use CodeIgniter\Database\Exceptions\DatabaseException; |
| 15 | +use CodeIgniter\I18n\Time; |
| 16 | +use CodeIgniter\Model; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use CodeIgniter\Test\DatabaseTestTrait; |
| 19 | +use Config\Services; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + * |
| 24 | + * @group DatabaseLive |
| 25 | + */ |
| 26 | +final class EntityLiveTest extends CIUnitTestCase |
| 27 | +{ |
| 28 | + use DatabaseTestTrait; |
| 29 | + |
| 30 | + protected $namespace = 'Tests\Support\Entity'; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $this->setUpMethods[] = 'setUpAddNamespace'; |
| 35 | + |
| 36 | + parent::setUp(); |
| 37 | + } |
| 38 | + |
| 39 | + protected function setUpAddNamespace(): void |
| 40 | + { |
| 41 | + Services::autoloader()->addNamespace( |
| 42 | + 'Tests\Support\Entity', |
| 43 | + SUPPORTPATH . 'Entity' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + protected function tearDown(): void |
| 48 | + { |
| 49 | + parent::tearDown(); |
| 50 | + |
| 51 | + $this->regressDatabase(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testEntityReturnsValuesWithCorrectTypes() |
| 55 | + { |
| 56 | + $entity = new class () extends Entity { |
| 57 | + protected $casts = [ |
| 58 | + 'id' => 'int', |
| 59 | + 'active' => 'int-bool', |
| 60 | + 'memo' => 'json', |
| 61 | + ]; |
| 62 | + }; |
| 63 | + $model = new class () extends Model { |
| 64 | + protected $table = 'users'; |
| 65 | + protected $allowedFields = [ |
| 66 | + 'username', 'active', 'memo', |
| 67 | + ]; |
| 68 | + protected $useTimestamps = true; |
| 69 | + }; |
| 70 | + $entity->fill(['username' => 'johnsmith', 'active' => false, 'memo' => ['foo', 'bar']]); |
| 71 | + $model->save($entity); |
| 72 | + |
| 73 | + $user = $model->asObject(get_class($entity))->find(1); |
| 74 | + |
| 75 | + $this->assertSame(1, $user->id); |
| 76 | + $this->assertSame('johnsmith', $user->username); |
| 77 | + $this->assertFalse($user->active); |
| 78 | + $this->assertSame(['foo', 'bar'], $user->memo); |
| 79 | + $this->assertInstanceOf(Time::class, $user->created_at); |
| 80 | + $this->assertInstanceOf(Time::class, $user->updated_at); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @TODO Fix the object cast handler implementation. |
| 85 | + */ |
| 86 | + public function testCastObject(): void |
| 87 | + { |
| 88 | + $this->expectException(DatabaseException::class); |
| 89 | + $this->expectExceptionMessage('Operand should contain 1 column(s)'); |
| 90 | + |
| 91 | + $entity = new class () extends Entity { |
| 92 | + protected $casts = [ |
| 93 | + 'id' => 'int', |
| 94 | + 'active' => 'int-bool', |
| 95 | + 'memo' => 'object', |
| 96 | + ]; |
| 97 | + }; |
| 98 | + $model = new class () extends Model { |
| 99 | + protected $table = 'users'; |
| 100 | + protected $allowedFields = [ |
| 101 | + 'username', 'active', 'memo', |
| 102 | + ]; |
| 103 | + protected $useTimestamps = true; |
| 104 | + }; |
| 105 | + $entity->fill(['username' => 'johnsmith', 'active' => false, 'memo' => ['foo', 'bar']]); |
| 106 | + $model->save($entity); |
| 107 | + } |
| 108 | +} |
0 commit comments