QRCodeTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, Kanji, Number, QRCodeDataException};
  15. use chillerlan\QRCode\Output\QRCodeOutputException;
  16. use PHPUnit\Framework\TestCase;
  17. use function random_bytes;
  18. /**
  19. * Tests basic functions of the QRCode class
  20. */
  21. class QRCodeTest extends TestCase{
  22. /** @internal */
  23. protected QRCode $qrcode;
  24. /** @internal */
  25. protected QROptions $options;
  26. /**
  27. * invoke test instances
  28. *
  29. * @internal
  30. */
  31. protected function setUp():void{
  32. $this->qrcode = new QRCode;
  33. $this->options = new QROptions;
  34. }
  35. /**
  36. * isNumber() should pass on any number and fail on anything else
  37. */
  38. public function testIsNumber():void{
  39. $this::assertTrue($this->qrcode->isNumber('0123456789'));
  40. $this::assertFalse($this->qrcode->isNumber('ABC123'));
  41. }
  42. /**
  43. * isAlphaNum() should pass on the 45 defined characters and fail on anything else (e.g. lowercase)
  44. */
  45. public function testIsAlphaNum():void{
  46. $this::assertTrue($this->qrcode->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'));
  47. $this::assertFalse($this->qrcode->isAlphaNum('abc'));
  48. }
  49. /**
  50. * isKanji() should pass on Kanji/SJIS characters and fail on everything else
  51. */
  52. public function testIsKanji():void{
  53. $this::assertTrue($this->qrcode->isKanji('茗荷'));
  54. $this::assertFalse($this->qrcode->isKanji('Ã'));
  55. $this::assertFalse($this->qrcode->isKanji('ABC'));
  56. $this::assertFalse($this->qrcode->isKanji('123'));
  57. }
  58. /**
  59. * isByte() passses any binary string and only fails on empty strings
  60. */
  61. public function testIsByte():void{
  62. $this::assertTrue($this->qrcode->isByte("\x01\x02\x03"));
  63. $this::assertTrue($this->qrcode->isByte(' ')); // not empty!
  64. $this::assertFalse($this->qrcode->isByte(''));
  65. }
  66. /**
  67. * tests if an exception is thrown when an invalid (built-in) output type is specified
  68. */
  69. public function testInitDataInterfaceException():void{
  70. $this->expectException(QRCodeOutputException::class);
  71. $this->expectExceptionMessage('invalid output type');
  72. $this->options->outputType = 'foo';
  73. (new QRCode($this->options))->render('test');
  74. }
  75. /**
  76. * tests if an exception is thrown when trying to call getMatrix() without data (empty string, no data set)
  77. */
  78. public function testGetMatrixException():void{
  79. $this->expectException(QRCodeDataException::class);
  80. $this->expectExceptionMessage('QRCode::getMatrix() No data given.');
  81. $this->qrcode->getMatrix('');
  82. }
  83. /**
  84. * test whether stings are trimmed (they are not) - i'm still torn on that (see isByte)
  85. */
  86. public function testAvoidTrimming():void{
  87. $m1 = $this->qrcode->getMatrix('hello')->matrix();
  88. $m2 = $this->qrcode->getMatrix('hello ')->matrix(); // added space
  89. $this::assertNotSame($m1, $m2);
  90. }
  91. /**
  92. * tests if the data mode is overriden if QROptions::$dataModeOverride is set to a valid value
  93. *
  94. * @see https://github.com/chillerlan/php-qrcode/issues/39
  95. */
  96. public function testDataModeOverride():void{
  97. // no (or invalid) value set - auto detection
  98. $this->options->dataModeOverride = 'foo';
  99. $this->qrcode = new QRCode;
  100. $this::assertInstanceOf(Number::class, $this->qrcode->initDataInterface('123'));
  101. $this::assertInstanceOf(AlphaNum::class, $this->qrcode->initDataInterface('ABC123'));
  102. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface(random_bytes(32)));
  103. $this::assertInstanceOf(Kanji::class, $this->qrcode->initDataInterface('茗荷'));
  104. // data mode set: force the given data mode
  105. $this->options->dataModeOverride = 'Byte';
  106. $this->qrcode = new QRCode($this->options);
  107. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('123'));
  108. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('ABC123'));
  109. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface(random_bytes(32)));
  110. $this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('茗荷'));
  111. }
  112. /**
  113. * tests if an exception is thrown when an invalid character occurs when forcing a data mode other than Byte
  114. */
  115. public function testDataModeOverrideError():void{
  116. $this->expectException(QRCodeDataException::class);
  117. $this->expectExceptionMessage('illegal char:');
  118. $this->options->dataModeOverride = 'AlphaNum';
  119. (new QRCode($this->options))->initDataInterface(random_bytes(32));
  120. }
  121. }