20250713 1.1.1

This commit is contained in:
2025-07-13 12:23:08 +03:00
parent 117deab9d8
commit 62762374e0
14 changed files with 386 additions and 41 deletions

View File

@@ -3,8 +3,14 @@
namespace goodboyalex\php_components_pack\tests\classes;
use goodboyalex\php_components_pack\classes\ObjectArray;
use goodboyalex\php_components_pack\enums\ObjectArraySerializeMethod;
use goodboyalex\php_components_pack\models\ObjectArraySerializeOptions;
use goodboyalex\php_components_pack\tests\data\A;
use goodboyalex\php_components_pack\tests\data\B;
use goodboyalex\php_components_pack\tests\data\C;
use goodboyalex\php_components_pack\tests\data\D;
use PHPUnit\Framework\TestCase;
use stdClass;
class ObjectArrayTest extends TestCase
{
@@ -38,6 +44,8 @@ class ObjectArrayTest extends TestCase
{
require_once __DIR__ . '/../data/A.php';
require_once __DIR__ . '/../data/B.php';
require_once __DIR__ . '/../data/C.php';
require_once __DIR__ . '/../data/D.php';
require_once __DIR__ . '/../../sources/interfaces/ISerializable.php';
require_once __DIR__ . '/../../sources/traits/ArrayBasicTrait.php';
require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayConstantsTrait.php';
@@ -480,4 +488,66 @@ class ObjectArrayTest extends TestCase
$this->assertEquals(12, $a_Array->Count());
}
public function testSerializeEx ()
{
$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]);
// Сериализуем
$serialized1 =
$objectArray->SerializeEx(new ObjectArraySerializeOptions(ObjectArraySerializeMethod::Serialize));
$serialized2 =
$objectArray->SerializeEx(new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncode));
$serialized3 =
$objectArray->SerializeEx(new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncodeWithToArray,
D::CLOSURE_TO_ARRAY()));
// Сохраняем в файл
file_put_contents(__DIR__ . 'serialized1.txt', $serialized1);
file_put_contents(__DIR__ . 'serialized2.txt', $serialized2);
file_put_contents(__DIR__ . 'serialized3.txt', $serialized3);
// Проверяем, что всё хорошо
$this->assertNotEmpty($serialized1);
$this->assertNotEmpty($serialized2);
$this->assertNotEmpty($serialized3);
}
public function testUnSerializeEx ()
{
$this->PrepareForTest();
// Загружаем данные
$serialized1 = file_get_contents(__DIR__ . 'serialized1.txt');
$serialized2 = file_get_contents(__DIR__ . 'serialized2.txt');
$serialized3 = file_get_contents(__DIR__ . 'serialized3.txt');
// Десериализуем
$objectArray1 = ObjectArray::UnSerializeEx($serialized1,
new ObjectArraySerializeOptions(ObjectArraySerializeMethod::Serialize));
$objectArray2 = ObjectArray::UnSerializeEx($serialized2,
new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncode));
$objectArray3 = ObjectArray::UnSerializeEx($serialized3,
new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncodeWithToArray,
onClassFrom: D::CLOSURE_FROM_ARRAY()));
// Проверяем, что всё хорошо
$this->assertEquals('test_string_A1',
$objectArray1->GetRow(fn (D $value) => $value->stringD == 'test_string1')->a->a);
$this->assertEquals('test_string_B2',
$objectArray2->GetRow(fn (stdClass $value) => $value->stringD == 'test_string2')->b->a);
$this->assertEquals('test_string_C3',
$objectArray3->GetRow(fn (D $value) => $value->stringD == 'test_string3')->c->stringC);
}
}