QRCodeTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. namespace chillerlan\QRCodeTest;
  11. use chillerlan\QRCode\{QROptions, QRCode};
  12. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  13. use PHPUnit\Framework\TestCase;
  14. use stdClass;
  15. /**
  16. * Tests basic functions of the QRCode class
  17. */
  18. final class QRCodeTest extends TestCase{
  19. use BuildDirTrait;
  20. private QRCode $qrcode;
  21. private QROptions $options;
  22. private const buildDir = 'output-test';
  23. /**
  24. * invoke test instances
  25. */
  26. protected function setUp():void{
  27. $this->createBuildDir($this::buildDir);
  28. $this->qrcode = new QRCode;
  29. $this->options = new QROptions;
  30. }
  31. /**
  32. * tests if an exception is thrown when an invalid (built-in) output type is specified
  33. */
  34. public function testInitOutputInterfaceException():void{
  35. $this->expectException(QRCodeOutputException::class);
  36. $this->expectExceptionMessage('invalid output module');
  37. $this->options->outputType = 'foo';
  38. $this->qrcode->setOptions($this->options)->render('test');
  39. }
  40. /**
  41. * tests if an exception is thrown if the given output class does not exist
  42. */
  43. public function testInitCustomOutputInterfaceNotExistsException():void{
  44. $this->expectException(QRCodeOutputException::class);
  45. $this->expectExceptionMessage('invalid output module');
  46. $this->options->outputType = QROutputInterface::CUSTOM;
  47. $this->qrcode->setOptions($this->options)->render('test');
  48. }
  49. /**
  50. * tests if an exception is thrown if the given output class does not implement QROutputInterface
  51. */
  52. public function testInitCustomOutputInterfaceNotImplementsException():void{
  53. $this->expectException(QRCodeOutputException::class);
  54. $this->expectExceptionMessage('output module does not implement QROutputInterface');
  55. $this->options->outputType = QROutputInterface::CUSTOM;
  56. $this->options->outputInterface = stdClass::class;
  57. $this->qrcode->setOptions($this->options)->render('test');
  58. }
  59. /**
  60. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  61. */
  62. public function testSaveException():void{
  63. $this->expectException(QRCodeOutputException::class);
  64. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  65. $this->options->cachefile = '/foo/bar.test';
  66. $this->qrcode->setOptions($this->options)->render('test');
  67. }
  68. /**
  69. * Tests if a cache file is properly saved in the given path
  70. */
  71. public function testRenderToCacheFile():void{
  72. $fileSubPath = $this::buildDir.'/test.cache.svg';
  73. $this->options->cachefile = $this->getBuildPath($fileSubPath);
  74. $this->options->outputBase64 = false;
  75. // create the cache file
  76. $data = $this->qrcode->setOptions($this->options)->render('test');
  77. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  78. }
  79. }