QRCodeTest.php 2.8 KB

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