BitBufferTest.php 1.4 KB

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