QRCodeTest.php 2.4 KB

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