|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sunrise\Coder\Tests\Codec; |
| 6 | + |
| 7 | +use Generator; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Sunrise\Coder\Codec\UrlEncodedCodec; |
| 11 | + |
| 12 | +use const PHP_QUERY_RFC3986; |
| 13 | + |
| 14 | +final class UrlEncodedCodecTest extends TestCase |
| 15 | +{ |
| 16 | + public function testSupportedMediaTypes(): void |
| 17 | + { |
| 18 | + $supportedMediaTypeIdentifiers = []; |
| 19 | + foreach ((new UrlEncodedCodec())->getSupportedMediaTypes() as $supportedMediaType) { |
| 20 | + $supportedMediaTypeIdentifiers[] = $supportedMediaType->getIdentifier(); |
| 21 | + } |
| 22 | + |
| 23 | + self::assertSame(['application/x-www-form-urlencoded'], $supportedMediaTypeIdentifiers); |
| 24 | + } |
| 25 | + |
| 26 | + #[DataProvider('decodeDataProvider')] |
| 27 | + public function testDecode(mixed $expectedData, string $codingData, array $codingContext = [], array $codecContext = []): void |
| 28 | + { |
| 29 | + self::assertSame($expectedData, (new UrlEncodedCodec($codecContext))->decode($codingData, $codingContext)); |
| 30 | + } |
| 31 | + |
| 32 | + #[DataProvider('encodeDataProvider')] |
| 33 | + public function testEncode(string $expectedData, mixed $codingData, array $codingContext = [], array $codecContext = []): void |
| 34 | + { |
| 35 | + self::assertSame($expectedData, (new UrlEncodedCodec($codecContext))->encode($codingData, $codingContext)); |
| 36 | + } |
| 37 | + |
| 38 | + public static function decodeDataProvider(): Generator |
| 39 | + { |
| 40 | + yield [ |
| 41 | + ['foo' => 'bar'], |
| 42 | + 'foo=bar', |
| 43 | + ]; |
| 44 | + |
| 45 | + yield [ |
| 46 | + ['foo' => 'bar', 'bar' => 'baz'], |
| 47 | + 'foo=bar&bar=baz', |
| 48 | + ]; |
| 49 | + |
| 50 | + yield [ |
| 51 | + ['foo' => 'bar baz'], |
| 52 | + 'foo=bar+baz', |
| 53 | + ]; |
| 54 | + |
| 55 | + yield [ |
| 56 | + ['foo' => 'bar baz'], |
| 57 | + 'foo=bar%20baz', |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + public static function encodeDataProvider(): Generator |
| 62 | + { |
| 63 | + yield [ |
| 64 | + 'foo=bar', |
| 65 | + ['foo' => 'bar'], |
| 66 | + ]; |
| 67 | + |
| 68 | + yield [ |
| 69 | + 'foo=bar&bar=baz', |
| 70 | + ['foo' => 'bar', 'bar' => 'baz'], |
| 71 | + ]; |
| 72 | + |
| 73 | + yield [ |
| 74 | + 'foo=bar+baz', |
| 75 | + ['foo' => 'bar baz'], |
| 76 | + ]; |
| 77 | + |
| 78 | + yield [ |
| 79 | + 'foo=bar%20baz', |
| 80 | + ['foo' => 'bar baz'], |
| 81 | + [UrlEncodedCodec::CONTEXT_KEY_ENCODING_TYPE => PHP_QUERY_RFC3986], |
| 82 | + ]; |
| 83 | + |
| 84 | + yield [ |
| 85 | + 'foo=bar%20baz', |
| 86 | + ['foo' => 'bar baz'], |
| 87 | + [], |
| 88 | + [UrlEncodedCodec::CONTEXT_KEY_ENCODING_TYPE => PHP_QUERY_RFC3986], |
| 89 | + ]; |
| 90 | + } |
| 91 | +} |
0 commit comments