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" 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 = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110101011110100001011110011111110000000010000010011111001010011111001010000010000000010111010111000001101011000001010111010000000010111010111010110000011010110010111010000000010111010100110101100000110101010111010000000010000010001101011000001101011010000010000000011111110101010101010101010101011111110000000000000000001010011111001010011000000000000000011110010101111010000101111010100111010000000001110001001011110100001011110110010010000000001100010011111001010011111001011110010000000011010000101000001101011000001011001010000000001101011010010110000011010110100000100000000000001001001110101100000110101101011100000000011100010100101011000001101011001100000000000000001000101100101001111100101111101010000000000111011111010111101000010111101100000000000001111000010000101111010000101101001110000000000100011110001111100101001111101000110000000010001001001101100000110101100110100010000000011100111001001101011000001101111011000000000010110101000000011010110000011011101100000000001111011110000110101100000110100001000000000010111100001111110010100111110100110100000000011001011111100001011110100001011010110000000000100101001101000010111101000000100110000000001011011100010100111110010100110011100000000010010101010011010110000011010000010010000000000111011101100000110101100001111110000000000000000000111011000001101011001000110110000000011111110000110000011010110011010111110000000010000010010010011111001010011000111100000000010111010010111010000101111011111101100000000010111010101011110100001011111100010010000000010111010111111001010011111011101010100000000010000010111000001101011000011001101000000000011111110111010110000011010110111100110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
  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. }