|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace PetrKnap\Binary; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +final class ByterTest extends TestCase |
| 9 | +{ |
| 10 | + #[DataProvider('dataBitesData')] |
| 11 | + public function testBitesData(array $expected, array $sizes): void |
| 12 | + { |
| 13 | + self::assertSame( |
| 14 | + $expected, |
| 15 | + (new Byter())->bite(self::getData(), ...$sizes), |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + public static function dataBitesData(): array |
| 20 | + { |
| 21 | + return [ |
| 22 | + 'one left bite' => [[hex2bin('0102'), hex2bin('030405')], [2]], |
| 23 | + 'one right bite' => [[hex2bin('0405'), hex2bin('010203')], [-2]], |
| 24 | + 'empty bite' => [['', hex2bin('0102030405')], [0]], |
| 25 | + 'full bite' => [[hex2bin('0102030405')], [5]], |
| 26 | + 'many bites' => [[hex2bin('0102'), hex2bin('0405'), '', hex2bin('03')], [2, -2, 0, 1]], |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + public function testBiteThrowsWhenThereIsNotEnoughData(): void |
| 31 | + { |
| 32 | + self::expectException(Exception\CouldNotBiteData::class); |
| 33 | + |
| 34 | + $data = self::getData(); |
| 35 | + $byter = new Byter(); |
| 36 | + $byter->bite( |
| 37 | + $data, |
| 38 | + $byter->size($data) + 1, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function testUnbitesBites(): void |
| 43 | + { |
| 44 | + self::assertSame( |
| 45 | + self::getData(), |
| 46 | + (new Byter())->unbite(hex2bin('0102'), hex2bin('030405')), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function testReturnsSizeOfData(): void |
| 51 | + { |
| 52 | + self::assertSame( |
| 53 | + 5, |
| 54 | + (new Byter())->size(self::getData()), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + private static function getData(): string |
| 59 | + { |
| 60 | + return hex2bin('0102030405'); |
| 61 | + } |
| 62 | +} |
0 commit comments