QRCodeTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\QRCode;
  12. use chillerlan\QRCode\QRConst;
  13. use chillerlan\QRCode\QROptions;
  14. use chillerlan\QRCode\Output\QRImage;
  15. use chillerlan\QRCode\Output\QRImageOptions;
  16. use chillerlan\QRCode\Output\QRString;
  17. use chillerlan\QRCode\Output\QRStringOptions;
  18. class QRCodeTest extends \PHPUnit_Framework_TestCase{
  19. /**
  20. * @var \chillerlan\QRCode\QROptions
  21. */
  22. protected $options;
  23. /**
  24. * @var \chillerlan\QRCode\Output\QROutputInterface
  25. */
  26. protected $output;
  27. protected function setUp(){
  28. $this->options = new QROptions;
  29. $this->output = new QRString;
  30. }
  31. public function testInstance(){
  32. $this->assertInstanceOf(QRString::class, $this->output);
  33. $this->assertInstanceOf(QROptions::class, $this->options);
  34. $this->assertInstanceOf(QRCode::class, new QRCode('foobar', $this->output, $this->options));
  35. }
  36. public function stringDataProvider(){
  37. return [
  38. ['1234567890'],
  39. ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'],
  40. ['#\\'],
  41. ['茗荷'],
  42. ];
  43. }
  44. /**
  45. * @dataProvider stringDataProvider
  46. */
  47. public function testDataCoverage($data){
  48. (new QRCode($data, new QRString))->getRawData();
  49. }
  50. /**
  51. * @dataProvider stringDataProvider
  52. */
  53. public function testTypeAndErrorcorrectlevelCoverage($data){
  54. foreach(QRConst::MAX_BITS as $type => $x){
  55. foreach(QRConst::RSBLOCK as $eclevel => $y){
  56. $this->options->typeNumber = $type;
  57. $this->options->errorCorrectLevel = $eclevel;
  58. $this->assertInstanceOf(QRCode::class, new QRCode($data, new QRString, $this->options));
  59. }
  60. }
  61. }
  62. /**
  63. * @dataProvider stringDataProvider
  64. */
  65. public function testTypeAutoOverride($data){
  66. $this->options->typeNumber = QRCode::TYPE_05;
  67. new QRCode($data, new QRString, $this->options);
  68. }
  69. /**
  70. * @expectedException \chillerlan\QRCode\QRCodeException
  71. * @expectedExceptionMessage No data given.
  72. */
  73. public function testNoDataException(){
  74. new QRCode('', new QRString);
  75. }
  76. /**
  77. * @expectedException \chillerlan\QRCode\QRCodeException
  78. * @expectedExceptionMessage Invalid error correct level: 42
  79. */
  80. public function testErrorCorrectLevelException(){
  81. $this->options->errorCorrectLevel = 42;
  82. new QRCode('foobar', new QRString, $this->options);
  83. }
  84. /**
  85. * @expectedException \chillerlan\QRCode\QRCodeException
  86. * @expectedExceptionMessage code length overflow. (261 > 72bit)
  87. */
  88. public function testCodeLengthOverflowException(){
  89. $this->options->typeNumber = QRCode::TYPE_01;
  90. $this->options->errorCorrectLevel = QRCode::ERROR_CORRECT_LEVEL_H;
  91. (new QRCode('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', new QRString, $this->options))->getRawData();
  92. }
  93. }