Skip to content

Commit 4ed4743

Browse files
committed
test: add test for Model and Entity with cast primaryKey
1 parent bcc9b54 commit 4ed4743

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Cast;
13+
14+
use CodeIgniter\Entity\Cast\BaseCast;
15+
16+
class CastUUID extends BaseCast
17+
{
18+
/**
19+
* Get
20+
*
21+
* @param string $binary Binary UUID
22+
*
23+
* @return string String UUID
24+
*/
25+
public static function get($binary, array $params = []): string
26+
{
27+
$string = unpack('h*', $binary);
28+
29+
return preg_replace('/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/', '$1-$2-$3-$4-$5', $string[1]);
30+
}
31+
32+
/**
33+
* Set
34+
*
35+
* @param string $string String UUID
36+
*
37+
* @return string Binary UUID
38+
*/
39+
public static function set($string, array $params = []): string
40+
{
41+
return pack('h*', str_replace('-', '', $string));
42+
}
43+
}

tests/_support/Entity/UUID.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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;
13+
14+
use CodeIgniter\Entity\Entity;
15+
use Tests\Support\Entity\Cast\CastUUID;
16+
17+
class UUID extends Entity
18+
{
19+
protected $casts = [
20+
'id' => 'uuid',
21+
];
22+
protected $castHandlers = [
23+
'uuid' => CastUUID::class,
24+
];
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Models;
13+
14+
use CodeIgniter\Model;
15+
use Tests\Support\Entity\UUID;
16+
17+
class UUIDPkeyModel extends Model
18+
{
19+
protected $table = 'uuid';
20+
protected $useAutoIncrement = false;
21+
protected $returnType = UUID::class;
22+
protected $allowedFields = [
23+
'value',
24+
];
25+
}

tests/system/Models/UpdateModelTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
use CodeIgniter\Database\Exceptions\DatabaseException;
1515
use CodeIgniter\Database\Exceptions\DataException;
1616
use CodeIgniter\Entity\Entity;
17+
use Config\Database;
1718
use InvalidArgumentException;
1819
use stdClass;
20+
use Tests\Support\Entity\UUID;
1921
use Tests\Support\Models\EventModel;
2022
use Tests\Support\Models\JobModel;
2123
use Tests\Support\Models\SecondaryModel;
2224
use Tests\Support\Models\UserModel;
25+
use Tests\Support\Models\UUIDPkeyModel;
2326
use Tests\Support\Models\ValidModel;
2427
use Tests\Support\Models\WithoutAutoIncrementModel;
2528

@@ -375,6 +378,39 @@ public function testUpdateWithEntityNoAllowedFields(): void
375378
$this->model->update($id, $entity);
376379
}
377380

381+
public function testUpdateEntityWithPrimaryKeyCast(): void
382+
{
383+
$this->createUuidTable();
384+
385+
$this->createModel(UUIDPkeyModel::class);
386+
387+
$entity = new UUID();
388+
$entity->id = '550e8400-e29b-41d4-a716-446655440000';
389+
$entity->value = 'test1';
390+
391+
$id = $this->model->insert($entity);
392+
$entity = $this->model->find($id);
393+
394+
$entity->value = 'id';
395+
$result = $this->model->save($entity);
396+
397+
$this->assertTrue($result);
398+
399+
$entity = $this->model->find($id);
400+
401+
$this->assertSame('id', $entity->value);
402+
}
403+
404+
private function createUuidTable(): void
405+
{
406+
$forge = Database::forge($this->DBGroup);
407+
$forge->dropTable('uuid');
408+
$forge->addField([
409+
'id' => ['type' => 'BINARY', 'constraint' => 16],
410+
'value' => ['type' => 'VARCHAR', 'constraint' => 400, 'null' => true],
411+
])->addKey('id', true)->createTable('uuid', true);
412+
}
413+
378414
public function testUseAutoIncrementSetToFalseUpdate(): void
379415
{
380416
$key = 'key';

0 commit comments

Comments
 (0)