QRCodeTest.php 2.4 KB

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