|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the Nexus framework. |
| 7 | + * |
| 8 | + * (c) John Paul E. Balandan, CPA <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Nexus\Tests\Encryption\Encoding; |
| 15 | + |
| 16 | +use Nexus\Encryption\Encoding\EncoderInterface; |
| 17 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +abstract class AbstractEncoderTestCase extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @param int<1, 32> $i |
| 27 | + */ |
| 28 | + #[DataProvider('provideEncodeDecodeCases')] |
| 29 | + public function testEncodeDecode(int $i): void |
| 30 | + { |
| 31 | + $encoder = $this->createEncoder(); |
| 32 | + $data = random_bytes($i); |
| 33 | + |
| 34 | + $encoded = $encoder->encode($data); |
| 35 | + $decoded = $encoder->decode($encoded); |
| 36 | + |
| 37 | + self::assertSame($data, $decoded); |
| 38 | + self::assertSame($this->nativeEncode($data), $encoded); |
| 39 | + self::assertSame($this->nativeDecode($encoded), $decoded); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return iterable<string, array{int}> |
| 44 | + */ |
| 45 | + public static function provideEncodeDecodeCases(): iterable |
| 46 | + { |
| 47 | + for ($i = 1; $i <= 32; ++$i) { |
| 48 | + yield \sprintf('%02d bytes', $i) => [$i]; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + abstract protected function createEncoder(): EncoderInterface; |
| 53 | + |
| 54 | + abstract protected function nativeEncode(string $data): string; |
| 55 | + |
| 56 | + abstract protected function nativeDecode(string $encoded): string; |
| 57 | +} |
0 commit comments