QRMatrixTest.php 7.1 KB

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