QROutputTestAbstract.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
  24. protected QROptions $options;
  25. protected QROutputInterface $outputInterface;
  26. protected QRMatrix $matrix;
  27. protected string $builddir = __DIR__.'/../../.build/output_test';
  28. /**
  29. * Attempts to create a directory under /.build and instances several required objects
  30. */
  31. protected function setUp():void{
  32. if(!file_exists($this->builddir)){
  33. mkdir($this->builddir, 0777, true);
  34. }
  35. $this->options = new QROptions;
  36. $this->matrix = (new QRData($this->options, [new Byte('testdata')]))
  37. ->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010));
  38. $this->outputInterface = $this->getOutputInterface($this->options);
  39. }
  40. /**
  41. * Returns a QROutputInterface instance with the given options and using $this->matrix
  42. */
  43. abstract protected function getOutputInterface(QROptions $options):QROutputInterface;
  44. /**
  45. * Validate the instance of the interface
  46. */
  47. public function testInstance():void{
  48. $this::assertInstanceOf(QROutputInterface::class, $this->outputInterface);
  49. }
  50. /**
  51. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  52. */
  53. public function testSaveException():void{
  54. $this->expectException(QRCodeOutputException::class);
  55. $this->expectExceptionMessage('Could not write data to cache file: /foo/bar.test');
  56. $this->options->cachefile = '/foo/bar.test';
  57. $this->outputInterface = $this->getOutputInterface($this->options);
  58. $this->outputInterface->dump();
  59. }
  60. /**
  61. * covers the module values settings
  62. */
  63. abstract public function testSetModuleValues():void;
  64. /*
  65. * additional, non-essential, potentially inaccurate coverage tests
  66. */
  67. /**
  68. * @see testStringOutput()
  69. * @return string[][]
  70. */
  71. abstract public function types():array;
  72. /**
  73. * coverage of the built-in output modules
  74. *
  75. * @dataProvider types
  76. */
  77. public function testStringOutput(string $type):void{
  78. $this->options->outputType = $type;
  79. $this->options->cachefile = $this->builddir.'/test.'.$type;
  80. $this->options->imageBase64 = false;
  81. $this->outputInterface = $this->getOutputInterface($this->options);
  82. $data = $this->outputInterface->dump(); // creates the cache file
  83. $this::assertSame($data, file_get_contents($this->options->cachefile));
  84. }
  85. /**
  86. * covers the built-in output modules, tests against pre-rendered data
  87. *
  88. * @dataProvider types
  89. */
  90. public function testRenderImage(string $type):void{
  91. // may fail on CI, different PHP (platform) versions produce different output
  92. // the samples were generated on php-7.4.3-Win32-vc15-x64
  93. if(
  94. (PHP_OS_FAMILY !== 'Windows' || PHP_VERSION_ID >= 80100)
  95. && in_array($type, [QRCode::OUTPUT_IMAGE_JPG, QRCode::OUTPUT_IMAGICK, QRCode::OUTPUT_MARKUP_SVG])
  96. ){
  97. $this::markTestSkipped('may fail on CI');
  98. }
  99. $this->options->outputType = $type;
  100. $this::assertSame(
  101. trim(file_get_contents(__DIR__.'/../samples/'.$type)),
  102. trim((new QRCode($this->options))->render('test'))
  103. );
  104. }
  105. }