QRCodeTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. class QRCodeTest extends \PHPUnit_Framework_TestCase{
  17. /**
  18. * @var \chillerlan\QRCode\QROptions
  19. */
  20. protected $options;
  21. /**
  22. * @var \chillerlan\QRCode\Output\QROutputInterface
  23. */
  24. protected $output;
  25. /**
  26. * @var \ReflectionClass
  27. */
  28. protected $reflectionClass;
  29. protected function setUp(){
  30. $this->options = new QROptions;
  31. $this->output = new QRString;
  32. $this->reflectionClass = new ReflectionClass(QRCode::class);
  33. }
  34. public function testInstance(){
  35. $this->assertInstanceOf(QRString::class, $this->output);
  36. $this->assertInstanceOf(QROptions::class, $this->options);
  37. $this->assertInstanceOf(QRCode::class, $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]));
  38. }
  39. public function stringDataProvider(){
  40. return [
  41. ['1234567890'],
  42. ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  43. ['#\\'],
  44. ['茗荷'],
  45. ];
  46. }
  47. /**
  48. * @dataProvider stringDataProvider
  49. */
  50. public function testDataCoverage($data){
  51. (new QRCode($data, $this->output))->getRawData();
  52. }
  53. /**
  54. * @dataProvider stringDataProvider
  55. */
  56. public function testTypeAndErrorcorrectlevelCoverage($data){
  57. foreach(QRConst::MAX_BITS as $type => $x){
  58. foreach(QRConst::RSBLOCK as $eclevel => $y){
  59. $this->options->typeNumber = $type;
  60. $this->options->errorCorrectLevel = $eclevel;
  61. $this->assertInstanceOf(QRCode::class, new QRCode($data, $this->output, $this->options));
  62. }
  63. }
  64. }
  65. /**
  66. * @dataProvider stringDataProvider
  67. */
  68. public function testTypeAutoOverride($data){
  69. $this->options->typeNumber = QRCode::TYPE_05;
  70. new QRCode($data, $this->output, $this->options);
  71. }
  72. public function getTypeNumberDataProvider(){
  73. return [
  74. [true, QRCode::TYPE_05, 'foobar'],
  75. [false, QRCode::TYPE_05, 'foobar'],
  76. [true, QRCode::TYPE_10, 'foobar'],
  77. [false, QRCode::TYPE_10, 'foobar'],
  78. [true, QRCode::TYPE_05, '1234567890'],
  79. [false, QRCode::TYPE_05, '1234567890'],
  80. [true, QRCode::TYPE_10, '1234567890'],
  81. [false, QRCode::TYPE_10, '1234567890'],
  82. [true, QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  83. [false, QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  84. [true, QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  85. [false, QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  86. [true, QRCode::TYPE_05, '茗荷'],
  87. [false, QRCode::TYPE_05, '茗荷'],
  88. [true, QRCode::TYPE_10, '茗荷'],
  89. [false, QRCode::TYPE_10, '茗荷'],
  90. ];
  91. }
  92. /**
  93. * @dataProvider getTypeNumberDataProvider
  94. */
  95. public function testInternalGetTypeNumber($test, $type, $data){
  96. $method = $this->reflectionClass->getMethod('getMatrix');
  97. $method->setAccessible(true);
  98. $this->options->typeNumber = $type;
  99. for($i = 0; $i <= 7; $i++){
  100. $method->invokeArgs($this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options]), [$test, $i]);
  101. }
  102. }
  103. /**
  104. * @expectedException \chillerlan\QRCode\QRCodeException
  105. * @expectedExceptionMessage No data given.
  106. */
  107. public function testNoDataException(){
  108. $this->reflectionClass->newInstanceArgs(['', $this->output]);
  109. }
  110. /**
  111. * @expectedException \chillerlan\QRCode\QRCodeException
  112. * @expectedExceptionMessage Invalid error correct level: 42
  113. */
  114. public function testErrorCorrectLevelException(){
  115. $this->options->errorCorrectLevel = 42;
  116. $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]);
  117. }
  118. /**
  119. * @expectedException \chillerlan\QRCode\QRCodeException
  120. * @expectedExceptionMessage code length overflow. (261 > 72bit)
  121. */
  122. public function testCodeLengthOverflowException(){
  123. $this->options->typeNumber = QRCode::TYPE_01;
  124. $this->options->errorCorrectLevel = QRCode::ERROR_CORRECT_LEVEL_H;
  125. $method = $this->reflectionClass->getMethod('getRawData');
  126. $method->invoke($this->reflectionClass->newInstanceArgs(['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', $this->output, $this->options]));
  127. }
  128. }