QRCodeTest.php 2.5 KB

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