Initial Version
This commit is contained in:
64
tests/php/shared/Json/JsonSerializableObjectTest.php
Normal file
64
tests/php/shared/Json/JsonSerializableObjectTest.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXF\Json;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for JsonSerializableObject base functionality
|
||||
*/
|
||||
class JsonSerializableObjectTest extends TestCase
|
||||
{
|
||||
public function testJsonSerializeReturnsObjectVars(): void
|
||||
{
|
||||
$testObject = new class extends JsonSerializableObject {
|
||||
public string $publicProperty = 'test';
|
||||
private string $privateProperty = 'private';
|
||||
protected string $protectedProperty = 'protected';
|
||||
};
|
||||
|
||||
$serialized = $testObject->jsonSerialize();
|
||||
|
||||
$this->assertIsArray($serialized);
|
||||
$this->assertArrayHasKey('publicProperty', $serialized);
|
||||
$this->assertEquals('test', $serialized['publicProperty']);
|
||||
$this->assertArrayHasKey('protectedProperty', $serialized); // get_object_vars includes protected
|
||||
$this->assertEquals('protected', $serialized['protectedProperty']);
|
||||
$this->assertArrayNotHasKey('privateProperty', $serialized); // but not private
|
||||
}
|
||||
|
||||
public function testJsonDeserializeSetsProperties(): void
|
||||
{
|
||||
$testObject = new class extends JsonSerializableObject {
|
||||
public string $name = '';
|
||||
public int $age = 0;
|
||||
};
|
||||
|
||||
$data = [
|
||||
'name' => 'John Doe',
|
||||
'age' => 30,
|
||||
'nonexistent' => 'ignored'
|
||||
];
|
||||
|
||||
$result = $testObject->jsonDeserialize($data);
|
||||
|
||||
$this->assertSame($testObject, $result);
|
||||
$this->assertEquals('John Doe', $testObject->name);
|
||||
$this->assertEquals(30, $testObject->age);
|
||||
$this->assertObjectNotHasProperty('nonexistent', $testObject);
|
||||
}
|
||||
|
||||
public function testJsonDeserializeHandlesJsonString(): void
|
||||
{
|
||||
$testObject = new class extends JsonSerializableObject {
|
||||
public string $message = '';
|
||||
};
|
||||
|
||||
$jsonString = '{"message": "Hello World"}';
|
||||
$testObject->jsonDeserialize($jsonString);
|
||||
|
||||
$this->assertEquals('Hello World', $testObject->message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXF\People\Entity\Individual;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for IndividualObject JsonSerializable functionality
|
||||
*/
|
||||
class IndividualObjectTest extends TestCase
|
||||
{
|
||||
public function testJsonSerialization(): void
|
||||
{
|
||||
// Create an IndividualObject instance
|
||||
$individual = new IndividualObject();
|
||||
|
||||
// Set some basic properties
|
||||
$individual->urid = 'test-urid-123';
|
||||
$individual->label = 'Test Individual';
|
||||
$individual->language = 'en';
|
||||
|
||||
// Set name
|
||||
$individual->names->First = 'John';
|
||||
$individual->names->Last = 'Doe';
|
||||
$individual->names->Prefix = 'Mr.';
|
||||
|
||||
// Add an alias
|
||||
$alias = new IndividualAliasObject();
|
||||
$alias->label = 'Johnny';
|
||||
$individual->names->Aliases[] = $alias;
|
||||
|
||||
// Serialize to JSON
|
||||
$json = json_encode($individual);
|
||||
|
||||
// Verify JSON structure
|
||||
$this->assertJson($json);
|
||||
|
||||
$data = json_decode($json, true);
|
||||
$this->assertEquals('individual', $data['type']);
|
||||
$this->assertEquals(1, $data['version']);
|
||||
$this->assertEquals('test-urid-123', $data['urid']);
|
||||
$this->assertEquals('Test Individual', $data['label']);
|
||||
$this->assertEquals('en', $data['language']);
|
||||
$this->assertEquals('John', $data['names']['First']);
|
||||
$this->assertEquals('Doe', $data['names']['Last']);
|
||||
$this->assertEquals('Mr.', $data['names']['Prefix']);
|
||||
$this->assertCount(1, $data['names']['Aliases']);
|
||||
$this->assertEquals('Johnny', $data['names']['Aliases'][0]['label']);
|
||||
}
|
||||
|
||||
public function testJsonDeserialization(): void
|
||||
{
|
||||
$jsonData = [
|
||||
'type' => 'individual',
|
||||
'version' => 1,
|
||||
'urid' => 'test-urid-456',
|
||||
'label' => 'Deserialized Individual',
|
||||
'language' => 'fr',
|
||||
'names' => [
|
||||
'First' => 'Jane',
|
||||
'Last' => 'Smith',
|
||||
'Prefix' => 'Ms.',
|
||||
'Aliases' => [
|
||||
['label' => 'Janie']
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$individual = new IndividualObject();
|
||||
$individual->jsonDeserialize($jsonData);
|
||||
|
||||
$this->assertEquals('test-urid-456', $individual->urid);
|
||||
$this->assertEquals('Deserialized Individual', $individual->label);
|
||||
$this->assertEquals('fr', $individual->language);
|
||||
$this->assertEquals('Jane', $individual->names->First);
|
||||
$this->assertEquals('Smith', $individual->names->Last);
|
||||
$this->assertEquals('Ms.', $individual->names->Prefix);
|
||||
$this->assertCount(1, $individual->names->Aliases);
|
||||
$this->assertEquals('Janie', $individual->names->Aliases[0]->label);
|
||||
}
|
||||
|
||||
public function testJsonRoundTrip(): void
|
||||
{
|
||||
// Create original object
|
||||
$original = new IndividualObject();
|
||||
$original->urid = 'round-trip-urid';
|
||||
$original->label = 'Round Trip Test';
|
||||
$original->names->First = 'Alice';
|
||||
$original->names->Last = 'Wonderland';
|
||||
|
||||
// Serialize and deserialize
|
||||
$json = json_encode($original);
|
||||
$deserialized = new IndividualObject();
|
||||
$deserialized->jsonDeserialize($json);
|
||||
|
||||
// Verify round-trip integrity
|
||||
$this->assertEquals($original->urid, $deserialized->urid);
|
||||
$this->assertEquals($original->label, $deserialized->label);
|
||||
$this->assertEquals($original->names->First, $deserialized->names->First);
|
||||
$this->assertEquals($original->names->Last, $deserialized->names->Last);
|
||||
|
||||
// Verify JSON representations are identical
|
||||
$originalJson = json_encode($original);
|
||||
$deserializedJson = json_encode($deserialized);
|
||||
$this->assertJsonStringEqualsJsonString($originalJson, $deserializedJson);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user