Skip to content

Commit abc4ad4

Browse files
committed
fix: Add tests for the model
1 parent 1cc41c3 commit abc4ad4

3 files changed

Lines changed: 187 additions & 0 deletions

File tree

‎tests/_support/Entity/Member.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Entity;
15+
16+
use CodeIgniter\Entity\Entity;
17+
18+
class Member extends Entity
19+
{
20+
protected $attributes = [
21+
'id' => 'unknown',
22+
'name' => 'unknown',
23+
'email' => 'unknown',
24+
'country' => 'unknown',
25+
'created_at' => 'unknown',
26+
'updated_at' => 'unknown',
27+
'deleted_at' => 'unknown',
28+
29+
// The property is not stored in the database
30+
'profile' => 'unknown',
31+
];
32+
33+
public function __construct(?array $data = null)
34+
{
35+
parent::__construct($data);
36+
37+
$this->attributes['profile'] = 'profile';
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Entity;
15+
16+
class MemberStrict extends Member
17+
{
18+
protected bool $mergeAttributesOnInject = true;
19+
}

‎tests/system/Models/UpdateModelTest.php‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use PHPUnit\Framework\Attributes\DataProvider;
2323
use PHPUnit\Framework\Attributes\Group;
2424
use stdClass;
25+
use Tests\Support\Entity\Member;
26+
use Tests\Support\Entity\MemberStrict;
2527
use Tests\Support\Entity\User;
2628
use Tests\Support\Entity\UUID;
2729
use Tests\Support\Models\EventModel;
@@ -680,6 +682,133 @@ public function testUpdateEntityUpdateOnlyChangedFalse(): void
680682
$this->assertNotSame($updateAtBefore, $updateAtAfter);
681683
}
682684

685+
/**
686+
* @param array<string, mixed> $selectedMember
687+
* @param array<string, mixed> $updatedMember
688+
*/
689+
#[DataProvider('provideUpdateOnlyChangedAndMergeAttributesOnInjectFlags')]
690+
public function testUpdateOnlyChangedAndMergeAttributesOnInjectFlags(UserModel $model, array $selectedMember, array $updatedMember): void
691+
{
692+
$member = $model->select('`id`, `name`, \'new property\' as `virtual`')->withDeleted()->find(1);
693+
$this->assertInstanceOf(Member::class, $member);
694+
$this->assertSame($selectedMember, $member->toRawArray());
695+
696+
$this->assertSame('new property', $member->virtual);
697+
698+
$member->name = 'Truman Burbank';
699+
$result = $model->update(1, $member);
700+
701+
$this->assertTrue($result);
702+
703+
$memberSilentOverwrite = $model->withDeleted()->find(1);
704+
705+
$this->assertSame($updatedMember, $memberSilentOverwrite->toRawArray());
706+
}
707+
708+
/**
709+
* @return iterable<string, array{0: UserModel, 1: array<string, mixed>, 2: array<string, mixed>}>
710+
*/
711+
public static function provideUpdateOnlyChangedAndMergeAttributesOnInjectFlags(): iterable
712+
{
713+
yield 'updateOnlyChanged=true and mergeAttributesOnInject=false' => [
714+
new class () extends UserModel {
715+
protected $returnType = Member::class;
716+
protected bool $updateOnlyChanged = true;
717+
},
718+
[
719+
'id' => 1,
720+
'name' => 'Derek Jones',
721+
'virtual' => 'new property',
722+
],
723+
[
724+
'id' => 1,
725+
'name' => 'Truman Burbank',
726+
'email' => 'derek@world.com',
727+
'country' => 'US',
728+
'created_at' => null,
729+
'updated_at' => null,
730+
'deleted_at' => null,
731+
],
732+
];
733+
734+
yield 'updateOnlyChanged=true and mergeAttributesOnInject=true' => [
735+
new class () extends UserModel {
736+
protected $returnType = MemberStrict::class;
737+
protected bool $updateOnlyChanged = true;
738+
},
739+
[
740+
'id' => 1,
741+
'name' => 'Derek Jones',
742+
'email' => 'unknown',
743+
'country' => 'unknown',
744+
'created_at' => 'unknown',
745+
'updated_at' => 'unknown',
746+
'deleted_at' => 'unknown',
747+
'profile' => 'profile',
748+
'virtual' => 'new property',
749+
],
750+
[
751+
'id' => 1,
752+
'name' => 'Truman Burbank',
753+
'email' => 'derek@world.com',
754+
'country' => 'US',
755+
'created_at' => null,
756+
'updated_at' => null,
757+
'deleted_at' => null,
758+
'profile' => 'profile',
759+
],
760+
];
761+
762+
yield 'updateOnlyChanged=false and mergeAttributesOnInject=false' => [
763+
new class () extends UserModel {
764+
protected $returnType = Member::class;
765+
protected bool $updateOnlyChanged = false;
766+
},
767+
[
768+
'id' => 1,
769+
'name' => 'Derek Jones',
770+
'virtual' => 'new property',
771+
],
772+
[
773+
'id' => 1,
774+
'name' => 'Truman Burbank',
775+
'email' => 'derek@world.com',
776+
'country' => 'US',
777+
'created_at' => null,
778+
'updated_at' => null,
779+
'deleted_at' => null,
780+
],
781+
];
782+
783+
yield 'updateOnlyChanged=false and mergeAttributesOnInject=true' => [
784+
new class () extends UserModel {
785+
protected $returnType = MemberStrict::class;
786+
protected bool $updateOnlyChanged = false;
787+
},
788+
[
789+
'id' => 1,
790+
'name' => 'Derek Jones',
791+
'email' => 'unknown',
792+
'country' => 'unknown',
793+
'created_at' => 'unknown',
794+
'updated_at' => 'unknown',
795+
'deleted_at' => 'unknown',
796+
'profile' => 'profile',
797+
'virtual' => 'new property',
798+
],
799+
[
800+
'id' => 1,
801+
'name' => 'Truman Burbank',
802+
'email' => 'unknown',
803+
'country' => 'unknown',
804+
'created_at' => null,
805+
'updated_at' => null,
806+
'deleted_at' => 'unknown',
807+
'profile' => 'profile',
808+
],
809+
];
810+
}
811+
683812
public function testUpdateBatchWithCasts(): void
684813
{
685814
$this->createModel(UserCastsTimestampModel::class);

0 commit comments

Comments
 (0)