Skip to content

Conversation

@innerfly
Copy link

Fixes an #6753, when json_encode fails to serialize non-backed enum.
Follow up #6762 (comment)

@innerfly innerfly changed the title Prevent serialization of non-backed enums #6753 Prevent serialization of non-backed enums Sep 18, 2024
Copy link
Member

@Arhell Arhell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Naktibalda look when you have time, thanks

@W0rma
Copy link
Contributor

W0rma commented Dec 11, 2025

@innerfly Can you try to add a test case?

@innerfly
Copy link
Author

@W0rma Simple test case:

<?php

namespace Unit;

use Codeception\Attribute\DataProvider;
use Codeception\Example;
use PHPUnit\Framework\Assert as Assert;

enum ExampleEnum
{
    case FOO;
    case BAR;
}

class ExampleCest
{
    protected function EnumProvider(): array
    {
        return [
            ['expected' => ExampleEnum::FOO, 'result' => ExampleEnum::BAR],
            ['expected' => ExampleEnum::BAR, 'result' => ExampleEnum::BAR],
        ];
    }

    #[DataProvider('EnumProvider')]
    public function testEnum(Example $example)
    {
        Assert::assertSame($example['expected'], $example['result']);
    }
}

Result:

There was 1 error:
1) ExampleCest: Test enum | "Unit\\ExampleEnum","Unit\\ExampleEnum"
 Test  tests/unit/ExampleCest.php:testEnum
                                                                  
  [JsonException] Non-backed enums have no default serialization  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants