|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Entity; |
15 | 15 |
|
| 16 | +use ArrayIterator; |
| 17 | +use ArrayObject; |
16 | 18 | use Closure; |
17 | 19 | use CodeIgniter\Entity\Exceptions\CastException; |
18 | 20 | use CodeIgniter\HTTP\URI; |
@@ -2198,4 +2200,48 @@ public function testHasChangedWithDateTimeInterface(): void |
2198 | 2200 | $entity->created_at = new DateTime('2024-01-01 12:00:00', new DateTimeZone('America/New_York')); |
2199 | 2201 | $this->assertTrue($entity->hasChanged('created_at')); |
2200 | 2202 | } |
| 2203 | + |
| 2204 | + public function testHasChangedWithTraversable(): void |
| 2205 | + { |
| 2206 | + $entity = new class () extends Entity { |
| 2207 | + protected $attributes = [ |
| 2208 | + 'items' => null, |
| 2209 | + ]; |
| 2210 | + }; |
| 2211 | + |
| 2212 | + // Test with ArrayObject |
| 2213 | + $entity->items = new ArrayObject(['a', 'b', 'c']); |
| 2214 | + $entity->syncOriginal(); |
| 2215 | + |
| 2216 | + $this->assertFalse($entity->hasChanged('items')); |
| 2217 | + |
| 2218 | + $entity->items = new ArrayObject(['a', 'b', 'd']); |
| 2219 | + $this->assertTrue($entity->hasChanged('items')); |
| 2220 | + |
| 2221 | + $entity->syncOriginal(); |
| 2222 | + $entity->items = new ArrayObject(['a', 'b', 'd']); |
| 2223 | + $this->assertFalse($entity->hasChanged('items')); |
| 2224 | + |
| 2225 | + // Test with ArrayIterator |
| 2226 | + $entity->items = new ArrayIterator(['x', 'y', 'z']); |
| 2227 | + $entity->syncOriginal(); |
| 2228 | + $entity->items = new ArrayIterator(['x', 'y', 'modified']); |
| 2229 | + $this->assertTrue($entity->hasChanged('items')); |
| 2230 | + |
| 2231 | + // Test with nested objects inside collection (verifies recursive normalization) |
| 2232 | + $obj1 = new stdClass(); |
| 2233 | + $obj1->name = 'first'; |
| 2234 | + |
| 2235 | + $obj2 = new stdClass(); |
| 2236 | + $obj2->name = 'second'; |
| 2237 | + |
| 2238 | + $entity->items = new ArrayObject([$obj1, $obj2]); |
| 2239 | + $entity->syncOriginal(); |
| 2240 | + |
| 2241 | + $obj3 = new stdClass(); |
| 2242 | + $obj3->name = 'modified'; |
| 2243 | + |
| 2244 | + $entity->items = new ArrayObject([$obj3, $obj2]); |
| 2245 | + $this->assertTrue($entity->hasChanged('items')); |
| 2246 | + } |
2201 | 2247 | } |
0 commit comments