Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions tests/_support/Enum/StateEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@

namespace Tests\Support\Enum;

/**
* An enum that also defines toArray(), used to test that UnitEnum handling
* takes precedence over toArray() in Entity::normalizeValue().
*/
enum StateEnum: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';

public function toArray(): array
{
return array_column(self::cases(), 'value');
return self::cases();
}
}
11 changes: 3 additions & 8 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,21 +1046,16 @@ public function testCastEnumSetWithUnitEnumObject(): void
$this->assertSame(ColorEnum::RED, $entity->color);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/10136
*/
public function testInjectRawDataWithEnumThatHasToArrayMethod(): void
{
// Regression test for https://github.com/codeigniter4/CodeIgniter4/issues/10136
// Enums implementing toArray() must still be handled by the UnitEnum branch in
// normalizeValue(), so hasChanged() does not incorrectly report a change after
// injectRawData() stores the same enum value.
$entity = new class () extends Entity {};

$entity->injectRawData(['state' => StateEnum::DRAFT]);

// toRawArray() returns raw attributes, so the enum object is returned as-is.
$this->assertSame(StateEnum::DRAFT, $entity->toRawArray()['state']);

// The key assertion: normalizeValue() must treat the enum as a UnitEnum
// (not call toArray() on it), so the original and current normalized forms match.
$this->assertFalse($entity->hasChanged('state'));
}

Expand Down