QRCodeTest.php 2.3 KB

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