QRDataGeneratorTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Class QRDataGeneratorTest
  4. *
  5. * @filesource QRDataGeneratorTest.php
  6. * @created 24.10.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\Data\AlphaNum;
  14. use chillerlan\QRCode\Data\QRDataInterface;
  15. use chillerlan\QRCode\Output\QRString;
  16. use chillerlan\QRCode\QRCode;
  17. use chillerlan\QRCode\QRDataGenerator;
  18. use chillerlan\QRCode\QROptions;
  19. use PHPUnit\Framework\TestCase;
  20. use ReflectionClass;
  21. use ReflectionMethod;
  22. use ReflectionProperty;
  23. class QRDataGeneratorTest extends TestCase{
  24. /**
  25. * @var \chillerlan\QRCode\QRDataGenerator
  26. */
  27. protected $qrTest;
  28. /**
  29. * @var \ReflectionClass
  30. */
  31. protected $reflectionClass;
  32. protected function setUp(){
  33. $this->reflectionClass = new ReflectionClass(QRDataGenerator::class);
  34. $this->qrTest = new QRDataGenerator('test', QRCode::TYPE_05, QRCode::ERROR_CORRECT_LEVEL_L);
  35. }
  36. private function getMethod(string $method):ReflectionMethod {
  37. $method = $this->reflectionClass->getMethod($method);
  38. $method->setAccessible(true);
  39. return $method;
  40. }
  41. private function getProperty(string $property):ReflectionProperty{
  42. $property = $this->reflectionClass->getProperty($property);
  43. $property->setAccessible(true);
  44. return $property;
  45. }
  46. public function stringDataProvider(){
  47. return [
  48. ['1234567890', QRCode::TYPE_01],
  49. ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', QRCode::TYPE_03],
  50. ['#\\', QRCode::TYPE_01],
  51. ['茗荷', QRCode::TYPE_01],
  52. ];
  53. }
  54. /**
  55. * @dataProvider stringDataProvider
  56. */
  57. public function testTypeAutoOverride($data, $type){
  58. $qrcode = $this->reflectionClass->newInstanceArgs([$data, $type, QRCode::ERROR_CORRECT_LEVEL_L]);
  59. $this->assertSame($type, $this->getProperty('typeNumber')->getValue($qrcode));
  60. }
  61. public function getMatrixDataProvider(){
  62. return [
  63. [QRCode::TYPE_01, 'foobar', 21],
  64. [QRCode::TYPE_05, 'foobar', 37],
  65. [QRCode::TYPE_10, 'foobar', 57],
  66. [QRCode::TYPE_05, '1234567890', 37],
  67. [QRCode::TYPE_10, '1234567890', 57],
  68. [QRCode::TYPE_03, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 29],
  69. [QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 37],
  70. [QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 57],
  71. [QRCode::TYPE_05, '茗荷', 37],
  72. [QRCode::TYPE_10, '茗荷', 57],
  73. ];
  74. }
  75. /**
  76. * @dataProvider getMatrixDataProvider
  77. */
  78. public function testInternalGetMatrix($type, $data, $pixelCount){
  79. $method = $this->getMethod('getMatrix');
  80. $property = $this->getProperty('pixelCount');
  81. for($i = 0; $i <= 7; $i++){
  82. $qrcode = $this->reflectionClass->newInstanceArgs([$data, $type, QRCode::ERROR_CORRECT_LEVEL_L]);
  83. $method->invokeArgs($qrcode, [false, $i]);
  84. $this->assertSame($pixelCount, $property->getValue($qrcode));
  85. }
  86. }
  87. public function testIsNumber(){
  88. $this->assertTrue($this->qrTest->isNumber('1234567890'));
  89. $this->assertFalse($this->qrTest->isNumber('abc'));
  90. }
  91. public function testIsAlphaNum(){
  92. $this->assertTrue($this->qrTest->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'));
  93. $this->assertFalse($this->qrTest->isAlphaNum('#'));
  94. }
  95. // http://stackoverflow.com/a/24755772
  96. public function testIsKanji(){
  97. $this->assertTrue($this->qrTest->isKanji('茗荷'));
  98. $this->assertFalse($this->qrTest->isKanji(''));
  99. $this->assertFalse($this->qrTest->isKanji('ÃÃÃ')); // non-kanji
  100. $this->assertFalse($this->qrTest->isKanji('荷')); // kanji forced into byte mode due to length
  101. }
  102. // coverage
  103. public function testGetBCHTypeNumber(){
  104. $this->assertSame(7973, $this->getMethod('getBCHTypeNumber')->invokeArgs($this->qrTest, [1]));
  105. }
  106. /**
  107. * @expectedException \chillerlan\QRCode\QRCodeException
  108. * @expectedExceptionMessage $typeNumber: 1 / $errorCorrectLevel: 42
  109. */
  110. public function testGetRSBlocksException(){
  111. $this->getMethod('getRSBlocks')->invokeArgs($this->qrTest, [1, 42]);
  112. }
  113. /**
  114. * @expectedException \chillerlan\QRCode\QRCodeException
  115. * @expectedExceptionMessage Invalid error correct level: 42
  116. */
  117. public function testGetMaxLengthECLevelException(){
  118. $this->getMethod('getMaxLength')->invokeArgs($this->qrTest, [QRCode::TYPE_01, QRDataInterface::MODE_BYTE, 42]);
  119. }
  120. /**
  121. * @expectedException \chillerlan\QRCode\QRCodeException
  122. * @expectedExceptionMessage Invalid mode: 1337
  123. */
  124. public function testGetMaxLengthModeException(){
  125. $this->getMethod('getMaxLength')->invokeArgs($this->qrTest, [QRCode::TYPE_01, 1337, QRCode::ERROR_CORRECT_LEVEL_H]);
  126. }
  127. public function getTypeNumberDataProvider(){
  128. return [
  129. [QRDataInterface::MODE_ALPHANUM, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 2],
  130. [QRDataInterface::MODE_BYTE, '#\\', 1],
  131. [QRDataInterface::MODE_KANJI, '茗荷', 1],
  132. [QRDataInterface::MODE_NUMBER, '1234567890', 1],
  133. ];
  134. }
  135. /**
  136. * @dataProvider getTypeNumberDataProvider
  137. */
  138. public function testGetTypeNumber($mode, $data, $expected){
  139. $i = QRDataGenerator::DATA_INTERFACES[$mode];
  140. $this->assertSame($expected, $this->getMethod('getTypeNumber')->invokeArgs($this->qrTest, [new $i($data), $mode]));
  141. }
  142. }