QRCodeTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Class QRCodeTest
  4. *
  5. * @filesource QRCodeTest.php
  6. * @created 17.11.2017
  7. * @package chillerlan\QRCodeTest
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2017 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCodeTest;
  13. use chillerlan\QRCode\{QROptions, QRCode};
  14. use chillerlan\QRCode\Data\{AlphaNum, Byte, Number, QRCodeDataException};
  15. use chillerlan\QRCode\Output\QRCodeOutputException;
  16. use chillerlan\QRCodeExamples\MyCustomOutput;
  17. use function random_bytes;
  18. class QRCodeTest extends QRTestAbstract{
  19. protected string $FQCN = QRCode::class;
  20. /**
  21. * @var \chillerlan\QRCode\QRCode
  22. */
  23. protected $qrcode;
  24. protected function setUp():void{
  25. parent::setUp();
  26. $this->qrcode = $this->reflection->newInstance();
  27. }
  28. public function testIsNumber():void{
  29. $this::assertTrue($this->qrcode->isNumber('0123456789'));
  30. $this::assertFalse($this->qrcode->isNumber('ABC'));
  31. }
  32. public function testIsAlphaNum():void{
  33. $this::assertTrue($this->qrcode->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'));
  34. $this::assertFalse($this->qrcode->isAlphaNum('abc'));
  35. }
  36. public function testIsKanji():void{
  37. $this::assertTrue($this->qrcode->isKanji('茗荷'));
  38. $this::assertFalse($this->qrcode->isKanji('Ã'));
  39. }
  40. // coverage
  41. public function typeDataProvider():array{
  42. return [
  43. 'png' => [QRCode::OUTPUT_IMAGE_PNG, 'data:image/png;base64,'],
  44. 'gif' => [QRCode::OUTPUT_IMAGE_GIF, 'data:image/gif;base64,'],
  45. 'jpg' => [QRCode::OUTPUT_IMAGE_JPG, 'data:image/jpg;base64,'],
  46. 'svg' => [QRCode::OUTPUT_MARKUP_SVG, '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="qr-svg " style="width: 100%; height: auto;" viewBox="'],
  47. 'html' => [QRCode::OUTPUT_MARKUP_HTML, '<div><span style="background:'],
  48. 'text' => [QRCode::OUTPUT_STRING_TEXT, '⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕'.PHP_EOL],
  49. 'json' => [QRCode::OUTPUT_STRING_JSON, '[[18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18],'],
  50. ];
  51. }
  52. /**
  53. * @dataProvider typeDataProvider
  54. * @param $type
  55. */
  56. public function testRenderImage($type, $expected):void{
  57. $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['outputType' => $type])]);
  58. $this::assertStringContainsString($expected, $this->qrcode->render('test'));
  59. }
  60. public function testInitDataInterfaceException():void{
  61. $this->expectException(QRCodeOutputException::class);
  62. $this->expectExceptionMessage('invalid output type');
  63. (new QRCode(new QROptions(['outputType' => 'foo'])))->render('test');
  64. }
  65. public function testGetMatrixException():void{
  66. $this->expectException(QRCodeDataException::class);
  67. $this->expectExceptionMessage('QRCode::getMatrix() No data given.');
  68. $this->qrcode->getMatrix('');
  69. }
  70. public function testTrim():void{
  71. $m1 = $this->qrcode->getMatrix('hello');
  72. $m2 = $this->qrcode->getMatrix('hello '); // added space
  73. $this::assertNotEquals($m1, $m2);
  74. }
  75. public function testImageTransparencyBGDefault():void{
  76. $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['imageTransparencyBG' => ['foo']])]);
  77. $this::assertSame([255,255,255], $this->getProperty('options')->getValue($this->qrcode)->imageTransparencyBG);
  78. }
  79. public function testCustomOutput():void{
  80. $options = new QROptions([
  81. 'version' => 5,
  82. 'eccLevel' => QRCode::ECC_L,
  83. 'outputType' => QRCode::OUTPUT_CUSTOM,
  84. 'outputInterface' => MyCustomOutput::class,
  85. ]);
  86. $expected = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110111010000101111010000011111110000000010000010111000001101011000001010000010000000010111010101101011000001101011010111010000000010111010110100111110010100111010111010000000010111010000001101011000001101010111010000000010000010100111110010100111110010000010000000011111110101010101010101010101011111110000000000000000010010100111110010100000000000000000011001110000101111010000101111001011110000000000000000111010000101111010000111100010000000001011010100111110010100111110011001010000000010000101111101011000001101011110011110000000000011010100011000001101011000101110100000000011001100001001101011000001101010011010000000010110111110000001101011000001100110100000000010000100100010100111110010100001100100000000011111110111101111010000101111010100110000000011010000111010000101111010000111100100000000010101111111111110010100111110011001000000000010110001110101011000001101011110011010000000001001111100011000001101011000101110010000000011000100110001101011000001101010011100000000001000011001000001101011000001100110000000000011101001011010100111110010100001100000000000010111010001101111010000101111010100110000000011100000001010000101111010000111100000000000000001110110111110010100111110011001000000000000011001011101011000001101011110011100000000011111110101011000001101011001111110110000000000000000110001101011000001101000111100000000011111110001000001101011000011010110000000000010000010101010100111110010101000100100000000010111010111101111010000101111111100110000000010111010011010000101111010001101100010000000010111010000111110010100111100101101100000000010000010101101011000001101001100111100000000011111110101011000001101011000110010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
  87. $this::assertSame($expected, $this->reflection->newInstanceArgs([$options])->render('test'));
  88. }
  89. public function testDataModeOverride():void{
  90. $this->qrcode = $this->reflection->newInstance();
  91. $this::assertInstanceOf(Number::class, $this->qrcode->initDataInterface('123'));
  92. $this::assertInstanceOf(AlphaNum::class, $this->qrcode->initDataInterface('ABC123'));
  93. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface(random_bytes(32)));
  94. $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['dataMode' => 'Byte'])]);
  95. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('123'));
  96. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('ABC123'));
  97. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface(random_bytes(32)));
  98. }
  99. public function testDataModeOverrideError():void{
  100. $this->expectException(QRCodeDataException::class);
  101. $this->expectExceptionMessage('illegal char:');
  102. $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['dataMode' => 'AlphaNum'])]);
  103. $this->qrcode->initDataInterface(random_bytes(32));
  104. }
  105. }