diff --git a/CHANGELOG.md b/CHANGELOG.md index e70f6e4..d1b92a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 wrapping it or writing a broken varint: `int32`/`sint32`/`sfixed32`/enum outside [-2^31, 2^31-1] (e.g. `int32Of(2 ** 31)` used to wrap to a negative number), `uint32`/`fixed32` outside [0, 2^32-1], and `uint64`/`fixed64` that are negative, above 2^64-1 or fractional. -- Serializing a string-backed PHP enum as a protobuf enum throws `Exception\InvalidSchema` instead of - `\InvalidArgumentException`. +- `enumT()`, `enumOf()`, `Type\EnumT` and `Reflection\EnumT` accept only int-backed PHP enums and throw + `Exception\InvalidSchema` for any other class when the type is declared. A string-backed enum used to fail + with `\InvalidArgumentException` only when a value was serialized. +- Malformed input makes `Serializer::deserialize()` and `Decoder::decode()` throw a subclass of the new + `Exception\MalformedMessage`: `Exception\BufferUnderflow` (now a subclass of it) for a truncated input, + `Exception\MalformedVarint` for a varint longer than 10 bytes, `Exception\UnsupportedWireType` for an unknown + wire type. They used to throw `Thesis\Varint\MalformedVarintNumber` and `\ValueError`, which escaped + `catch (ProtobufException)` when the low-level API was used. +- `Encoder::encode()` and `Decoder::decode()` no longer wrap every `\Throwable` into `EncodingError`/`DecodingError`. + Library errors are `ProtobufException`s already; anything else, such as a `\TypeError` from a property whose + PHP type does not match its protobuf type, is a bug in the schema or the code and now propagates as is. + `Decoder::decode()` still reports a class that cannot be reflected as `DecodingError`. + +### Deprecated + +- `Encoder\EncodingError` is not thrown any more: a `catch (EncodingError)` no longer catches anything. Catch + `ProtobufException` or one of its specific subclasses instead. ### Fixed diff --git a/src/Decoder/Internal/ReflectionDecoder.php b/src/Decoder/Internal/ReflectionDecoder.php index f952bb9..d3cdbab 100644 --- a/src/Decoder/Internal/ReflectionDecoder.php +++ b/src/Decoder/Internal/ReflectionDecoder.php @@ -5,7 +5,6 @@ namespace Thesis\Protobuf\Decoder\Internal; use Thesis\Protobuf\Decoder; -use Thesis\Protobuf\ProtobufException; use Thesis\Protobuf\Reflection\Reflector; use Thesis\Protobuf\Serializer; @@ -30,10 +29,8 @@ public function decode(string $buffer, string $classType): object ), $classType, ); - } catch (ProtobufException $e) { - throw $e; - } catch (\Throwable $e) { - throw new Decoder\DecodingError($e->getMessage(), (int) $e->getCode(), $e); + } catch (\ReflectionException $e) { + throw new Decoder\DecodingError($e->getMessage(), $e->getCode(), $e); } } } diff --git a/src/Encoder/EncodingError.php b/src/Encoder/EncodingError.php index 30f0855..1d231d8 100644 --- a/src/Encoder/EncodingError.php +++ b/src/Encoder/EncodingError.php @@ -8,5 +8,7 @@ /** * @api + * @deprecated Not thrown any more: encoding errors are reported by the specific subclasses of ProtobufException, + * such as Exception\ValueOutOfRange. Catch ProtobufException instead. Will be removed in the next minor version. */ final class EncodingError extends ProtobufException {} diff --git a/src/Encoder/Internal/ReflectionEncoder.php b/src/Encoder/Internal/ReflectionEncoder.php index 60d559d..63d0023 100644 --- a/src/Encoder/Internal/ReflectionEncoder.php +++ b/src/Encoder/Internal/ReflectionEncoder.php @@ -5,7 +5,6 @@ namespace Thesis\Protobuf\Encoder\Internal; use Thesis\Protobuf\Encoder; -use Thesis\Protobuf\ProtobufException; use Thesis\Protobuf\Reflection\Reflector; use Thesis\Protobuf\Serializer; @@ -22,14 +21,8 @@ public function __construct( #[\Override] public function encode(object $message): string { - try { - return $this->serializer->serialize( - $this->reflector->message($message), - ); - } catch (ProtobufException $e) { - throw $e; - } catch (\Throwable $e) { - throw new Encoder\EncodingError($e->getMessage(), (int) $e->getCode(), $e); - } + return $this->serializer->serialize( + $this->reflector->message($message), + ); } } diff --git a/src/Exception/BufferUnderflow.php b/src/Exception/BufferUnderflow.php index e1599b7..5e3078d 100644 --- a/src/Exception/BufferUnderflow.php +++ b/src/Exception/BufferUnderflow.php @@ -4,9 +4,9 @@ namespace Thesis\Protobuf\Exception; -use Thesis\Protobuf\ProtobufException; - /** + * The input ends before the value being read does. + * * @api */ -final class BufferUnderflow extends ProtobufException {} +final class BufferUnderflow extends MalformedMessage {} diff --git a/src/Exception/MalformedMessage.php b/src/Exception/MalformedMessage.php new file mode 100644 index 0000000..2e36ed8 --- /dev/null +++ b/src/Exception/MalformedMessage.php @@ -0,0 +1,15 @@ + $wireType + */ + public function __construct( + public readonly int $wireType, + public readonly int $field, + ) { + parent::__construct(\sprintf('Unsupported wire type %d for field %d.', $wireType, $field)); + } +} diff --git a/src/Internal/Buffer/buffer.php b/src/Internal/Buffer/buffer.php index 04a3b11..0a2234d 100644 --- a/src/Internal/Buffer/buffer.php +++ b/src/Internal/Buffer/buffer.php @@ -5,7 +5,7 @@ namespace Thesis\Protobuf\Internal\Buffer; use BcMath\Number; -use Thesis\Protobuf\Exception\BufferUnderflow; +use Thesis\Protobuf\Exception\MalformedMessage; use Thesis\Protobuf\Internal\Serde\SerdeVarint; use Thesis\Protobuf\Internal\Wire; @@ -23,7 +23,7 @@ function copy(ReadBuffer $src, WriteBuffer $dst): void /** * @internal - * @throws BufferUnderflow + * @throws MalformedMessage */ function slice(ReadBuffer $src): ReadBuffer { diff --git a/src/Internal/IntBackedEnum.php b/src/Internal/IntBackedEnum.php new file mode 100644 index 0000000..3960e6b --- /dev/null +++ b/src/Internal/IntBackedEnum.php @@ -0,0 +1,34 @@ + */ + private static array $checked = []; + + /** + * Protobuf enum numbers are integers, so only an int-backed PHP enum can represent a protobuf enum. + * + * @throws InvalidSchema + */ + public static function check(string $enum): void + { + if (isset(self::$checked[$enum])) { + return; + } + + if (!enum_exists($enum) || new \ReflectionEnum($enum)->getBackingType()?->getName() !== 'int') { + throw new InvalidSchema(\sprintf('"%s" must be an int-backed enum to represent a protobuf enum.', $enum)); + } + + self::$checked[$enum] = true; + } +} diff --git a/src/Internal/Serde/SerdeString.php b/src/Internal/Serde/SerdeString.php index ee4f5ce..4297b84 100644 --- a/src/Internal/Serde/SerdeString.php +++ b/src/Internal/Serde/SerdeString.php @@ -32,10 +32,9 @@ public function serialize(WriteBuffer $buffer, mixed $value): void #[\Override] public function deserialize(ReadBuffer $buffer): string { + // A varint is never negative; one above PHP_INT_MAX saturates and fails the read below. + /** @var non-negative-int $length */ $length = (int) SerdeVarint::T->deserialize($buffer)->value; - if ($length < 0) { - throw new \UnexpectedValueException("String length must be positive or equal to 0, '{$length}' given."); - } if ($length === 0) { return ''; diff --git a/src/Internal/Serde/SerializeEnum.php b/src/Internal/Serde/SerializeEnum.php index 56f9a16..8f17fab 100644 --- a/src/Internal/Serde/SerializeEnum.php +++ b/src/Internal/Serde/SerializeEnum.php @@ -4,7 +4,6 @@ namespace Thesis\Protobuf\Internal\Serde; -use Thesis\Protobuf\Exception\InvalidSchema; use Thesis\Protobuf\Internal\Buffer\WriteBuffer; /** @@ -18,10 +17,9 @@ enum SerializeEnum implements SerializeValue #[\Override] public function serialize(WriteBuffer $buffer, mixed $value): void { + // EnumT accepts only int-backed enums. + /** @var int $case */ $case = $value->value; - if (!\is_int($case)) { - throw new InvalidSchema(\sprintf('Enum "%s" must be int-backed to be a protobuf enum.', $value::class)); - } // Enum numbers are int32: a negative one is sign-extended to 64 bits. SerdeInt64::T->serialize($buffer, Range::int32($case, 'enum')); diff --git a/src/Internal/Wire/wire.php b/src/Internal/Wire/wire.php index df55594..f4eead6 100644 --- a/src/Internal/Wire/wire.php +++ b/src/Internal/Wire/wire.php @@ -6,6 +6,8 @@ use BcMath\Number; use Thesis\Protobuf\Exception\BufferUnderflow; +use Thesis\Protobuf\Exception\MalformedMessage; +use Thesis\Protobuf\Exception\MalformedVarint; use Thesis\Protobuf\Internal\Buffer\ReadBuffer; use Thesis\Protobuf\Internal\Buffer\WriteBuffer; use Thesis\Protobuf\Internal\Serde\SerdeVarint; @@ -22,7 +24,7 @@ * bytes - 1 ≤ size ≤ 10 + length. * * @internal - * @throws BufferUnderflow + * @throws MalformedMessage */ function discardUnknown(ReadBuffer $buffer, Tag $tag): UnknownField { @@ -43,16 +45,33 @@ function discardUnknown(ReadBuffer $buffer, Tag $tag): UnknownField /** * @internal - * @throws BufferUnderflow + * @throws MalformedMessage */ function readVarint(ReadBuffer $buffer): Number { - $number = Varint\BcMath::Codec->decodeVarintSized($buffer->peek(10)); + $number = decodeVarint($buffer->peek(10)); $buffer->read($number->size); return $number->value; } +/** + * @internal + * @param non-empty-string $bytes up to 10 bytes from the start of a varint + * @throws MalformedMessage + */ +function decodeVarint(string $bytes): Varint\Sized +{ + try { + return Varint\BcMath::Codec->decodeVarintSized($bytes); + } catch (Varint\MalformedVarintNumber) { + // No terminating byte among the bytes given: either the input ends or the varint is too long. + throw \strlen($bytes) < 10 + ? new BufferUnderflow('Unexpected end of input: truncated varint.') + : new MalformedVarint('Malformed varint: longer than 10 bytes.'); + } +} + /** * @internal */ @@ -63,18 +82,16 @@ function writeTag(WriteBuffer $buffer, Tag $tag): void /** * @internal - * @throws BufferUnderflow + * @throws MalformedMessage */ function peekTag(ReadBuffer $buffer): Tag { - $number = Varint\BcMath::Codec->decodeVarintSized($buffer->peek(10)); - - return Tag::from($number->value); + return Tag::from(decodeVarint($buffer->peek(10))->value); } /** * @internal - * @throws BufferUnderflow + * @throws MalformedMessage */ function readTag(ReadBuffer $buffer): Tag { diff --git a/src/Reflection/EnumT.php b/src/Reflection/EnumT.php index 5691af8..d7ac8b5 100644 --- a/src/Reflection/EnumT.php +++ b/src/Reflection/EnumT.php @@ -4,6 +4,9 @@ namespace Thesis\Protobuf\Reflection; +use Thesis\Protobuf\Exception\InvalidSchema; +use Thesis\Protobuf\Internal\IntBackedEnum; + /** * @api * @template-covariant T of \BackedEnum @@ -13,10 +16,13 @@ { /** * @param class-string $enum + * @throws InvalidSchema if the class is not an int-backed enum */ public function __construct( public string $enum, - ) {} + ) { + IntBackedEnum::check($enum); + } #[\Override] public function accept(Visitor $visitor): mixed diff --git a/src/Serializer.php b/src/Serializer.php index 3455027..f0042ed 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -4,7 +4,7 @@ namespace Thesis\Protobuf; -use Thesis\Protobuf\Exception\BufferUnderflow; +use Thesis\Protobuf\Exception\MalformedMessage; use Thesis\Protobuf\Internal\Buffer\ByteBuffer; use Thesis\Protobuf\Internal\Serde\UnknownEnumValue; use Thesis\Protobuf\Internal\Wire; @@ -18,6 +18,9 @@ */ final readonly class Serializer { + /** + * @throws Exception\ValueOutOfRange if a value does not fit its protobuf type + */ public function serialize(Message $message): string { $buffer = new ByteBuffer(); @@ -38,7 +41,7 @@ public function serialize(Message $message): string } /** - * @throws BufferUnderflow + * @throws MalformedMessage if the bytes are not a valid protobuf encoding */ public function deserialize(MessageT $type, string $bytes): Message { diff --git a/src/Tag.php b/src/Tag.php index ae87040..c953e9c 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -14,6 +14,9 @@ final class Tag /** @phpstan-ignore property.uninitialized */ public private(set) Number $number { get => $this->number ??= new Number($this->num << 3 | $this->type->value); } + /** + * @throws Exception\UnsupportedWireType + */ public static function from(Number $number): self { $tag = (int) $number->value; @@ -24,7 +27,7 @@ public static function from(Number $number): self return new self( $num, - WireType::from($type), + WireType::tryFrom($type) ?? throw new Exception\UnsupportedWireType($type, $num), ); } diff --git a/src/Type/EnumT.php b/src/Type/EnumT.php index 90fb088..38263de 100644 --- a/src/Type/EnumT.php +++ b/src/Type/EnumT.php @@ -4,6 +4,8 @@ namespace Thesis\Protobuf\Type; +use Thesis\Protobuf\Exception\InvalidSchema; +use Thesis\Protobuf\Internal\IntBackedEnum; use Thesis\Protobuf\Type; /** @@ -19,10 +21,13 @@ /** * @param class-string<\BackedEnum> $enum + * @throws InvalidSchema if the class is not an int-backed enum */ public function __construct( public string $enum, - ) {} + ) { + IntBackedEnum::check($enum); + } #[\Override] public function accept(Visitor $visitor): mixed diff --git a/src/Value.php b/src/Value.php index e2c3133..e3a820b 100644 --- a/src/Value.php +++ b/src/Value.php @@ -171,7 +171,6 @@ public static function listOf( * @param Type $valueT * @param Map $map * @return self> - * @throws \UnexpectedValueException */ public static function mapOf( Type $keyT, diff --git a/src/constructors.php b/src/constructors.php index bc65779..c3fea61 100644 --- a/src/constructors.php +++ b/src/constructors.php @@ -273,7 +273,6 @@ function listT(Type $element, ?bool $packed = null): Type\ListT * @param Type $valueT * @param Map $map * @return Value> - * @throws \UnexpectedValueException */ function mapOf( Type $keyT, @@ -294,7 +293,6 @@ function mapOf( * @param Type $keyT * @param Type $valueT * @return Type\MapT - * @throws \UnexpectedValueException */ function mapT(Type $keyT, Type $valueT): Type\MapT { diff --git a/tests/Decoder/Internal/ReflectionDecoderTest.php b/tests/Decoder/Internal/ReflectionDecoderTest.php index 516f5fa..cbeafb3 100644 --- a/tests/Decoder/Internal/ReflectionDecoderTest.php +++ b/tests/Decoder/Internal/ReflectionDecoderTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Thesis\Protobuf\Decoder\Builder; +use Thesis\Protobuf\Decoder\DecodingError; use Thesis\Protobuf\Reflection; #[CoversClass(ReflectionDecoder::class)] @@ -32,6 +33,23 @@ public function testRequiredPropertiesCannotBeInitializedImplicitly(): void $decoder->decode('', Request::class); } + + public function testUnknownClassIsReportedAsDecodingError(): void + { + $this->expectException(DecodingError::class); + $this->expectExceptionMessage('NoSuchClass'); + + /** @phpstan-ignore argument.type */ + Builder::buildDefault()->decode('', 'NoSuchClass'); + } + + public function testConfigurationErrorsAreNotMaskedAsDecodingErrors(): void + { + // The property type does not match the declared protobuf type: a bug in the schema, not in the input. + $this->expectException(\TypeError::class); + + Builder::buildDefault()->decode("\x0a\x03abc", MismatchedPropertyType::class); + } } final readonly class Request @@ -43,3 +61,11 @@ public function __construct( public int $id, ) {} } + +final readonly class MismatchedPropertyType +{ + public function __construct( + #[Reflection\Field(1, Reflection\StringT::T)] + public int $value = 0, + ) {} +} diff --git a/tests/Encoder/Internal/ReflectionEncoderTest.php b/tests/Encoder/Internal/ReflectionEncoderTest.php index b4f01cf..89388bb 100644 --- a/tests/Encoder/Internal/ReflectionEncoderTest.php +++ b/tests/Encoder/Internal/ReflectionEncoderTest.php @@ -22,6 +22,14 @@ public function testEncode(): void self::assertSame('0a084a6f686e20446f651032', bin2hex($encoder->encode(new Request('John Doe', 50)))); } + public function testConfigurationErrorsAreNotWrapped(): void + { + // The property type does not match the declared protobuf type: a bug in the schema, not an encoding error. + $this->expectException(\TypeError::class); + + Builder::buildDefault()->encode(new MismatchedPropertyType('abc')); + } + public function testEncodeOptionalScalarZeroValues(): void { $encoder = Builder::buildDefault(); @@ -58,3 +66,11 @@ public function __construct( public ?bool $enabled = null, ) {} } + +final readonly class MismatchedPropertyType +{ + public function __construct( + #[Reflection\Field(1, Reflection\Int32T::T)] + public string $value = '', + ) {} +} diff --git a/tests/EnumDeclarationTest.php b/tests/EnumDeclarationTest.php new file mode 100644 index 0000000..7e211ae --- /dev/null +++ b/tests/EnumDeclarationTest.php @@ -0,0 +1,68 @@ +expectException(InvalidSchema::class); + $this->expectExceptionMessage($message); + + $declare(); + } + + /** + * @return iterable + */ + public static function provideOnlyIntBackedEnumsAreAcceptedCases(): iterable + { + $stringBacked = \sprintf('"%s" must be an int-backed enum', EnumDeclarationTestString::class); + $pure = \sprintf('"%s" must be an int-backed enum', EnumDeclarationTestPure::class); + $class = \sprintf('"%s" must be an int-backed enum', \stdClass::class); + + yield 'string-backed' => [static fn() => enumT(EnumDeclarationTestString::class), $stringBacked]; + yield 'string-backed value' => [static fn() => enumOf(EnumDeclarationTestString::A), $stringBacked]; + /** @phpstan-ignore argument.type */ + yield 'pure enum' => [static fn() => enumT(EnumDeclarationTestPure::class), $pure]; + /** @phpstan-ignore argument.type */ + yield 'not an enum' => [static fn() => enumT(\stdClass::class), $class]; + yield 'reflection string-backed' => [static fn() => new Reflection\EnumT(EnumDeclarationTestString::class), $stringBacked]; + /** @phpstan-ignore argument.type */ + yield 'reflection pure enum' => [static fn() => new Reflection\EnumT(EnumDeclarationTestPure::class), $pure]; + } + + public function testIntBackedEnumsAreAccepted(): void + { + self::assertSame(EnumDeclarationTestInt::class, enumT(EnumDeclarationTestInt::class)->enum); + self::assertSame(EnumDeclarationTestInt::class, new Reflection\EnumT(EnumDeclarationTestInt::class)->enum); + } +} + +enum EnumDeclarationTestInt: int +{ + case A = 1; +} + +enum EnumDeclarationTestString: string +{ + case A = 'a'; +} + +enum EnumDeclarationTestPure +{ + case A; +} diff --git a/tests/MalformedInputTest.php b/tests/MalformedInputTest.php new file mode 100644 index 0000000..26104f6 --- /dev/null +++ b/tests/MalformedInputTest.php @@ -0,0 +1,127 @@ + $exception + */ + #[DataProvider('provideMalformedInputIsReportedCases')] + public function testMalformedInputIsReported(string $hex, string $exception, string $message): void + { + $type = messageT(fieldT(1, int64T), fieldT(2, stringT)); + + try { + new Serializer()->deserialize($type, (string) hex2bin($hex)); + self::fail('Deserialization must fail.'); + } catch (MalformedMessage $e) { + self::assertInstanceOf($exception, $e); + self::assertStringContainsString($message, $e->getMessage()); + } + } + + /** + * @return iterable, string}> + */ + public static function provideMalformedInputIsReportedCases(): iterable + { + yield 'truncated tag' => ['80', BufferUnderflow::class, 'truncated varint']; + yield 'truncated varint value' => ['08ffff', BufferUnderflow::class, 'truncated varint']; + yield 'truncated length' => ['1280', BufferUnderflow::class, 'truncated varint']; + yield 'length beyond the end' => ['120561', BufferUnderflow::class, "Expected '5' bytes"]; + yield 'varint longer than 10 bytes' => ['08ffffffffffffffffffff01', MalformedVarint::class, 'longer than 10 bytes']; + yield 'wire type 6' => ['0e', UnsupportedWireType::class, 'wire type 6']; + yield 'wire type 7' => ['0f', UnsupportedWireType::class, 'wire type 7']; + yield 'unknown field with wire type 7' => ['1f', UnsupportedWireType::class, 'wire type 7']; + } + + public function testTruncatedTagAfterRepeatedElementIsReported(): void + { + // After an unpacked element the parser peeks at the next tag to see whether the field repeats. + $type = messageT(fieldT(1, listT(int64T, false))); + + $this->expectException(BufferUnderflow::class); + $this->expectExceptionMessage('truncated varint'); + + new Serializer()->deserialize($type, "\x08\x01\x80"); + } + + public function testRandomInputFailsOnlyWithProtobufExceptions(): void + { + $type = messageT( + fieldT(1, int32T), + fieldT(2, stringT), + fieldT(3, listT(sint64T)), + fieldT(4, listT(stringT)), + fieldT(5, mapT(uint32T, stringT)), + fieldT(6, messageT(fieldT(1, fixed64T), fieldT(2, doubleT))), + fieldT(7, enumT(MalformedInputTestEnum::class)), + fieldT(8, uint64T), + fieldT(9, floatT), + fieldT(10, boolT), + ); + $serializer = new Serializer(); + + // A fixed seed keeps the inputs, and so the test, reproducible. + $random = new Randomizer(new Mt19937(20261117)); + + for ($i = 0; $i < 5000; ++$i) { + $bytes = $random->getBytes($random->getInt(1, 24)); + + try { + $serializer->deserialize($type, $bytes); + } catch (ProtobufException) { + // Expected for most random inputs. + } catch (\Throwable $e) { + self::fail(\sprintf('Input %s failed with %s: %s', bin2hex($bytes), $e::class, $e->getMessage())); + } + } + + $this->addToAssertionCount(1); + } + + public function testDecoderReportsMalformedInputAsIs(): void + { + $this->expectExceptionObject(new UnsupportedWireType(7, 1)); + + Decoder\Builder::buildDefault()->decode("\x0f", MalformedInputTestMessage::class); + } +} + +enum MalformedInputTestEnum: int +{ + case Zero = 0; + case One = 1; +} + +final readonly class MalformedInputTestMessage +{ + public function __construct( + #[Reflection\Field(1, Reflection\Int64T::T)] + public int $value = 0, + ) {} +} diff --git a/tests/ValueRangeTest.php b/tests/ValueRangeTest.php index 1a632cf..0e8e3c3 100644 --- a/tests/ValueRangeTest.php +++ b/tests/ValueRangeTest.php @@ -8,7 +8,6 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Thesis\Protobuf\Exception\InvalidSchema; use Thesis\Protobuf\Exception\ValueOutOfRange; use Thesis\Protobuf\Internal\Serde\Range; use Thesis\Protobuf\Internal\Serde\SerdeFixed32; @@ -120,14 +119,6 @@ public function testNegativeEnumRoundTrips(): void self::assertSame(ValueRangeTestEnum::MinusOne, ($decoded->fields[1] ?? null)?->value->value); } - public function testStringBackedEnumIsRejected(): void - { - $this->expectException(InvalidSchema::class); - $this->expectExceptionMessage(\sprintf('Enum "%s" must be int-backed', ValueRangeTestStringEnum::class)); - - new Serializer()->serialize(message(fieldOf(1, enumOf(ValueRangeTestStringEnum::A)))); - } - public function testEncoderReportsOutOfRangeValues(): void { $this->expectException(ValueOutOfRange::class); @@ -146,11 +137,6 @@ enum ValueRangeTestEnum: int case TooSmall = -2147483649; } -enum ValueRangeTestStringEnum: string -{ - case A = 'a'; -} - final readonly class ValueRangeTestMessage { public function __construct(