QRMatrixTest.php 5.9 KB

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