QRMatrixTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 $FQCN = QRMatrix::class;
  19. protected $version = 7;
  20. /**
  21. * @link http://www.thonky.com/qr-code-tutorial/format-version-tables
  22. */
  23. const VERSION_REF = [
  24. 7 => '000111110010010100',
  25. 8 => '001000010110111100',
  26. 9 => '001001101010011001',
  27. 10 => '001010010011010011',
  28. 11 => '001011101111110110',
  29. 12 => '001100011101100010',
  30. 13 => '001101100001000111',
  31. 14 => '001110011000001101',
  32. 15 => '001111100100101000',
  33. 16 => '010000101101111000',
  34. 17 => '010001010001011101',
  35. 18 => '010010101000010111',
  36. 19 => '010011010100110010',
  37. 20 => '010100100110100110',
  38. 21 => '010101011010000011',
  39. 22 => '010110100011001001',
  40. 23 => '010111011111101100',
  41. 24 => '011000111011000100',
  42. 25 => '011001000111100001',
  43. 26 => '011010111110101011',
  44. 27 => '011011000010001110',
  45. 28 => '011100110000011010',
  46. 29 => '011101001100111111',
  47. 30 => '011110110101110101',
  48. 31 => '011111001001010000',
  49. 32 => '100000100111010101',
  50. 33 => '100001011011110000',
  51. 34 => '100010100010111010',
  52. 35 => '100011011110011111',
  53. 36 => '100100101100001011',
  54. 37 => '100101010000101110',
  55. 38 => '100110101001100100',
  56. 39 => '100111010101000001',
  57. 40 => '101000110001101001'
  58. ];
  59. /**
  60. * @var \chillerlan\QRCode\Data\QRMatrix
  61. */
  62. protected $matrix;
  63. protected function setUp():void{
  64. parent::setUp();
  65. $this->matrix = $this->reflection->newInstanceArgs([$this->version, QRCode::ECC_L]);
  66. }
  67. public function testInvalidVersionException(){
  68. $this->expectException(QRCodeDataException::class);
  69. $this->expectExceptionMessage('invalid QR Code version');
  70. $this->reflection->newInstanceArgs([42, 0]);
  71. }
  72. public function testInvalidEccException(){
  73. $this->expectException(QRCodeDataException::class);
  74. $this->expectExceptionMessage('invalid ecc level');
  75. $this->reflection->newInstanceArgs([1, 42]);
  76. }
  77. public function testInstance(){
  78. $this->assertInstanceOf($this->FQCN, $this->matrix);
  79. }
  80. public function testSize(){
  81. $this->assertCount($this->matrix->size(), $this->matrix->matrix());
  82. }
  83. public function testVersion(){
  84. $this->assertSame($this->version, $this->matrix->version());
  85. }
  86. public function testVersionPattern() {
  87. foreach (self::VERSION_REF as $version => $mask) {
  88. $hexRef = base_convert(self::VERSION_REF[$version],2 ,16);
  89. $hexImpl = dechex((new ReflectionClass(QRMatrix::class))->getConstant('versionPattern')[$version]);
  90. $this->assertEquals($hexRef, $hexImpl);
  91. }
  92. }
  93. public function testECC(){
  94. $this->assertSame(QRCode::ECC_L, $this->matrix->eccLevel());
  95. }
  96. public function testMaskPattern(){
  97. $this->assertSame(-1, $this->matrix->maskPattern());
  98. }
  99. public function testGetSetCheck(){
  100. $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
  101. $this->assertSame(65280, $this->matrix->get(10, 10));
  102. $this->assertTrue($this->matrix->check(10, 10));
  103. $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
  104. $this->assertSame(255, $this->matrix->get(20, 20));
  105. $this->assertFalse($this->matrix->check(20, 20));
  106. }
  107. public function testSetDarkModule(){
  108. $this->matrix->setDarkModule();
  109. $this->assertSame(QRMatrix::M_DARKMODULE << 8, $this->matrix->get(8, $this->matrix->size() - 8));
  110. }
  111. public function testSetFinderPattern(){
  112. $this->matrix->setFinderPattern();
  113. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, 0));
  114. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, $this->matrix->size() - 1));
  115. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($this->matrix->size() - 1, 0));
  116. }
  117. public function testSetSeparators(){
  118. $this->matrix->setSeparators();
  119. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(7, 0));
  120. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, 7));
  121. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, $this->matrix->size() - 8));
  122. $this->assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get($this->matrix->size() - 8, 0));
  123. }
  124. public function testSetAlignmentPattern(){
  125. $this->matrix
  126. ->setFinderPattern()
  127. ->setAlignmentPattern()
  128. ;
  129. $alignmentPattern = (new ReflectionClass(QRMatrix::class))->getConstant('alignmentPattern')[$this->version];
  130. foreach($alignmentPattern as $py){
  131. foreach($alignmentPattern as $px){
  132. if($this->matrix->get($px, $py) === QRMatrix::M_FINDER << 8){
  133. $this->assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($px, $py), 'skipped finder pattern');
  134. continue;
  135. }
  136. $this->assertSame(QRMatrix::M_ALIGNMENT << 8, $this->matrix->get($px, $py));
  137. }
  138. }
  139. }
  140. public function testSetTimingPattern(){
  141. $this->matrix
  142. ->setAlignmentPattern()
  143. ->setTimingPattern()
  144. ;
  145. $size = $this->matrix->size();
  146. for($i = 7; $i < $size - 7; $i++){
  147. if($i % 2 === 0){
  148. $p1 = $this->matrix->get(6, $i);
  149. if($p1 === QRMatrix::M_ALIGNMENT << 8){
  150. $this->assertSame(QRMatrix::M_ALIGNMENT << 8, $p1, 'skipped alignment pattern');
  151. continue;
  152. }
  153. $this->assertSame(QRMatrix::M_TIMING << 8, $p1);
  154. $this->assertSame(QRMatrix::M_TIMING << 8, $this->matrix->get($i, 6));
  155. }
  156. }
  157. }
  158. public function testSetVersionNumber(){
  159. $this->matrix->setVersionNumber(true);
  160. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 9, 0));
  161. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 11, 5));
  162. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get(0, $this->matrix->size() - 9));
  163. $this->assertSame(QRMatrix::M_VERSION, $this->matrix->get(5, $this->matrix->size() - 11));
  164. }
  165. public function testSetFormatInfo(){
  166. $this->matrix->setFormatInfo(0, true);
  167. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get(8, 0));
  168. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get(0, 8));
  169. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 1, 8));
  170. $this->assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 8, 8));
  171. }
  172. public function testSetQuietZone(){
  173. $size = $this->matrix->size();
  174. $q = 5;
  175. $this->matrix->set(0, 0, true, QRMatrix::M_TEST);
  176. $this->matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
  177. $this->matrix->setQuietZone($q);
  178. $this->assertCount($size + 2 * $q, $this->matrix->matrix());
  179. $this->assertCount($size + 2 * $q, $this->matrix->matrix()[$size - 1]);
  180. $size = $this->matrix->size();
  181. $this->assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get(0, 0));
  182. $this->assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get($size - 1, $size - 1));
  183. $this->assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($q, $q));
  184. $this->assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($size - 1 - $q, $size - 1 - $q));
  185. }
  186. public function testSetQuietZoneException(){
  187. $this->expectException(QRCodeDataException::class);
  188. $this->expectExceptionMessage('use only after writing data');
  189. $this->matrix->setQuietZone();
  190. }
  191. }