Skip to content

Commit 8f20f04

Browse files
committed
test: add Entity Live tests
1 parent 767b59b commit 8f20f04

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 Tests\Support\Entity\Database\Migrations;
13+
14+
use CodeIgniter\Database\Migration;
15+
16+
class CreateUsersTable extends Migration
17+
{
18+
public function up(): void
19+
{
20+
$this->forge->addField([
21+
'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
22+
'username' => ['type' => 'varchar', 'constraint' => 30, 'null' => true],
23+
'active' => ['type' => 'tinyint', 'constraint' => 1, 'null' => 0, 'default' => 0],
24+
'memo' => ['type' => 'text', 'null' => true],
25+
'created_at' => ['type' => 'datetime', 'null' => true],
26+
'updated_at' => ['type' => 'datetime', 'null' => true],
27+
'deleted_at' => ['type' => 'datetime', 'null' => true],
28+
]);
29+
$this->forge->addPrimaryKey('id');
30+
$this->forge->addUniqueKey('username');
31+
$this->forge->createTable('users');
32+
}
33+
34+
public function down(): void
35+
{
36+
$this->forge->dropTable('users');
37+
}
38+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

Comments
 (0)