QRCodeTest.php 2.5 KB

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