BitBufferTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Class BitBufferTest
  4. *
  5. * @created 08.02.2016
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2015 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Common;
  11. use chillerlan\QRCode\QRCodeException;
  12. use chillerlan\QRCode\Common\{BitBuffer, Mode};
  13. use PHPUnit\Framework\Attributes\DataProvider;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * BitBuffer coverage test
  17. */
  18. final class BitBufferTest extends TestCase{
  19. private BitBuffer $bitBuffer;
  20. protected function setUp():void{
  21. $this->bitBuffer = new BitBuffer;
  22. }
  23. /**
  24. * @phpstan-return array<string, array{0: int, 1: int}>
  25. */
  26. public static function bitProvider():array{
  27. return [
  28. 'number' => [Mode::NUMBER, 16],
  29. 'alphanum' => [Mode::ALPHANUM, 32],
  30. 'byte' => [Mode::BYTE, 64],
  31. 'kanji' => [Mode::KANJI, 128],
  32. 'hanzi' => [Mode::HANZI, 208],
  33. ];
  34. }
  35. #[DataProvider('bitProvider')]
  36. public function testPut(int $data, int $expected):void{
  37. $this->bitBuffer->put($data, 4);
  38. $this::assertSame($expected, $this->bitBuffer->getBuffer()[0]);
  39. $this::assertSame(4, $this->bitBuffer->getLength());
  40. }
  41. public function testReadException():void{
  42. $this->expectException(QRCodeException::class);
  43. $this->expectExceptionMessage('invalid $numBits');
  44. $this->bitBuffer->put(Mode::KANJI, 4);
  45. $this->bitBuffer->read($this->bitBuffer->available() + 1);
  46. }
  47. }