QRCodeTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\GoogleAuth\Authenticator;
  12. use chillerlan\QRCode\Output\{QRImage, QRImageOptions, QRString};
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use ReflectionClass;
  15. use PHPUnit\Framework\TestCase;
  16. class QRCodeTest extends 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', QRCode::TYPE_01],
  42. ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', QRCode::TYPE_03],
  43. ['#\\', QRCode::TYPE_01],
  44. ['茗荷', QRCode::TYPE_01],
  45. ];
  46. }
  47. /**
  48. * @dataProvider stringDataProvider
  49. */
  50. public function testDataCoverage($data){
  51. (new QRCode($data, $this->output))->getRawData();
  52. $this->markTestSkipped('code coverage');
  53. }
  54. /**
  55. * @dataProvider stringDataProvider
  56. */
  57. public function testTypeAndErrorcorrectlevelCoverage($data){
  58. foreach(range(1, 10) as $type){
  59. foreach(range(0, 3) as $eclevel){
  60. $this->options->typeNumber = $type;
  61. $this->options->errorCorrectLevel = $eclevel;
  62. $this->assertInstanceOf(QRCode::class, new QRCode($data, $this->output, $this->options));
  63. }
  64. }
  65. }
  66. /**
  67. * @expectedException \chillerlan\QRCode\QRCodeException
  68. * @expectedExceptionMessage No data given.
  69. */
  70. public function testNoDataException(){
  71. $this->reflectionClass->newInstanceArgs(['', $this->output]);
  72. }
  73. /**
  74. * @expectedException \chillerlan\QRCode\QRCodeException
  75. * @expectedExceptionMessage Invalid error correct level: 42
  76. */
  77. public function testErrorCorrectLevelException(){
  78. $this->options->errorCorrectLevel = 42;
  79. $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]);
  80. }
  81. /**
  82. * @expectedException \chillerlan\QRCode\QRCodeException
  83. * @expectedExceptionMessage code length overflow. (261 > 72bit)
  84. */
  85. public function testCodeLengthOverflowException(){
  86. $this->options->typeNumber = QRCode::TYPE_01;
  87. $this->options->errorCorrectLevel = QRCode::ERROR_CORRECT_LEVEL_H;
  88. $method = $this->reflectionClass->getMethod('getRawData');
  89. $method->invoke($this->reflectionClass->newInstanceArgs(['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', $this->output, $this->options]));
  90. }
  91. public function testAuthenticatorExample(){
  92. $authenticator = new Authenticator;
  93. $data = $authenticator->getUri($authenticator->createSecret(), 'test', 'chillerlan.net');
  94. $qrcode = $this->reflectionClass->newInstanceArgs([$data, new QRImage(new QRImageOptions(['type' => QRCode::OUTPUT_IMAGE_GIF]))]);
  95. $this->markTestSkipped(print_r($qrcode->output(), true));
  96. }
  97. }