Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

QrCodeTest.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. 'round_block_size_mode' => 'shrink',
  109. ]);
  110. $pngData = $qrCode->writeString();
  111. $this->assertTrue(is_string($pngData));
  112. $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
  113. $this->assertEquals('QR Code', $reader->text());
  114. }
  115. /**
  116. * @testdox Size and margin are handled correctly
  117. */
  118. public function testSetSize(): void
  119. {
  120. $size = 400;
  121. $margin = 10;
  122. $qrCode = new QrCode('QR Code');
  123. $qrCode->setSize($size);
  124. $qrCode->setMargin($margin);
  125. $pngData = $qrCode->writeString();
  126. $image = imagecreatefromstring($pngData);
  127. $this->assertTrue(imagesx($image) === $size + 2 * $margin);
  128. $this->assertTrue(imagesy($image) === $size + 2 * $margin);
  129. }
  130. /**
  131. * @testdox Size and margin are handled correctly with rounded blocks
  132. * @dataProvider roundedSizeProvider
  133. */
  134. public function testSetSizeRounded($size, $margin, $round, $mode, $expectedSize): void
  135. {
  136. $qrCode = new QrCode('QR Code contents with some length to have some data');
  137. $qrCode->setRoundBlockSize($round, $mode);
  138. $qrCode->setSize($size);
  139. $qrCode->setMargin($margin);
  140. $pngData = $qrCode->writeString();
  141. $image = imagecreatefromstring($pngData);
  142. $this->assertTrue(imagesx($image) === $expectedSize);
  143. $this->assertTrue(imagesy($image) === $expectedSize);
  144. }
  145. public function roundedSizeProvider()
  146. {
  147. return [
  148. [
  149. 'size' => 400,
  150. 'margin' => 0,
  151. 'round' => true,
  152. 'mode' => QrCode::ROUND_BLOCK_SIZE_MODE_ENLARGE,
  153. 'expectedSize' => 406
  154. ],
  155. [
  156. 'size' => 400,
  157. 'margin' => 5,
  158. 'round' => true,
  159. 'mode' => QrCode::ROUND_BLOCK_SIZE_MODE_ENLARGE,
  160. 'expectedSize' => 416
  161. ],
  162. [
  163. 'size' => 400,
  164. 'margin' => 0,
  165. 'round' => true,
  166. 'mode' => QrCode::ROUND_BLOCK_SIZE_MODE_MARGIN,
  167. 'expectedSize' => 400
  168. ],
  169. [
  170. 'size' => 400,
  171. 'margin' => 5,
  172. 'round' => true,
  173. 'mode' => QrCode::ROUND_BLOCK_SIZE_MODE_MARGIN,
  174. 'expectedSize' => 410
  175. ],
  176. [
  177. 'size' => 400,
  178. 'margin' => 0,
  179. 'round' => true,
  180. 'mode' => QrCode::ROUND_BLOCK_SIZE_MODE_SHRINK,
  181. 'expectedSize' => 377
  182. ],
  183. [
  184. 'size' => 400,
  185. 'margin' => 5,
  186. 'round' => true,
  187. 'mode' => QrCode::ROUND_BLOCK_SIZE_MODE_SHRINK,
  188. 'expectedSize' => 387
  189. ],
  190. ];
  191. }
  192. /**
  193. * @testdox Label can be added and QR code is still readable
  194. */
  195. public function testSetLabel(): void
  196. {
  197. $qrCode = new QrCode('QR Code');
  198. $qrCode->setSize(300);
  199. $qrCode->setLabel('Scan the code', 15);
  200. $pngData = $qrCode->writeString();
  201. $this->assertTrue(is_string($pngData));
  202. $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
  203. $this->assertEquals('QR Code', $reader->text());
  204. }
  205. /**
  206. * @testdox Logo can be added and QR code is still readable
  207. */
  208. public function testSetLogo(): void
  209. {
  210. $qrCode = new QrCode('QR Code');
  211. $qrCode->setSize(500);
  212. $qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
  213. $qrCode->setLogoWidth(100);
  214. $qrCode->setValidateResult(true);
  215. $pngData = $qrCode->writeString();
  216. $this->assertTrue(is_string($pngData));
  217. }
  218. /**
  219. * @testdox Resulting QR code can be written to file
  220. */
  221. public function testWriteFile(): void
  222. {
  223. $filename = __DIR__.'/output/qr-code.png';
  224. $qrCode = new QrCode('QR Code');
  225. $qrCode->writeFile($filename);
  226. $image = imagecreatefromstring(file_get_contents($filename));
  227. $this->assertTrue(is_resource($image));
  228. imagedestroy($image);
  229. }
  230. /**
  231. * @testdox QR code data can be retrieved
  232. */
  233. public function testData(): void
  234. {
  235. $qrCode = new QrCode('QR Code');
  236. $data = $qrCode->getData();
  237. $this->assertArrayHasKey('block_count', $data);
  238. $this->assertArrayHasKey('block_size', $data);
  239. $this->assertArrayHasKey('inner_width', $data);
  240. $this->assertArrayHasKey('inner_height', $data);
  241. $this->assertArrayHasKey('outer_width', $data);
  242. $this->assertArrayHasKey('outer_height', $data);
  243. $this->assertArrayHasKey('margin_left', $data);
  244. $this->assertArrayHasKey('margin_right', $data);
  245. }
  246. /**
  247. * @testdox Invalid image data results in appropriate exception
  248. */
  249. public function testNonImageData(): void
  250. {
  251. $qrCode = new QrCode('QR Code');
  252. $qrCode->setLogoPath(__DIR__.'/QrCodeTest.php');
  253. $qrCode->setLogoSize(200, 200);
  254. $qrCode->setWriterByExtension('svg');
  255. $this->expectException(GenerateImageException::class);
  256. $qrCode->writeString();
  257. }
  258. }