You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 lines
2.9 KiB

  1. <?php
  2. /*
  3. * (c) Jeroen van den Enden <info@endroid.nl>
  4. *
  5. * This source file is subject to the MIT license that is bundled
  6. * with this source code in the file LICENSE.
  7. */
  8. namespace Endroid\Tests\QrCode;
  9. use Endroid\QrCode\Exceptions\ImageFunctionFailedException;
  10. use Endroid\QrCode\Exceptions\ImageFunctionUnknownException;
  11. use Endroid\QrCode\QrCode;
  12. use PHPUnit_Framework_TestCase;
  13. class QrCodeTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var QrCode
  17. */
  18. protected $qrCode;
  19. /**
  20. * Tests if a valid data uri is returned.
  21. */
  22. public function testGetDataUri()
  23. {
  24. $qrCode = $this->getQrCode();
  25. $dataUri = $qrCode->getDataUri();
  26. $this->assertTrue(is_string($dataUri));
  27. }
  28. /**
  29. * Tests if a valid image string is returned.
  30. *
  31. * @throws ImageFunctionFailedException
  32. * @throws ImageFunctionUnknownException
  33. */
  34. public function testGetImageString()
  35. {
  36. $qrCode = $this->getQrCode();
  37. $imageString = $qrCode->get('png');
  38. $this->assertTrue(is_string($imageString));
  39. }
  40. /**
  41. * Tests if a valid image string is returned.
  42. *
  43. * @throws ImageFunctionFailedException
  44. * @throws ImageFunctionUnknownException
  45. */
  46. public function testGetQrCodeWithLogoString()
  47. {
  48. $qrCode = $this->createQrCodeWithLogo();
  49. $imageString = $qrCode->get('png');
  50. $this->assertTrue(is_string($imageString));
  51. }
  52. /**
  53. * For https://github.com/endroid/QrCode/issues/49.
  54. */
  55. public function testRenderHttpAddress()
  56. {
  57. $qrCode = new QrCode();
  58. $qrCode
  59. ->setText('http://www.example.com/it/it/contact/qr/hit/id/1 ')
  60. ->setExtension('png')
  61. ->setSize(300)
  62. ->setPadding(10)
  63. ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
  64. ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
  65. ->setErrorCorrection(QrCode::LEVEL_MEDIUM);
  66. $qrCode->get('png');
  67. }
  68. /**
  69. * Returns a QR code.
  70. */
  71. protected function getQrCode()
  72. {
  73. if (!$this->qrCode) {
  74. $this->qrCode = $this->createQrCode();
  75. }
  76. return $this->qrCode;
  77. }
  78. /**
  79. * Creates a QR code.
  80. *
  81. * @return QrCode
  82. */
  83. protected function createQrCode()
  84. {
  85. $qrCode = new QrCode();
  86. $qrCode->setText('Life is too short to be generating QR codes');
  87. $qrCode->setSize(300);
  88. return $qrCode;
  89. }
  90. /**
  91. * Creates a QR code with a logo.
  92. *
  93. * @return QrCode
  94. */
  95. protected function createQrCodeWithLogo()
  96. {
  97. $qrCode = new QrCode();
  98. $qrCode->setText('Life is too short to be generating QR codes')
  99. ->setSize(300)
  100. ->setLogo(dirname(__DIR__).'/assets/image/logo.png')
  101. ->setLogoSize(60);
  102. return $qrCode;
  103. }
  104. }