QROutputTestAbstract.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Class QROutputTestAbstract
  4. *
  5. * @created 24.12.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Output;
  11. use chillerlan\QRCode\{QRCode, QROptions};
  12. use chillerlan\QRCode\Common\MaskPattern;
  13. use chillerlan\QRCode\Data\{Byte, QRData, QRMatrix};
  14. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  15. use PHPUnit\Framework\TestCase;
  16. use function file_exists, in_array, mkdir;
  17. use const PHP_OS_FAMILY, PHP_VERSION_ID;
  18. /**
  19. * Test abstract for the several (built-in) output modules,
  20. * should also be used to test custom output modules
  21. */
  22. abstract class QROutputTestAbstract extends TestCase{
  23. /** @internal */
  24. protected string $builddir = __DIR__.'/../../.build/output_test';
  25. /** @internal */
  26. protected QROutputInterface $outputInterface;
  27. /** @internal */
  28. protected QROptions $options;
  29. /** @internal */
  30. protected QRMatrix $matrix;
  31. /**
  32. * Attempts to create a directory under /.build and instances several required objects
  33. *
  34. * @internal
  35. */
  36. protected function setUp():void{
  37. if(!file_exists($this->builddir)){
  38. mkdir($this->builddir, 0777, true);
  39. }
  40. $this->options = new QROptions;
  41. $this->matrix = (new QRData($this->options, [new Byte('testdata')]))
  42. ->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010));
  43. $this->outputInterface = $this->getOutputInterface($this->options);
  44. }
  45. /**
  46. * Returns a QROutputInterface instance with the given options and using $this->matrix
  47. *
  48. * @internal
  49. */
  50. abstract protected function getOutputInterface(QROptions $options):QROutputInterface;
  51. /**
  52. * Validate the instance of the interface
  53. */
  54. public function testInstance():void{
  55. $this::assertInstanceOf(QROutputInterface::class, $this->outputInterface);
  56. }
  57. /**
  58. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  59. */
  60. public function testSaveException():void{
  61. $this->expectException(QRCodeOutputException::class);
  62. $this->expectExceptionMessage('Could not write data to cache file: /foo/bar.test');
  63. $this->options->cachefile = '/foo/bar.test';
  64. $this->outputInterface = $this->getOutputInterface($this->options);
  65. $this->outputInterface->dump();
  66. }
  67. /**
  68. * covers the module values settings
  69. */
  70. abstract public function testSetModuleValues():void;
  71. /*
  72. * additional, non-essential, potentially inaccurate coverage tests
  73. */
  74. /**
  75. * @see testStringOutput()
  76. * @return string[][]
  77. * @internal
  78. */
  79. abstract public function types():array;
  80. /**
  81. * coverage of the built-in output modules
  82. *
  83. * @dataProvider types
  84. */
  85. public function testStringOutput(string $type):void{
  86. $this->options->outputType = $type;
  87. $this->options->cachefile = $this->builddir.'/test.'.$type;
  88. $this->options->imageBase64 = false;
  89. $this->outputInterface = $this->getOutputInterface($this->options);
  90. $data = $this->outputInterface->dump(); // creates the cache file
  91. $this::assertSame($data, file_get_contents($this->options->cachefile));
  92. }
  93. /**
  94. * covers the built-in output modules, tests against pre-rendered data
  95. *
  96. * @dataProvider types
  97. */
  98. public function testRenderImage(string $type):void{
  99. // may fail on CI, different PHP (platform) versions produce different output
  100. // the samples were generated on php-7.4.3-Win32-vc15-x64
  101. if(
  102. (PHP_OS_FAMILY !== 'Windows' || PHP_VERSION_ID >= 80100)
  103. && in_array($type, [QRCode::OUTPUT_IMAGE_JPG, QRCode::OUTPUT_IMAGICK, QRCode::OUTPUT_MARKUP_SVG])
  104. ){
  105. $this::markTestSkipped('may fail on CI');
  106. }
  107. $this->options->outputType = $type;
  108. $this::assertSame(
  109. trim(file_get_contents(__DIR__.'/samples/'.$type)),
  110. trim((new QRCode($this->options))->render('test'))
  111. );
  112. }
  113. }