safeValidate($value); static::assertFalse($result->isValid()); static::assertSame(['invalid_bytes'], \array_column($result->violations, 'code')); } /** @return array */ public static function invalidShapeProvider(): array { return [ 'null' => [NULL], 'string' => ['Hello'], 'number' => [42], 'list' => [[72, 101, 108, 108, 111]], 'plain object' => [['value' => 'SGVsbG8=']], 'non-string payload' => [['$bytes' => [72, 101]]], 'extra field' => [['$bytes' => 'SGVsbG8=', 'extra' => TRUE]], ]; } #[DataProvider('encodingProvider')] public function testRequiresCanonicalBase64(string $encoded, bool $valid): void { $result = new BytesSchema()->safeValidate(['$bytes' => $encoded]); static::assertSame($valid, $result->isValid()); } /** * @return array */ public static function encodingProvider(): array { return [ 'canonical padded' => ['Zg==', TRUE], 'canonical unpadded' => ['Zg', TRUE], 'empty bytes' => ['', TRUE], 'whitespace' => ['Z g==', FALSE], 'non-zero padding bits' => ['Zh==', FALSE], 'partial padding' => ['Zg=', FALSE], ]; } /** @param list $codes */ #[DataProvider('lengthProvider')] public function testEnforcesDecodedByteLength(string $encoded, bool $valid, array $codes): void { $result = new BytesSchema(minLength: 2, maxLength: 4)->safeValidate(['$bytes' => $encoded]); static::assertSame($valid, $result->isValid()); static::assertSame($codes, \array_column($result->violations, 'code')); } /** @return array}> */ public static function lengthProvider(): array { return [ 'below minimum' => [\base64_encode("\x01"), FALSE, ['min_length']], 'at minimum' => [\base64_encode("\x01\x02"), TRUE, []], 'within range' => [\base64_encode("\x01\x02\x03"), TRUE, []], 'at maximum' => [\base64_encode("\x01\x02\x03\x04"), TRUE, []], 'above maximum' => [\base64_encode("\x01\x02\x03\x04\x05"), FALSE, ['max_length']], ]; } public function testZeroLengthBounds(): void { static::assertTrue(new BytesSchema(minLength: 0)->safeValidate(['$bytes' => ''])->isValid()); static::assertTrue(new BytesSchema(maxLength: 0)->safeValidate(['$bytes' => ''])->isValid()); static::assertFalse(new BytesSchema(maxLength: 0)->safeValidate(['$bytes' => 'AA=='])->isValid()); } #[DataProvider('canonicalBase64VectorProvider')] public function testAcceptsCanonicalBase64Vectors(string $encoded): void { static::assertTrue(new BytesSchema()->safeValidate(['$bytes' => $encoded])->isValid()); } /** @return iterable */ public static function canonicalBase64VectorProvider(): iterable { $values = [ '', "\0\0", "\0\0\0", "\0\0\0\0", '__', 'é', 'àç', "\0éàç", "```\x1B", 'aaa', 'Hello, World!', '😀😃😄😁😆😅😂🤣😊😇', '👩‍💻👨‍💻👩‍🔬👨‍🔬👩‍🚀👨‍🚀', '🌍🌎🌏🌐🪐🌟✨⚡🔥💧', "\xFB\xFF\xBF", "\xFB\xFF\xBF", "\x4D", "\x4D\x61", "\x4D\x61\x6E", "\x4D", "\x4D\x61", "\x00\x4D\x61\x6E\x00", ]; foreach ($values as $index => $value) { $padded = \base64_encode($value); yield 'padded ' . $index => [$padded]; $unpadded = \rtrim($padded, '='); if ($padded !== $unpadded) { yield 'unpadded ' . $index => [$unpadded]; } } } #[DataProvider('invalidBase64VectorProvider')] public function testRejectsInvalidBase64Vectors(string $encoded): void { static::assertFalse(new BytesSchema()->safeValidate(['$bytes' => $encoded])->isValid()); } /** @return array */ public static function invalidBase64VectorProvider(): array { return [ 'cedilla pair' => ['çç'], 'accented character' => ['é'], 'trailing dollars' => ['YWJjZGU$$$'], 'at signs' => ['@@@@'], 'punctuation suffix' => ['abcd!'], 'interior padding' => ['ab=cd'], 'valid then one accent' => ['YWFhé'], 'valid then two accents' => ['YWFhéé'], 'valid then three accents' => ['YWFhééé'], 'valid then four accents' => ['YWFhéééé'], 'unneeded one padding' => ['YWFh='], 'unneeded two padding' => ['YWFh=='], 'unneeded three padding' => ['YWFh==='], 'unneeded four padding' => ['YWFh===='], 'unneeded five padding' => ['YWFh====='], 'unneeded six padding' => ['YWFh======'], 'accent in payload' => ['TWEé'], 'emoji in payload' => ['TWE👍'], 'one excessive padding for Ma' => ['TWE=='], 'two excessive padding for Ma' => ['TWE==='], 'one excessive padding for M' => ['TQ==='], 'two excessive padding for M' => ['TQ===='], ]; } public function testAcceptsTenMegabyteBase64Payload(): void { $bytes = \str_repeat("\xF0\x9F\x90\xA9", 2_500_000); static::assertTrue( new BytesSchema() ->safeValidate(['$bytes' => \base64_encode($bytes)]) ->isValid(), ); } }