QRCodeTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Class QRCodeTest
  4. *
  5. * @created 17.11.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. declare(strict_types=1);
  11. namespace chillerlan\QRCodeTest;
  12. use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
  13. use chillerlan\QRCode\Common\ECICharset;
  14. use chillerlan\QRCode\Output\{QRCodeOutputException, QRGdImagePNG};
  15. use chillerlan\QRCodeTest\Traits\BuildDirTrait;
  16. use PHPUnit\Framework\Attributes\Test;
  17. use PHPUnit\Framework\TestCase;
  18. use stdClass;
  19. /**
  20. * Tests basic functions of the QRCode class
  21. */
  22. final class QRCodeTest extends TestCase{
  23. use BuildDirTrait;
  24. private QRCode $qrcode;
  25. private QROptions $options;
  26. private const buildDir = 'output-test';
  27. /**
  28. * invoke test instances
  29. */
  30. protected function setUp():void{
  31. $this->createBuildDir($this::buildDir);
  32. $this->qrcode = new QRCode;
  33. $this->options = new QROptions;
  34. }
  35. /**
  36. * tests if an exception is thrown if the given output class does not exist
  37. */
  38. #[Test]
  39. public function initCustomOutputInterfaceNotExistsException():void{
  40. $this->expectException(QRCodeOutputException::class);
  41. $this->expectExceptionMessage('invalid output class');
  42. $this->options->outputInterface = 'foo';
  43. $this->qrcode->setOptions($this->options)->render('test');
  44. }
  45. /**
  46. * tests if an exception is thrown if the given output class does not implement QROutputInterface
  47. */
  48. #[Test]
  49. public function initCustomOutputInterfaceNotImplementsException():void{
  50. $this->expectException(QRCodeOutputException::class);
  51. $this->expectExceptionMessage('output class does not implement QROutputInterface');
  52. $this->options->outputInterface = stdClass::class;
  53. $this->qrcode->setOptions($this->options)->render('test');
  54. }
  55. /**
  56. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  57. */
  58. #[Test]
  59. public function saveException():void{
  60. $this->expectException(QRCodeOutputException::class);
  61. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  62. $this->options->cachefile = '/foo/bar.test';
  63. $this->qrcode->setOptions($this->options)->render('test');
  64. }
  65. /**
  66. * Tests if a cache file is properly saved in the given path
  67. */
  68. #[Test]
  69. public function renderToCacheFile():void{
  70. $fileSubPath = $this::buildDir.'/test.cache.svg';
  71. $this->options->cachefile = $this->getBuildPath($fileSubPath);
  72. $this->options->outputBase64 = false;
  73. // create the cache file
  74. $data = $this->qrcode->setOptions($this->options)->render('test');
  75. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  76. }
  77. /**
  78. * Tests adding and decoding an ECI sequence
  79. */
  80. #[Test]
  81. public function addEciSegment():void{
  82. $expected = '无可奈何燃花作香';
  83. $qrCode = (new QRCode([
  84. 'outputBase64' => false,
  85. 'outputInterface' => QRGdImagePNG::class,
  86. ]));
  87. $qrCode->addEciSegment(ECICharset::GB18030, $expected);
  88. $result = $qrCode->readFromBlob($qrCode->render());
  89. $this::assertSame($expected, $result->data);
  90. }
  91. #[Test]
  92. public function addEciSegmentInvalidCharsetException():void{
  93. $this->expectException(QRCodeException::class);
  94. $this->expectExceptionMessage('unable to add ECI segment');
  95. (new QRCode)->addEciSegment(666, 'nope');
  96. }
  97. }