20250714 1.1.3

This commit is contained in:
2025-07-14 16:09:49 +03:00
parent e342c7f41f
commit 2c3ea12fc1
5 changed files with 166 additions and 5 deletions

View File

@@ -228,7 +228,7 @@ class ObjectArrayTest extends TestCase
$this->assertSame(['c', 'b'], $sr);
}
public function testToArray ()
public function testAsArray ()
{
$this->PrepareForTest();
@@ -240,7 +240,7 @@ class ObjectArrayTest extends TestCase
$a_Array = new ObjectArray($array);
$sr = $a_Array->ToArray();
$sr = $a_Array->AsArray();
$this->assertIsArray($sr);
$this->assertSame($array, $sr);
@@ -550,4 +550,55 @@ class ObjectArrayTest extends TestCase
$this->assertEquals('test_string_C3',
$objectArray3->GetRow(fn (D $value) => $value->stringD == 'test_string3')->c->stringC);
}
public function testToArray ()
{
$this->PrepareForTest();
// Создаём тестовые классы
$class1 = new D ('test_string1', 12345, true, new A("test_string_A1", 6789, false),
new B("test_string_B1", 9876, "false"), new C("test_string_C1", 54321, true));
$class2 = new D ('test_string2', 123456, false, new A("test_string_A1", 678910, true),
new B("test_string_B2", 98765, "true"), new C("test_string_C2", 543210, false));
$class = new D ('test_string3', 123450, true, new A("test_string_A2", 67890, false),
new B("test_string_B3", 90876, "false"), new C("test_string_C3", 543201, true));
// Создаём массив объектов
$objectArray = new ObjectArray([$class1, $class2, $class]);
// Сериализуем
$asArray = $objectArray->ToArray();
// Сохраняем в файл
file_put_contents(__DIR__ . 'serialized4.txt',
json_encode($asArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// Проверяем, что всё хорошо
$this->assertNotEmpty($asArray);
}
public function testFromArray ()
{
$this->PrepareForTest();
// Загружаем данные
$serialized = file_get_contents(__DIR__ . 'serialized4.txt');
// Десериализуем
$array = json_decode($serialized, true, flags: JSON_UNESCAPED_UNICODE);
// Создаём массив объектов
$objectArray = new ObjectArray();
// Получаем из массива
$objectArray->FromArray($array);
// Проверяем, что всё хорошо
$this->assertEquals('test_string_A1',
$objectArray->GetRow(fn (D $value) => $value->stringD == 'test_string1')->a->a);
$this->assertEquals('test_string_B2',
$objectArray->GetRow(fn (D $value) => $value->stringD == 'test_string2')->b->a);
$this->assertEquals('test_string_C3',
$objectArray->GetRow(fn (D $value) => $value->stringD == 'test_string3')->c->stringC);
}
}