QRCodeTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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;
  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 if the given output class does not exist
  33. */
  34. public function testInitCustomOutputInterfaceNotExistsException():void{
  35. $this->expectException(QRCodeOutputException::class);
  36. $this->expectExceptionMessage('invalid output class');
  37. $this->options->outputInterface = '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 implement QROutputInterface
  42. */
  43. public function testInitCustomOutputInterfaceNotImplementsException():void{
  44. $this->expectException(QRCodeOutputException::class);
  45. $this->expectExceptionMessage('output class does not implement QROutputInterface');
  46. $this->options->outputInterface = stdClass::class;
  47. $this->qrcode->setOptions($this->options)->render('test');
  48. }
  49. /**
  50. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  51. */
  52. public function testSaveException():void{
  53. $this->expectException(QRCodeOutputException::class);
  54. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  55. $this->options->cachefile = '/foo/bar.test';
  56. $this->qrcode->setOptions($this->options)->render('test');
  57. }
  58. /**
  59. * Tests if a cache file is properly saved in the given path
  60. */
  61. public function testRenderToCacheFile():void{
  62. $fileSubPath = $this::buildDir.'/test.cache.svg';
  63. $this->options->cachefile = $this->getBuildPath($fileSubPath);
  64. $this->options->outputBase64 = false;
  65. // create the cache file
  66. $data = $this->qrcode->setOptions($this->options)->render('test');
  67. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  68. }
  69. }