1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
|
<?php
namespace Zumba\JsonSerializer\Test;
use Zumba\JsonSerializer\ClosureSerializer;
use Zumba\JsonSerializer\JsonSerializer;
use Zumba\JsonSerializer\Exception\JsonSerializerException;
use stdClass;
use SuperClosure\Serializer as SuperClosureSerializer;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
class JsonSerializerTest extends TestCase
{
/**
* Serializer instance
*
* @var JsonSerializer
*/
protected $serializer;
/**
* Test case setup
*
* @before
* @return void
*/
#[\PHPUnit\Framework\Attributes\Before]
public function setUpSerializer()
{
$customObjectSerializerMap['Zumba\\JsonSerializer\\Test\\SupportClasses\\MyType'] = new \Zumba\JsonSerializer\Test\SupportClasses\MyTypeSerializer();
$this->serializer = new JsonSerializer(null, $customObjectSerializerMap);
}
/**
* Test serialization of scalar values
*
* @dataProvider scalarData
* @param mixed $scalar
* @param string $jsoned
* @return void
*/
#[\PHPUnit\Framework\Attributes\DataProvider('scalarData')]
public function testSerializeScalar($scalar, $jsoned)
{
$this->assertSame($jsoned, $this->serializer->serialize($scalar));
}
/**
* Test serialization of float values with locale
*
* @return void
*/
public function testSerializeFloatLocalized()
{
$possibleLocales = ['fr_FR', 'fr_FR.utf8', 'fr', 'fra', 'French'];
$originalLocale = setlocale(LC_NUMERIC, 0);
if (!setlocale(LC_NUMERIC, $possibleLocales)) {
$this->markTestSkipped('Unable to set an i18n locale.');
}
$data = [1.0, 1.1, 0.00000000001, 1.999999999999, 223423.123456789, 1e5, 1e11];
$expected = version_compare(PHP_VERSION, '7.1', '>=') ?
'[1.0,1.1,1.0e-11,1.999999999999,223423.123456789,100000.0,100000000000.0]' :
'[1.0,1.1,1.0e-11,1.999999999999,223423.12345679,100000.0,100000000000.0]';
$this->assertSame($expected, $this->serializer->serialize($data));
setlocale(LC_NUMERIC, $originalLocale);
}
/**
* Test unserialization of scalar values
*
* @dataProvider scalarData
* @param mixed $scalar
* @param string $jsoned
* @return void
*/
#[\PHPUnit\Framework\Attributes\DataProvider('scalarData')]
public function testUnserializeScalar($scalar, $jsoned)
{
$this->assertSame($scalar, $this->serializer->unserialize($jsoned));
}
/**
* List of scalar data
*/
public static function scalarData(): array
{
return array(
array('testing', '"testing"'),
array(123, '123'),
array(0, '0'),
array(0.0, '0.0'),
array(17.0, '17.0'),
array(17e1, '170.0'),
array(17.2, '17.2'),
array(true, 'true'),
array(false, 'false'),
array(null, 'null'),
// Non UTF8
array('ßåö', '"ßåö"')
);
}
/**
* Test the serialization of resources
*
* @return void
*/
public function testSerializeResource()
{
$this->expectException(JsonSerializerException::class);
$this->serializer->serialize(fopen(__FILE__, 'r'));
}
/**
* Test the serialization of closures when not providing closure serializer
*
* @return void
*/
public function testSerializeClosureWithoutSerializer()
{
$this->expectException(JsonSerializerException::class);
$this->serializer->serialize(
array('func' => function () {
echo 'whoops';
})
);
}
/**
* Test serialization of array without objects
*
* @dataProvider arrayNoObjectData
* @param array $array
* @param string $jsoned
* @return void
*/
#[\PHPUnit\Framework\Attributes\DataProvider('arrayNoObjectData')]
public function testSerializeArrayNoObject($array, $jsoned)
{
$this->assertSame($jsoned, $this->serializer->serialize($array));
}
/**
* Test unserialization of array without objects
*
* @dataProvider arrayNoObjectData
* @param array $array
* @param string $jsoned
* @return void
*/
#[\PHPUnit\Framework\Attributes\DataProvider('arrayNoObjectData')]
public function testUnserializeArrayNoObject($array, $jsoned)
{
$this->assertSame($array, $this->serializer->unserialize($jsoned));
}
/**
* List of array data
*/
public static function arrayNoObjectData(): array
{
return array(
array(array(1, 2, 3), '[1,2,3]'),
array(array(1, 'abc', false), '[1,"abc",false]'),
array(array('a' => 1, 'b' => 2, 'c' => 3), '{"a":1,"b":2,"c":3}'),
array(array('integer' => 1, 'string' => 'abc', 'bool' => false), '{"integer":1,"string":"abc","bool":false}'),
array(array(1, array('nested')), '[1,["nested"]]'),
array(array('integer' => 1, 'array' => array('nested')), '{"integer":1,"array":["nested"]}'),
array(array('integer' => 1, 'array' => array('nested' => 'object')), '{"integer":1,"array":{"nested":"object"}}'),
array(array(1.0, 2, 3e1), '[1.0,2,30.0]'),
);
}
/**
* Test serialization of objects
*
* @return void
*/
public function testSerializeObject()
{
$obj = new stdClass();
$this->assertSame('{"@type":"stdClass"}', $this->serializer->serialize($obj));
$obj = $empty = new SupportClasses\EmptyClass();
$this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}', $this->serializer->serialize($obj));
$obj = new SupportClasses\AllVisibilities();
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"this is public","prot":"protected","priv":"dont tell anyone"}';
$this->assertSame($expected, $this->serializer->serialize($obj));
$obj->pub = 'new value';
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":"new value","prot":"protected","priv":"dont tell anyone"}';
$this->assertSame($expected, $this->serializer->serialize($obj));
$obj->pub = $empty;
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}';
$this->assertSame($expected, $this->serializer->serialize($obj));
$array = array('instance' => $empty);
$expected = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}';
$this->assertSame($expected, $this->serializer->serialize($array));
$obj = new stdClass();
$obj->total = 10.0;
$obj->discount = 0.0;
$expected = '{"@type":"stdClass","total":10.0,"discount":0.0}';
$this->assertSame($expected, $this->serializer->serialize($obj));
}
/**
* Test unserialization of objects
*
* @return void
*/
public function testUnserializeObjects()
{
$serialized = '{"@type":"stdClass"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('stdClass', $obj);
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj);
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\AllVisibilities","pub":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"},"prot":"protected","priv":"dont tell anyone"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\AllVisibilities', $obj);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $obj->pub);
$prop = new ReflectionProperty($obj, 'prot');
$prop->setAccessible(true);
$this->assertSame('protected', $prop->getValue($obj));
$prop = new ReflectionProperty($obj, 'priv');
$prop->setAccessible(true);
$this->assertSame('dont tell anyone', $prop->getValue($obj));
$serialized = '{"instance":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\EmptyClass"}}';
$array = $this->serializer->unserialize($serialized);
$this->assertTrue(is_array($array));
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\EmptyClass', $array['instance']);
}
/**
* Test serialization of Enums
*
* @return void
*/
public function testSerializeEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}
$unitEnum = SupportEnums\MyUnitEnum::Hearts;
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"}';
$this->assertSame($expected, $this->serializer->serialize($unitEnum));
$backedEnum = SupportEnums\MyBackedEnum::Hearts;
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"}';
$this->assertSame($expected, $this->serializer->serialize($backedEnum));
$intBackedEnum = SupportEnums\MyIntBackedEnum::One;
$expected = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1}';
$this->assertSame($expected, $this->serializer->serialize($intBackedEnum));
}
/**
* Test serialization of multiple Enums
*
* @return void
*/
public function testSerializeMultipleEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}
$obj = new stdClass();
$obj->enum1 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum2 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum3 = SupportEnums\MyIntBackedEnum::One;
$obj->enum4 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum5 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum6 = SupportEnums\MyIntBackedEnum::One;
$expected = '{"@type":"stdClass","enum1":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"},"enum2":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"},"enum3":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1},"enum4":{"@type":"@1"},"enum5":{"@type":"@2"},"enum6":{"@type":"@3"}}';
$this->assertSame($expected, $this->serializer->serialize($obj));
}
/**
* Test unserialization of Enums
*
* @return void
*/
public function testUnserializeEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyUnitEnum', $obj);
$this->assertSame(SupportEnums\MyUnitEnum::Hearts, $obj);
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyBackedEnum', $obj);
$this->assertSame(SupportEnums\MyBackedEnum::Hearts, $obj);
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"Two","value":2}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyIntBackedEnum', $obj);
$this->assertSame(SupportEnums\MyIntBackedEnum::Two, $obj);
$this->assertSame(SupportEnums\MyIntBackedEnum::Two->value, $obj->value);
// wrong value of BackedEnum is ignored
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"S"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportEnums\MyBackedEnum', $obj);
$this->assertSame(SupportEnums\MyBackedEnum::Hearts, $obj);
$this->assertSame(SupportEnums\MyBackedEnum::Hearts->value, $obj->value);
}
/**
* Test unserialization of multiple Enums
*
* @return void
*/
public function testUnserializeMultipleEnums()
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}
$obj = new stdClass();
$obj->enum1 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum2 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum3 = SupportEnums\MyIntBackedEnum::One;
$obj->enum4 = SupportEnums\MyUnitEnum::Hearts;
$obj->enum5 = SupportEnums\MyBackedEnum::Hearts;
$obj->enum6 = SupportEnums\MyIntBackedEnum::One;
$serialized = '{"@type":"stdClass","enum1":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Hearts"},"enum2":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Hearts","value":"H"},"enum3":{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyIntBackedEnum","name":"One","value":1},"enum4":{"@type":"@1"},"enum5":{"@type":"@2"},"enum6":{"@type":"@3"}}';
$actualObj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('stdClass', $actualObj);
$this->assertEquals($obj, $actualObj);
}
/**
* Test unserialization of wrong UnitEnum
*
* @return void
*/
public function testUnserializeWrongUnitEnum() {
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}
// bad case generate Error
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyUnitEnum","name":"Circles"}';
$this->expectException(\Error::class);
$this->serializer->unserialize($serialized);
}
/**
* Test unserialization of wrong BackedEnum
*
* @return void
*/
public function testUnserializeWrongBackedEnum() {
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped("Enums are only available since PHP 8.1");
}
// bad case generate Error
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportEnums\\\\MyBackedEnum","name":"Circles","value":"C"}';
$this->expectException(\Error::class);
$this->serializer->unserialize($serialized);
}
/**
* Test serialization of objects using the custom serializers
*
* @return void
*/
public function testCustomObjectSerializer()
{
$obj = new SupportClasses\MyType();
$obj->field1 = 'x';
$obj->field2 = 'y';
$this->assertSame('{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}', $this->serializer->serialize($obj));
}
/**
* Test unserialization of objects using the custom serializers
*
* @return void
*/
public function testCustomObjectsUnserializer()
{
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MyType","fields":"x y"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('Zumba\JsonSerializer\Test\SupportClasses\MyType', $obj);
$this->assertSame('x', $obj->field1);
$this->assertSame('y', $obj->field2);
}
/**
* Test magic serialization methods
*
* @return void
*/
public function testSerializationMagicMethods()
{
$obj = new SupportClasses\MagicClass();
$serialized = '{"@type":"Zumba\\\\JsonSerializer\\\\Test\\\\SupportClasses\\\\MagicClass","show":true}';
$this->assertSame($serialized, $this->serializer->serialize($obj));
$this->assertFalse($obj->woke);
$obj = $this->serializer->unserialize($serialized);
$this->assertTrue($obj->woke);
}
/**
* Test serialization of DateTime classes
*
* Some interal classes, such as DateTime, cannot be initialized with
* ReflectionClass::newInstanceWithoutConstructor()
*
* @return void
*/
public function testSerializationOfDateTime()
{
$date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
$obj = $this->serializer->unserialize($this->serializer->serialize($date));
$this->assertSame($date->getTimestamp(), $obj->getTimestamp());
$date = new \DateTimeImmutable('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
$obj = $this->serializer->unserialize($this->serializer->serialize($date));
$this->assertSame($date->getTimestamp(), $obj->getTimestamp());
}
/**
* Test the serialization of closures providing closure serializer
*
* @return void
*/
public function testSerializationOfClosureWithSuperClosureOnConstructor()
{
if (!class_exists('SuperClosure\Serializer')) {
$this->markTestSkipped('SuperClosure is not installed.');
}
$closureSerializer = new SuperClosureSerializer();
$serializer = new JsonSerializer($closureSerializer);
$serialized = $serializer->serialize(
array(
'func' => function () {
return 'it works';
},
'nice' => true
)
);
$unserialized = $serializer->unserialize($serialized);
$this->assertTrue(is_array($unserialized));
$this->assertTrue($unserialized['nice']);
$this->assertInstanceOf('Closure', $unserialized['func']);
$this->assertSame('it works', $unserialized['func']());
}
/**
* Test the serialization of closures providing closure serializer
*
* @return void
*/
public function testSerializationOfClosureWithSuperClosureOnManager()
{
if (!class_exists('SuperClosure\Serializer')) {
$this->markTestSkipped('SuperClosure is not installed.');
}
$closureSerializer = new SuperClosureSerializer();
$serializer = new JsonSerializer();
$serializer->addClosureSerializer(new ClosureSerializer\SuperClosureSerializer($closureSerializer));
$serialized = $serializer->serialize(
array(
'func' => function () {
return 'it works';
},
'nice' => true
)
);
$unserialized = $serializer->unserialize($serialized);
$this->assertTrue(is_array($unserialized));
$this->assertTrue($unserialized['nice']);
$this->assertInstanceOf('Closure', $unserialized['func']);
$this->assertSame('it works', $unserialized['func']());
}
/**
* Test the serialization of closures providing closure serializer
*
* @return void
*/
public function testSerializationOfClosureWitOpisClosure()
{
if (!class_exists('Opis\Closure\SerializableClosure')) {
$this->markTestSkipped('OpisClosure is not installed.');
}
$serializer = new JsonSerializer();
$serializer->addClosureSerializer(new ClosureSerializer\OpisClosureSerializer());
$serialized = $serializer->serialize(
array(
'func' => function () {
return 'it works';
},
'nice' => true
)
);
$unserialized = $serializer->unserialize($serialized);
$this->assertTrue(is_array($unserialized));
$this->assertTrue($unserialized['nice']);
$this->assertInstanceOf('Closure', $unserialized['func']);
$this->assertSame('it works', $unserialized['func']());
}
/**
* Test the serialization of closures providing closure serializer
*
* @return void
*/
public function testSerializationOfClosureWitMultipleClosures()
{
if (!class_exists('SuperClosure\Serializer')) {
$this->markTestSkipped('SuperClosure is not installed.');
}
if (!class_exists('Opis\Closure\SerializableClosure')) {
$this->markTestSkipped('OpisClosure is not installed.');
}
$closureSerializer = new SuperClosureSerializer();
$serializer = new JsonSerializer();
$serializer->addClosureSerializer(new ClosureSerializer\SuperClosureSerializer($closureSerializer));
$serializeData = array(
'func' => function () {
return 'it works';
},
'nice' => true
);
// Make sure it was serialized with SuperClosure
$serialized = $serializer->serialize($serializeData);
echo $serialized;
$this->assertGreaterThanOrEqual(0, strpos($serialized, 'SuperClosure'));
$this->assertFalse(strpos($serialized, 'OpisClosure'));
// Test adding a new preferred closure serializer
$serializer->addClosureSerializer(new ClosureSerializer\OpisClosureSerializer());
$unserialized = $serializer->unserialize($serialized);
$this->assertTrue(is_array($unserialized));
$this->assertTrue($unserialized['nice']);
$this->assertInstanceOf('Closure', $unserialized['func']);
$this->assertSame('it works', $unserialized['func']());
// Serialize again with the new preferred closure serializer
$serialized = $serializer->serialize($serializeData);
$this->assertFalse(strpos($serialized, 'SuperClosure'));
$this->assertGreaterThanOrEqual(0, strpos($serialized, 'OpisClosure'));
}
/**
* Test the unserialization of closures without providing closure serializer
*
* @return void
*/
public function testUnserializeOfClosureWithoutSerializer()
{
if (!class_exists('SuperClosure\Serializer')) {
$this->markTestSkipped('SuperClosure is not installed.');
}
$closureSerializer = new SuperClosureSerializer();
$serializer = new JsonSerializer($closureSerializer);
$serialized = $serializer->serialize(
array(
'func' => function () {
return 'it works';
},
'nice' => true
)
);
$this->expectException(JsonSerializerException::class);
$this->serializer->unserialize($serialized);
}
/**
* Test unserialize of unknown class
*
* @return void
*/
public function testUnserializeUnknownClass()
{
$this->expectException(JsonSerializerException::class);
$serialized = '{"@type":"UnknownClass"}';
$this->serializer->unserialize($serialized);
}
/**
* Test serialization of undeclared properties
*
* @return void
*/
public function testSerializationUndeclaredProperties()
{
$obj = new stdClass();
$obj->param1 = true;
$obj->param2 = 'store me, please';
$serialized = '{"@type":"stdClass","param1":true,"param2":"store me, please"}';
$this->assertSame($serialized, $this->serializer->serialize($obj));
$obj2 = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('stdClass', $obj2);
$this->assertTrue($obj2->param1);
$this->assertSame('store me, please', $obj2->param2);
$serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('stdClass', $obj->sub);
$this->assertSame('value', $obj->sub->key);
}
/**
* Test undeclared properties setter (valid)
*
* @return void
*/
public function testSetUnserializeUndeclaredPropertyModeValid()
{
$value = $this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_SET);
$this->assertSame($value, $this->serializer);
}
/**
* Test undeclared properties setter (invalid)
*
* @return void
*/
public function testSetUnserializeUndeclaredPropertyModeInvalid()
{
$this->expectException('InvalidArgumentException');
$value = $this->serializer->setUnserializeUndeclaredPropertyMode('bad value');
}
/**
* Test unserialization of undeclared properties in SET mode
*
* @return void
*/
public function testUnserializeUndeclaredPropertySet()
{
$this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_SET);
$serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
$obj = $this->serializer->unserialize($serialized);
$this->assertInstanceOf('stdClass', $obj->sub);
$this->assertSame('value', $obj->sub->key);
}
/**
* Test unserialization of undeclared properties in IGNORE mode
*
* @return void
*/
public function testUnserializeUndeclaredPropertyIgnore()
{
$this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_IGNORE);
$serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
$obj = $this->serializer->unserialize($serialized);
$this->assertFalse(isset($obj->sub));
}
/**
* Test unserialization of undeclared properties in EXCEPTION mode
*
* @return void
*/
public function testUnserializeUndeclaredPropertyException()
{
$this->serializer->setUnserializeUndeclaredPropertyMode(JsonSerializer::UNDECLARED_PROPERTY_MODE_EXCEPTION);
$this->expectException(JsonSerializerException::class);
$serialized = '{"@type":"stdClass","sub":{"@type":"stdClass","key":"value"}}';
$obj = $this->serializer->unserialize($serialized);
}
/**
* Test serialize with recursion
*
* @return void
*/
public function testSerializeRecursion()
{
$c1 = new stdClass();
$c1->c2 = new stdClass();
$c1->c2->c3 = new stdClass();
$c1->c2->c3->c1 = $c1;
$c1->something = 'ok';
$c1->c2->c3->ok = true;
$expected = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
$this->assertSame($expected, $this->serializer->serialize($c1));
$c1 = new stdClass();
$c1->mirror = $c1;
$expected = '{"@type":"stdClass","mirror":{"@type":"@0"}}';
$this->assertSame($expected, $this->serializer->serialize($c1));
}
/**
* Test unserialize with recursion
*
* @return void
*/
public function testUnserializeRecursion()
{
$serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"ok":true}},"something":"ok"}';
$obj = $this->serializer->unserialize($serialized);
$this->assertTrue($obj->c2->c3->ok);
$this->assertSame($obj, $obj->c2->c3->c1);
$this->assertNotSame($obj, $obj->c2);
$serialized = '{"@type":"stdClass","c2":{"@type":"stdClass","c3":{"@type":"stdClass","c1":{"@type":"@0"},"c2":{"@type":"@1"},"c3":{"@type":"@2"}},"c3_copy":{"@type":"@2"}}}';
$obj = $this->serializer->unserialize($serialized);
$this->assertSame($obj, $obj->c2->c3->c1);
$this->assertSame($obj->c2, $obj->c2->c3->c2);
$this->assertSame($obj->c2->c3, $obj->c2->c3->c3);
$this->assertSame($obj->c2->c3_copy, $obj->c2->c3);
}
/**
* Test unserialize with bad JSON
*
* @return void
*/
public function testUnserializeBadJSON()
{
$this->expectException(JsonSerializerException::class);
$this->serializer->unserialize('[this is not a valid json!}');
}
/**
* The test attempts to serialize an array containing a NAN
*/
public function testSerializeInvalidData()
{
$this->expectException(JsonSerializerException::class);
$this->serializer->serialize(array(NAN));
}
/**
*
* @return void
*/
public function testSerializeBinaryStringScalar()
{
$data = '';
for ($i = 0; $i <= 255; $i++) {
$data .= chr($i);
}
$unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
$this->assertSame($data, $unserialized);
}
/**
*
* @return void
*/
public function testSerializeArrayWithBinaryStringsAsValues()
{
$data = '';
for ($i = 0; $i <= 255; $i++) {
$data .= chr($i);
}
$data = [$data, "$data 1", "$data 2"];
$unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
$this->assertSame($data, $unserialized);
}
/**
* Starting from 1 and not from 0 because php cannot handle the nil character (\u0000) in json keys as per:
* https://github.com/remicollet/pecl-json-c/issues/7
* https://github.com/json-c/json-c/issues/108
*
* @return void
*/
public function testSerializeArrayWithBinaryStringsAsKeys()
{
$data = '';
for ($i = 1; $i <= 255; $i++) {
$data .= chr($i);
}
$data = [$data => $data, "$data 1" => 'something'];
$unserialized = $this->serializer->unserialize($this->serializer->serialize($data));
$this->assertSame($data, $unserialized);
}
/**
*
* @return void
*/
public function testSerializeObjectWithBinaryStrings()
{
$data = '';
for ($i = 0; $i <= 255; $i++) {
$data .= chr($i);
}
$obj = new \stdClass();
$obj->string = $data;
$unserialized = $this->serializer->unserialize($this->serializer->serialize($obj));
$this->assertInstanceOf('stdClass', $obj);
$this->assertSame($obj->string, $unserialized->string);
}
/**
* Test serialization of SplDoubleLinkedList
*
* @return void
*/
public function testSerializationOfSplDoublyLinkedList()
{
$list = new \SplDoublyLinkedList();
$list->push('fizz');
$list->push(42);
$unserialized = $this->serializer->unserialize($this->serializer->serialize($list));
$this->assertTrue($list->serialize() === $unserialized->serialize());
}
}
|