QRCodeTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\QRCodeDataException;
  15. use chillerlan\QRCode\Output\QRCodeOutputException;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * Tests basic functions of the QRCode class
  19. */
  20. class QRCodeTest extends TestCase{
  21. /** @internal */
  22. protected QRCode $qrcode;
  23. /** @internal */
  24. protected QROptions $options;
  25. /**
  26. * invoke test instances
  27. *
  28. * @internal
  29. */
  30. protected function setUp():void{
  31. $this->qrcode = new QRCode;
  32. $this->options = new QROptions;
  33. }
  34. /**
  35. * isNumber() should pass on any number and fail on anything else
  36. */
  37. public function testIsNumber():void{
  38. $this::assertTrue($this->qrcode->isNumber('0123456789'));
  39. $this::assertFalse($this->qrcode->isNumber('ABC123'));
  40. }
  41. /**
  42. * isAlphaNum() should pass on the 45 defined characters and fail on anything else (e.g. lowercase)
  43. */
  44. public function testIsAlphaNum():void{
  45. $this::assertTrue($this->qrcode->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'));
  46. $this::assertFalse($this->qrcode->isAlphaNum('abc'));
  47. }
  48. /**
  49. * isKanji() should pass on Kanji/SJIS characters and fail on everything else
  50. */
  51. public function testIsKanji():void{
  52. $this::assertTrue($this->qrcode->isKanji('茗荷'));
  53. $this::assertFalse($this->qrcode->isKanji('Ã'));
  54. $this::assertFalse($this->qrcode->isKanji('ABC'));
  55. $this::assertFalse($this->qrcode->isKanji('123'));
  56. }
  57. /**
  58. * isByte() passses any binary string and only fails on empty strings
  59. */
  60. public function testIsByte():void{
  61. $this::assertTrue($this->qrcode->isByte("\x01\x02\x03"));
  62. $this::assertTrue($this->qrcode->isByte(' ')); // not empty!
  63. $this::assertFalse($this->qrcode->isByte(''));
  64. }
  65. /**
  66. * tests if an exception is thrown when an invalid (built-in) output type is specified
  67. */
  68. public function testInitDataInterfaceException():void{
  69. $this->expectException(QRCodeOutputException::class);
  70. $this->expectExceptionMessage('invalid output type');
  71. $this->options->outputType = 'foo';
  72. (new QRCode($this->options))->render('test');
  73. }
  74. /**
  75. * tests if an exception is thrown when trying to call getMatrix() without data (empty string, no data set)
  76. */
  77. public function testGetMatrixException():void{
  78. $this->expectException(QRCodeDataException::class);
  79. $this->expectExceptionMessage('QRCode::getMatrix() No data given.');
  80. $this->qrcode->getMatrix();
  81. }
  82. }