Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ public function update($id = null, $row = null): bool

protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
{
return parent::objectToRawArray($object, $onlyChanged);
return parent::objectToRawArray($object, $onlyChanged, $recursive);
Comment thread
michalsn marked this conversation as resolved.
Resolved
}

/**
Expand Down
129 changes: 129 additions & 0 deletions tests/system/Models/ObjectToRawArrayModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Models;

use CodeIgniter\Entity\Entity;
use CodeIgniter\Model;
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\Group;
use ReflectionMethod;
Comment thread
gr8man marked this conversation as resolved.
Resolved

/**
* @internal
*/
#[Group('Models')]
Comment thread
gr8man marked this conversation as resolved.
Resolved
final class ObjectToRawArrayModelTest extends CIUnitTestCase
{
private function createModel(): Model
{
return new class () extends Model {
public function __construct()
{
// Skip DB connection — we only test objectToRawArray
}

protected $table = 'test';
protected $allowedFields = ['name', 'nested', 'entity'];
protected $returnType = 'array';
protected $useSoftDeletes = false;
};
}

/**
* Call protected objectToRawArray via reflection.
*/
private function callObjectToRawArray(Model $model, object $object, bool $onlyChanged, bool $recursive): array

Check failure on line 46 in tests/system/Models/ObjectToRawArrayModelTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\Models\ObjectToRawArrayModelTest::callObjectToRawArray() return type has no value type specified in iterable type array.
{
$method = new ReflectionMethod(Model::class, 'objectToRawArray');

return $method->invoke($model, $object, $onlyChanged, $recursive);
Comment thread
gr8man marked this conversation as resolved.
Resolved
}

public function testObjectToRawArrayPassesRecursiveTrue(): void
{
$model = $this->createModel();

$inner = new class () extends Entity {
protected $attributes = ['name' => 'inner'];
protected $original = ['name' => 'inner'];
};

$outer = new class () extends Entity {
protected $attributes = ['name' => 'outer', 'nested' => null];
protected $original = ['name' => 'outer', 'nested' => null];
};
$outer->nested = $inner;

$result = $this->callObjectToRawArray($model, $outer, false, true);

$this->assertArrayHasKey('name', $result);
$this->assertSame('outer', $result['name']);
$this->assertArrayHasKey('nested', $result);
$this->assertIsArray($result['nested']);
$this->assertSame(['name' => 'inner'], $result['nested']);
}

public function testObjectToRawArrayPassesRecursiveFalse(): void
{
$model = $this->createModel();

$inner = new class () extends Entity {
protected $attributes = ['name' => 'inner'];
protected $original = ['name' => 'inner'];
};

$outer = new class () extends Entity {
protected $attributes = ['name' => 'outer', 'nested' => null];
protected $original = ['name' => 'outer', 'nested' => null];
};
$outer->nested = $inner;

$result = $this->callObjectToRawArray($model, $outer, false, false);

$this->assertArrayHasKey('name', $result);
$this->assertSame('outer', $result['name']);
$this->assertArrayHasKey('nested', $result);
// With recursive=false, nested Entity should remain as object
$this->assertInstanceOf(Entity::class, $result['nested']);
}

public function testObjectToRawArrayNonEntity(): void
{
$model = $this->createModel();

$obj = new class () {
public string $name = 'test';
public string $value = '123';
};

$result = $this->callObjectToRawArray($model, $obj, false, false);

$this->assertSame(['name' => 'test', 'value' => '123'], $result);
}

public function testObjectToRawArrayOnlyChanged(): void
{
$model = $this->createModel();

$entity = new class () extends Entity {
protected $attributes = ['name' => 'original', 'value' => 'keep'];
protected $original = ['name' => 'original', 'value' => 'keep'];
};
$entity->name = 'modified';

$result = $this->callObjectToRawArray($model, $entity, true, false);

$this->assertSame(['name' => 'modified'], $result);
}
}
Loading