QRMatrixTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Class QRMatrixTest
  4. *
  5. * @filesource QRMatrixTest.php
  6. * @created 17.11.2017
  7. * @package chillerlan\QRCodeTest\Data
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2017 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCodeTest\Data;
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\QRCode\QRCode;
  15. use chillerlan\QRCodeTest\QRTestAbstract;
  16. class QRMatrixTest extends QRTestAbstract{
  17. protected $FQCN = QRMatrix::class;
  18. protected $version = 7;
  19. /**
  20. * @var \chillerlan\QRCode\Data\QRMatrix
  21. */
  22. protected $matrix;
  23. protected function setUp(){
  24. parent::setUp();
  25. $this->matrix = $this->reflection->newInstanceArgs([$this->version, QRCode::ECC_L]);
  26. }
  27. /**
  28. * @expectedException \chillerlan\QRCode\Data\QRCodeDataException
  29. * @expectedExceptionMessage invalid QR Code version
  30. */
  31. public function testInvalidVersionException(){
  32. $this->reflection->newInstanceArgs([42, 0]);
  33. }
  34. /**
  35. * @expectedException \chillerlan\QRCode\Data\QRCodeDataException
  36. * @expectedExceptionMessage invalid ecc level
  37. */
  38. public function testInvalidEccException(){
  39. $this->reflection->newInstanceArgs([1, 42]);
  40. }
  41. public function testInstance(){
  42. $this->assertInstanceOf($this->FQCN, $this->matrix);
  43. }
  44. public function testSize(){
  45. $this->assertCount($this->matrix->size(), $this->matrix->matrix());
  46. }
  47. public function testVersion(){
  48. $this->assertSame($this->version, $this->matrix->version());
  49. }
  50. public function testECC(){
  51. $this->assertSame(QRCode::ECC_L, $this->matrix->eccLevel());
  52. }
  53. public function testMaskPattern(){
  54. $this->assertSame(-1, $this->matrix->maskPattern());
  55. }
  56. public function testGetSetCheck(){
  57. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  58. $this->assertSame(65280, $this->matrix->get(10, 10));
  59. $this->assertTrue($this->matrix->check(10, 10));
  60. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  61. $this->assertSame(255, $this->matrix->get(20, 20));
  62. $this->assertFalse($this->matrix->check(20, 20));
  63. }
  64. public function testSetDarkModule(){
  65. $this->matrix->setDarkModule();
  66. $this->assertSame(QRMatrix::M_DARKMODULE << 8, $this->matrix->get(8, $this->matrix->size() - 8));
  67. }
  68. public function testSetFinderPattern(){
  69. $this->matrix->setFinderPattern();
  70. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, 0));
  71. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, $this->matrix->size() - 1));
  72. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($this->matrix->size() - 1, 0));
  73. }
  74. public function testSetSeparators(){
  75. $this->matrix->setSeparators();
  76. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(7, 0));
  77. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, 7));
  78. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, $this->matrix->size() - 8));
  79. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get($this->matrix->size() - 8, 0));
  80. }
  81. public function testSetAlignmentPattern(){
  82. $this->matrix
  83. ->setFinderPattern()
  84. ->setAlignmentPattern()
  85. ;
  86. $alignmentPattern = QRMatrix::alignmentPattern[$this->version];
  87. foreach($alignmentPattern as $py){
  88. foreach($alignmentPattern as $px){
  89. if($this->matrix->get($px, $py) === QRMatrix::M_FINDER << 8){
  90. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($px, $py), 'skipped finder pattern');
  91. continue;
  92. }
  93. $this->assertSame(QRMatrix::M_ALIGNMENT << 8, $this->matrix->get($px, $py));
  94. }
  95. }
  96. }
  97. public function testSetTimingPattern(){
  98. $this->matrix
  99. ->setAlignmentPattern()
  100. ->setTimingPattern()
  101. ;
  102. $size = $this->matrix->size();
  103. for($i = 7; $i < $size - 7; $i++){
  104. if($i % 2 === 0){
  105. $p1 = $this->matrix->get(6, $i);
  106. if($p1 === QRMatrix::M_ALIGNMENT << 8){
  107. $this->assertSame(QRMatrix::M_ALIGNMENT << 8, $p1, 'skipped alignment pattern');
  108. continue;
  109. }
  110. $this->assertSame(QRMatrix::M_TIMING << 8, $p1);
  111. $this->assertSame(QRMatrix::M_TIMING << 8, $this->matrix->get($i, 6));
  112. }
  113. }
  114. }
  115. public function testSetVersionNumber(){
  116. $this->matrix->setVersionNumber(true);
  117. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 9, 0));
  118. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 11, 5));
  119. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get(0, $this->matrix->size() - 9));
  120. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get(5, $this->matrix->size() - 11));
  121. }
  122. public function testSetFormatInfo(){
  123. $this->matrix->setFormatInfo(0, true);
  124. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get(8, 0));
  125. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get(0, 8));
  126. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 1, 8));
  127. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 8, 8));
  128. }
  129. public function testSetQuietZone(){
  130. $size = $this->matrix->size();
  131. $q = 5;
  132. $this->matrix->set(0, 0, true, QRMatrix::M_TEST);
  133. $this->matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  134. $this->matrix->setQuietZone($q);
  135. $this->assertCount($size + 2 * $q, $this->matrix->matrix());
  136. $this->assertCount($size + 2 * $q, $this->matrix->matrix()[$size - 1]);
  137. $size = $this->matrix->size();
  138. $this->assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get(0, 0));
  139. $this->assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get($size - 1, $size - 1));
  140. $this->assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($q, $q));
  141. $this->assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($size - 1 - $q, $size - 1 - $q));
  142. }
  143. /**
  144. * @expectedException \chillerlan\QRCode\Data\QRCodeDataException
  145. * @expectedExceptionMessage use only after writing data
  146. */
  147. public function testSetQuietZoneException(){
  148. $this->matrix->setQuietZone();
  149. }
  150. }