Skip to content

Commit

Permalink
Add tests for int backed enums
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <[email protected]>
  • Loading branch information
MauricioFauth committed Sep 8, 2023
1 parent 3d9f1b7 commit 061736d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ public function testSerializeEnums()
$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));
}

/**
Expand All @@ -283,6 +287,12 @@ public function testUnserializeEnums()
$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);
Expand Down
9 changes: 9 additions & 0 deletions tests/SupportEnums/MyIntBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Zumba\JsonSerializer\Test\SupportEnums;

enum MyIntBackedEnum: int
{
case One = 1;
case Two = 2;
}

0 comments on commit 061736d

Please sign in to comment.