QrCodeTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * (c) Jeroen van den Enden <info@endroid.nl>
  5. *
  6. * This source file is subject to the MIT license that is bundled
  7. * with this source code in the file LICENSE.
  8. */
  9. namespace Endroid\QrCode\Tests;
  10. use Endroid\QrCode\Exception\GenerateImageException;
  11. use Endroid\QrCode\Factory\QrCodeFactory;
  12. use Endroid\QrCode\QrCode;
  13. use PHPUnit\Framework\TestCase;
  14. use Zxing\QrReader;
  15. class QrCodeTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider stringProvider
  19. * @testdox QR code created with text $text is readable
  20. */
  21. public function testReadable(string $text): void
  22. {
  23. $qrCode = new QrCode();
  24. $qrCode->setSize(300);
  25. $qrCode->setText($text);
  26. $pngData = $qrCode->writeString();
  27. $this->assertTrue(is_string($pngData));
  28. $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
  29. $this->assertEquals($text, $reader->text());
  30. }
  31. public function stringProvider(): array
  32. {
  33. return [
  34. ['Tiny'],
  35. ['This one has spaces'],
  36. ['d2llMS9uU01BVmlvalM2YU9BUFBPTTdQMmJabHpqdndt'],
  37. ['http://this.is.an/url?with=query&string=attached'],
  38. ['11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'],
  39. ['{"i":"serialized.data","v":1,"t":1,"d":"4AEPc9XuIQ0OjsZoSRWp9DRWlN6UyDvuMlyOYy8XjOw="}'],
  40. ['Spëci&al ch@ract3rs'],
  41. ['有限公司'],
  42. ];
  43. }
  44. /**
  45. * @dataProvider writerNameProvider
  46. * @testdox Writer set by name $writerName results in the correct data type
  47. */
  48. public function testWriteQrCodeByWriterName(string $writerName, ?string $fileContent): void
  49. {
  50. $qrCode = new QrCode('QR Code');
  51. $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
  52. $qrCode->setLogoWidth(100);
  53. $qrCode->setWriterByName($writerName);
  54. $data = $qrCode->writeString();
  55. $this->assertTrue(is_string($data));
  56. if (null !== $fileContent) {
  57. $uriData = $qrCode->writeDataUri();
  58. $this->assertTrue(0 === strpos($uriData, $fileContent));
  59. }
  60. }
  61. public function writerNameProvider(): array
  62. {
  63. return [
  64. ['binary', null],
  65. ['debug', null],
  66. ['eps', null],
  67. ['png', 'data:image/png;base64'],
  68. ['svg', 'data:image/svg+xml;base64'],
  69. ];
  70. }
  71. /**
  72. * @dataProvider extensionsProvider
  73. * @testdox Writer set by extension $extension results in the correct data type
  74. */
  75. public function testWriteQrCodeByWriterExtension(string $extension, ?string $fileContent): void
  76. {
  77. $qrCode = new QrCode('QR Code');
  78. $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
  79. $qrCode->setLogoWidth(100);
  80. $qrCode->setWriterByExtension($extension);
  81. $data = $qrCode->writeString();
  82. $this->assertTrue(is_string($data));
  83. if (null !== $fileContent) {
  84. $uriData = $qrCode->writeDataUri();
  85. $this->assertTrue(0 === strpos($uriData, $fileContent));
  86. }
  87. }
  88. public function extensionsProvider(): array
  89. {
  90. return [
  91. ['bin', null],
  92. ['txt', null],
  93. ['eps', null],
  94. ['png', 'data:image/png;base64'],
  95. ['svg', 'data:image/svg+xml;base64'],
  96. ];
  97. }
  98. /**
  99. * @testdox Factory creates a valid QR code
  100. */
  101. public function testFactory(): void
  102. {
  103. $qrCodeFactory = new QrCodeFactory();
  104. $qrCode = $qrCodeFactory->create('QR Code', [
  105. 'writer' => 'png',
  106. 'size' => 300,
  107. 'margin' => 10,
  108. ]);
  109. $pngData = $qrCode->writeString();
  110. $this->assertTrue(is_string($pngData));
  111. $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
  112. $this->assertEquals('QR Code', $reader->text());
  113. }
  114. /**
  115. * @testdox Size and margin are handled correctly
  116. */
  117. public function testSetSize(): void
  118. {
  119. $size = 400;
  120. $margin = 10;
  121. $qrCode = new QrCode('QR Code');
  122. $qrCode->setSize($size);
  123. $qrCode->setMargin($margin);
  124. $pngData = $qrCode->writeString();
  125. $image = imagecreatefromstring($pngData);
  126. $this->assertTrue(imagesx($image) === $size + 2 * $margin);
  127. $this->assertTrue(imagesy($image) === $size + 2 * $margin);
  128. }
  129. /**
  130. * @testdox Label can be added and QR code is still readable
  131. */
  132. public function testSetLabel(): void
  133. {
  134. $qrCode = new QrCode('QR Code');
  135. $qrCode->setSize(300);
  136. $qrCode->setLabel('Scan the code', 15);
  137. $pngData = $qrCode->writeString();
  138. $this->assertTrue(is_string($pngData));
  139. $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
  140. $this->assertEquals('QR Code', $reader->text());
  141. }
  142. /**
  143. * @testdox Logo can be added and QR code is still readable
  144. */
  145. public function testSetLogo(): void
  146. {
  147. $qrCode = new QrCode('QR Code');
  148. $qrCode->setSize(500);
  149. $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
  150. $qrCode->setLogoWidth(100);
  151. $qrCode->setValidateResult(true);
  152. $pngData = $qrCode->writeString();
  153. $this->assertTrue(is_string($pngData));
  154. }
  155. /**
  156. * @testdox Resulting QR code can be written to file
  157. */
  158. public function testWriteFile(): void
  159. {
  160. $filename = __DIR__.'/output/qr-code.png';
  161. $qrCode = new QrCode('QR Code');
  162. $qrCode->writeFile($filename);
  163. $image = imagecreatefromstring(file_get_contents($filename));
  164. $this->assertTrue(is_resource($image));
  165. imagedestroy($image);
  166. }
  167. /**
  168. * @testdox QR code data can be retrieved
  169. */
  170. public function testData(): void
  171. {
  172. $qrCode = new QrCode('QR Code');
  173. $data = $qrCode->getData();
  174. $this->assertArrayHasKey('block_count', $data);
  175. $this->assertArrayHasKey('block_size', $data);
  176. $this->assertArrayHasKey('inner_width', $data);
  177. $this->assertArrayHasKey('inner_height', $data);
  178. $this->assertArrayHasKey('outer_width', $data);
  179. $this->assertArrayHasKey('outer_height', $data);
  180. $this->assertArrayHasKey('margin_left', $data);
  181. $this->assertArrayHasKey('margin_right', $data);
  182. }
  183. /**
  184. * @testdox Invalid image data results in appropriate exception
  185. */
  186. public function testNonImageData(): void
  187. {
  188. $qrCode = new QrCode('QR Code');
  189. $qrCode->setLogoPath(__DIR__.'/QrCodeTest.php');
  190. $qrCode->setLogoSize(200, 200);
  191. $qrCode->setWriterByExtension('svg');
  192. $this->expectException(GenerateImageException::class);
  193. $qrCode->writeString();
  194. }
  195. }