|
| 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 CodeIgniter\Models; |
| 15 | + |
| 16 | +use CodeIgniter\Entity\Entity; |
| 17 | +use CodeIgniter\Model; |
| 18 | +use CodeIgniter\Test\CIUnitTestCase; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | +use ReflectionMethod; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('Models')] |
| 26 | +final class ObjectToRawArrayModelTest extends CIUnitTestCase |
| 27 | +{ |
| 28 | + private function createModel(): Model |
| 29 | + { |
| 30 | + return new class () extends Model { |
| 31 | + public function __construct() |
| 32 | + { |
| 33 | + // Skip DB connection — we only test objectToRawArray |
| 34 | + } |
| 35 | + |
| 36 | + protected $table = 'test'; |
| 37 | + protected $allowedFields = ['name', 'nested', 'entity']; |
| 38 | + protected $returnType = 'array'; |
| 39 | + protected $useSoftDeletes = false; |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Call protected objectToRawArray via reflection. |
| 45 | + */ |
| 46 | + private function callObjectToRawArray(Model $model, object $object, bool $onlyChanged, bool $recursive): array |
| 47 | + { |
| 48 | + $method = new ReflectionMethod(Model::class, 'objectToRawArray'); |
| 49 | + |
| 50 | + return $method->invoke($model, $object, $onlyChanged, $recursive); |
| 51 | + } |
| 52 | + |
| 53 | + public function testObjectToRawArrayPassesRecursiveTrue(): void |
| 54 | + { |
| 55 | + $model = $this->createModel(); |
| 56 | + |
| 57 | + $inner = new class () extends Entity { |
| 58 | + protected $attributes = ['name' => 'inner']; |
| 59 | + protected $original = ['name' => 'inner']; |
| 60 | + }; |
| 61 | + |
| 62 | + $outer = new class () extends Entity { |
| 63 | + protected $attributes = ['name' => 'outer', 'nested' => null]; |
| 64 | + protected $original = ['name' => 'outer', 'nested' => null]; |
| 65 | + }; |
| 66 | + $outer->nested = $inner; |
| 67 | + |
| 68 | + $result = $this->callObjectToRawArray($model, $outer, false, true); |
| 69 | + |
| 70 | + $this->assertArrayHasKey('name', $result); |
| 71 | + $this->assertSame('outer', $result['name']); |
| 72 | + $this->assertArrayHasKey('nested', $result); |
| 73 | + $this->assertIsArray($result['nested']); |
| 74 | + $this->assertSame(['name' => 'inner'], $result['nested']); |
| 75 | + } |
| 76 | + |
| 77 | + public function testObjectToRawArrayPassesRecursiveFalse(): void |
| 78 | + { |
| 79 | + $model = $this->createModel(); |
| 80 | + |
| 81 | + $inner = new class () extends Entity { |
| 82 | + protected $attributes = ['name' => 'inner']; |
| 83 | + protected $original = ['name' => 'inner']; |
| 84 | + }; |
| 85 | + |
| 86 | + $outer = new class () extends Entity { |
| 87 | + protected $attributes = ['name' => 'outer', 'nested' => null]; |
| 88 | + protected $original = ['name' => 'outer', 'nested' => null]; |
| 89 | + }; |
| 90 | + $outer->nested = $inner; |
| 91 | + |
| 92 | + $result = $this->callObjectToRawArray($model, $outer, false, false); |
| 93 | + |
| 94 | + $this->assertArrayHasKey('name', $result); |
| 95 | + $this->assertSame('outer', $result['name']); |
| 96 | + $this->assertArrayHasKey('nested', $result); |
| 97 | + // With recursive=false, nested Entity should remain as object |
| 98 | + $this->assertInstanceOf(Entity::class, $result['nested']); |
| 99 | + } |
| 100 | + |
| 101 | + public function testObjectToRawArrayNonEntity(): void |
| 102 | + { |
| 103 | + $model = $this->createModel(); |
| 104 | + |
| 105 | + $obj = new class () { |
| 106 | + public string $name = 'test'; |
| 107 | + public string $value = '123'; |
| 108 | + }; |
| 109 | + |
| 110 | + $result = $this->callObjectToRawArray($model, $obj, false, false); |
| 111 | + |
| 112 | + $this->assertSame(['name' => 'test', 'value' => '123'], $result); |
| 113 | + } |
| 114 | + |
| 115 | + public function testObjectToRawArrayOnlyChanged(): void |
| 116 | + { |
| 117 | + $model = $this->createModel(); |
| 118 | + |
| 119 | + $entity = new class () extends Entity { |
| 120 | + protected $attributes = ['name' => 'original', 'value' => 'keep']; |
| 121 | + protected $original = ['name' => 'original', 'value' => 'keep']; |
| 122 | + }; |
| 123 | + $entity->name = 'modified'; |
| 124 | + |
| 125 | + $result = $this->callObjectToRawArray($model, $entity, true, false); |
| 126 | + |
| 127 | + $this->assertSame(['name' => 'modified'], $result); |
| 128 | + } |
| 129 | +} |
0 commit comments