QRCodeTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  14. use chillerlan\QRCode\QRCode;
  15. use chillerlan\QRCodeExamples\MyCustomOutput;
  16. class QRCodeTest extends QRTestAbstract{
  17. protected $FQCN = QRCode::class;
  18. /**
  19. * @var \chillerlan\QRCode\QRCode
  20. */
  21. protected $qrcode;
  22. protected function setUp(){
  23. parent::setUp();
  24. $this->qrcode = $this->reflection->newInstance();
  25. }
  26. public function testIsNumber(){
  27. $this->assertTrue($this->qrcode->isNumber('0123456789'));
  28. $this->assertFalse($this->qrcode->isNumber('ABC'));
  29. }
  30. public function testIsAlphaNum(){
  31. $this->assertTrue($this->qrcode->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'));
  32. $this->assertFalse($this->qrcode->isAlphaNum('abc'));
  33. }
  34. public function testIsKanji(){
  35. $this->assertTrue($this->qrcode->isKanji('茗荷'));
  36. $this->assertFalse($this->qrcode->isKanji('Ã'));
  37. }
  38. // coverage
  39. public function typeDataProvider(){
  40. return [
  41. [QRCode::OUTPUT_IMAGE_PNG, 'data:image/png;base64,'],
  42. [QRCode::OUTPUT_IMAGE_GIF, 'data:image/gif;base64,'],
  43. [QRCode::OUTPUT_IMAGE_JPG, 'data:image/jpg;base64,'],
  44. [QRCode::OUTPUT_MARKUP_SVG, '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'],
  45. [QRCode::OUTPUT_MARKUP_HTML, '<div><span style="background:'],
  46. [QRCode::OUTPUT_STRING_TEXT, '⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕'.PHP_EOL],
  47. [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],'],
  48. ];
  49. }
  50. /**
  51. * @dataProvider typeDataProvider
  52. * @param $type
  53. */
  54. public function testRenderImage($type, $expected){
  55. $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['outputType' => $type])]);
  56. $this->assertContains($expected, $this->qrcode->render('test'));
  57. }
  58. /**
  59. * @expectedException \chillerlan\QRCode\QRCodeException
  60. * @expectedExceptionMessage Invalid error correct level: 42
  61. */
  62. public function testSetOptionsException(){
  63. $this->qrcode->setOptions(new QROptions(['eccLevel' => 42]));
  64. }
  65. /**
  66. * @expectedException \chillerlan\QRCode\Output\QRCodeOutputException
  67. * @expectedExceptionMessage invalid output type
  68. */
  69. public function testInitDataInterfaceException(){
  70. $this->qrcode->setOptions(new QROptions(['outputType' => 'foo']))->render('test');
  71. }
  72. /**
  73. * @expectedException \chillerlan\QRCode\Data\QRCodeDataException
  74. * @expectedExceptionMessage QRCode::getMatrix() No data given.
  75. */
  76. public function testGetMatrixException(){
  77. $this->qrcode->getMatrix('');
  78. }
  79. public function testTrim() {
  80. $m1 = $this->qrcode->getMatrix('hello');
  81. $m2 = $this->qrcode->getMatrix('hello '); // added space
  82. $this->assertNotEquals($m1, $m2);
  83. }
  84. public function testImageTransparencyBGDefault(){
  85. $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['imageTransparencyBG' => 'foo'])]);
  86. $this->assertSame([255,255,255], $this->getProperty('options')->getValue($this->qrcode)->imageTransparencyBG);
  87. }
  88. public function testCustomOutput(){
  89. $options = new QROptions([
  90. 'version' => 5,
  91. 'eccLevel' => QRCode::ECC_L,
  92. 'outputType' => QRCode::OUTPUT_CUSTOM,
  93. 'outputInterface' => MyCustomOutput::class,
  94. ]);
  95. $expected = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110101011110100001011110011111110000000010000010011111001010011111001010000010000000010111010111000001101011000001010111010000000010111010111010110000011010110010111010000000010111010100110101100000110101010111010000000010000010001101011000001101011010000010000000011111110101010101010101010101011111110000000000000000001010011111001010011000000000000000011110010101111010000101111010100111010000000001110001001011110100001011110110010010000000001100010011111001010011111001011110010000000011010000101000001101011000001011001010000000001101011010010110000011010110100000100000000000001001001110101100000110101101011100000000011100010100101011000001101011001100000000000000001000101100101001111100101111101010000000000111011111010111101000010111101100000000000001111000010000101111010000101101001110000000000100011110001111100101001111101000110000000010001001001101100000110101100110100010000000011100111001001101011000001101111011000000000010110101000000011010110000011011101100000000001111011110000110101100000110100001000000000010111100001111110010100111110100110100000000011001011111100001011110100001011010110000000000100101001101000010111101000000100110000000001011011100010100111110010100110011100000000010010101010011010110000011010000010010000000000111011101100000110101100001111110000000000000000000111011000001101011001000110110000000011111110000110000011010110011010111110000000010000010010010011111001010011000111100000000010111010010111010000101111011111101100000000010111010101011110100001011111100010010000000010111010111111001010011111011101010100000000010000010111000001101011000011001101000000000011111110111010110000011010110111100110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
  96. $this->assertSame($expected, $this->reflection->newInstanceArgs([$options])->render('test'));
  97. }
  98. }