QRCodeTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. *
  4. * @filesource QRCodeTest.php
  5. * @created 08.02.2016
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2015 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest;
  11. use chillerlan\QRCode\Output\QRString;
  12. use chillerlan\QRCode\QRCode;
  13. use chillerlan\QRCode\QRConst;
  14. use chillerlan\QRCode\QROptions;
  15. use ReflectionClass;
  16. use PHPUnit\Framework\TestCase;
  17. class QRCodeTest extends TestCase{
  18. /**
  19. * @var \chillerlan\QRCode\QROptions
  20. */
  21. protected $options;
  22. /**
  23. * @var \chillerlan\QRCode\Output\QROutputInterface
  24. */
  25. protected $output;
  26. /**
  27. * @var \ReflectionClass
  28. */
  29. protected $reflectionClass;
  30. protected function setUp(){
  31. $this->options = new QROptions;
  32. $this->output = new QRString;
  33. $this->reflectionClass = new ReflectionClass(QRCode::class);
  34. }
  35. public function testInstance(){
  36. $this->assertInstanceOf(QRString::class, $this->output);
  37. $this->assertInstanceOf(QROptions::class, $this->options);
  38. $this->assertInstanceOf(QRCode::class, $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]));
  39. }
  40. public function stringDataProvider(){
  41. return [
  42. ['1234567890', QRCode::TYPE_01],
  43. ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', QRCode::TYPE_03],
  44. ['#\\', QRCode::TYPE_01],
  45. ['茗荷', QRCode::TYPE_01],
  46. ];
  47. }
  48. /**
  49. * @dataProvider stringDataProvider
  50. */
  51. public function testDataCoverage($data){
  52. (new QRCode($data, $this->output))->getRawData();
  53. $this->markTestSkipped('code coverage');
  54. }
  55. /**
  56. * @dataProvider stringDataProvider
  57. */
  58. public function testTypeAndErrorcorrectlevelCoverage($data){
  59. foreach(QRConst::MAX_BITS as $type => $x){
  60. foreach(QRConst::RSBLOCK as $eclevel => $y){
  61. $this->options->typeNumber = $type;
  62. $this->options->errorCorrectLevel = $eclevel;
  63. $this->assertInstanceOf(QRCode::class, new QRCode($data, $this->output, $this->options));
  64. }
  65. }
  66. }
  67. /**
  68. * @dataProvider stringDataProvider
  69. */
  70. public function testTypeAutoOverride($data, $type){
  71. $property = $this->reflectionClass->getProperty('typeNumber');
  72. $property->setAccessible(true);
  73. $this->options->typeNumber = 'foo';
  74. $this->assertSame($type, $property->getValue($this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options])));
  75. $this->options->typeNumber = 42;
  76. $this->assertSame($type, $property->getValue($this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options])));
  77. $this->options->typeNumber = QRCode::TYPE_05;
  78. $this->assertSame(QRCode::TYPE_05, $property->getValue($this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options])));
  79. }
  80. public function getMatrixDataProvider(){
  81. return [
  82. [QRCode::TYPE_01, 'foobar', 21],
  83. [QRCode::TYPE_05, 'foobar', 37],
  84. [QRCode::TYPE_10, 'foobar', 57],
  85. [QRCode::TYPE_05, '1234567890', 37],
  86. [QRCode::TYPE_10, '1234567890', 57],
  87. [QRCode::TYPE_03, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 29],
  88. [QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 37],
  89. [QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 57],
  90. [QRCode::TYPE_05, '茗荷', 37],
  91. [QRCode::TYPE_10, '茗荷', 57],
  92. ];
  93. }
  94. /**
  95. * @dataProvider getMatrixDataProvider
  96. */
  97. public function testInternalGetMatrix($type, $data, $pixelCount){
  98. $method = $this->reflectionClass->getMethod('getMatrix');
  99. $method->setAccessible(true);
  100. $property = $this->reflectionClass->getProperty('pixelCount');
  101. $property->setAccessible(true);
  102. $this->options->typeNumber = $type;
  103. for($i = 0; $i <= 7; $i++){
  104. $qrcode = $this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options]);
  105. $method->invokeArgs($qrcode, [false, $i]);
  106. $this->assertSame($pixelCount, $property->getValue($qrcode));
  107. }
  108. }
  109. /**
  110. * @expectedException \chillerlan\QRCode\QRCodeException
  111. * @expectedExceptionMessage No data given.
  112. */
  113. public function testNoDataException(){
  114. $this->reflectionClass->newInstanceArgs(['', $this->output]);
  115. }
  116. /**
  117. * @expectedException \chillerlan\QRCode\QRCodeException
  118. * @expectedExceptionMessage Invalid error correct level: 42
  119. */
  120. public function testErrorCorrectLevelException(){
  121. $this->options->errorCorrectLevel = 42;
  122. $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]);
  123. }
  124. /**
  125. * @expectedException \chillerlan\QRCode\QRCodeException
  126. * @expectedExceptionMessage code length overflow. (261 > 72bit)
  127. */
  128. public function testCodeLengthOverflowException(){
  129. $this->options->typeNumber = QRCode::TYPE_01;
  130. $this->options->errorCorrectLevel = QRCode::ERROR_CORRECT_LEVEL_H;
  131. $method = $this->reflectionClass->getMethod('getRawData');
  132. $method->invoke($this->reflectionClass->newInstanceArgs(['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', $this->output, $this->options]));
  133. }
  134. }