diff --git a/tests/Fixtures/DataFalse.php b/tests/Fixtures/DataFalse.php index ce3d2632f..5c7301cf8 100644 --- a/tests/Fixtures/DataFalse.php +++ b/tests/Fixtures/DataFalse.php @@ -6,8 +6,11 @@ class DataFalse { + public false $data; + public function __construct( - public false $data + false $data ) { + $this->data = $data; } } diff --git a/tests/Fixtures/DataTrue.php b/tests/Fixtures/DataTrue.php index 0bfe05b68..9e3f2b09f 100644 --- a/tests/Fixtures/DataTrue.php +++ b/tests/Fixtures/DataTrue.php @@ -7,4 +7,10 @@ class DataTrue { public true $data; + + public function __construct( + true $data + ) { + $this->data = $data; + } } diff --git a/tests/Serializer/JsonSerializationTest.php b/tests/Serializer/JsonSerializationTest.php index 7f59ca0d7..b41c771df 100644 --- a/tests/Serializer/JsonSerializationTest.php +++ b/tests/Serializer/JsonSerializationTest.php @@ -16,6 +16,7 @@ use JMS\Serializer\Tests\Fixtures\Author; use JMS\Serializer\Tests\Fixtures\AuthorList; use JMS\Serializer\Tests\Fixtures\DataFalse; +use JMS\Serializer\Tests\Fixtures\DataTrue; use JMS\Serializer\Tests\Fixtures\DiscriminatedAuthor; use JMS\Serializer\Tests\Fixtures\DiscriminatedComment; use JMS\Serializer\Tests\Fixtures\FirstClassMapCollection; @@ -28,6 +29,7 @@ use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; use JMS\Serializer\Visitor\SerializationVisitorInterface; use PHPUnit\Framework\Attributes\DataProvider; +use TypeError; class JsonSerializationTest extends BaseSerializationTestCase { @@ -483,10 +485,33 @@ public function testSerializingUnionProperties() self::assertEquals(static::getContent('data_string'), $serialized); } + public function testTrueDataType() + { + if (PHP_VERSION_ID < 80200) { + $this->markTestSkipped('True type requires PHP 8.2'); + + return; + } + + self::assertEquals( + static::getContent('data_true'), + $this->serialize(new DataTrue(true)), + ); + + self::assertEquals( + new DataTrue(true), + $this->deserialize(static::getContent('data_true'), DataTrue::class), + ); + + $this->expectException(TypeError::class); + $this->deserialize(static::getContent('data_false'), DataTrue::class); + } + public function testFalseDataType() { if (PHP_VERSION_ID < 80200) { $this->markTestSkipped('False type requires PHP 8.2'); + return; } @@ -500,11 +525,8 @@ public function testFalseDataType() $this->deserialize(static::getContent('data_false'), DataFalse::class), ); - //What should we expect here? - self::assertEquals( - null, - $this->deserialize(static::getContent('data_true'), DataFalse::class), - ); + $this->expectException(TypeError::class); + $this->deserialize(static::getContent('data_true'), DataFalse::class); } public function testDeserializingComplexDiscriminatedUnionProperties()