QROutputTestAbstract.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, mkdir;
  17. use const PHP_OS_FAMILY;
  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. # if(PHP_OS_FAMILY === 'Windows'){
  62. # $this::markTestSkipped('why does this fail on CI??');
  63. # }
  64. $this->expectException(QRCodeOutputException::class);
  65. $this->expectExceptionMessage('Could not write data to cache file: /foo/bar.test');
  66. $this->options->cachefile = '/foo/bar.test';
  67. $this->outputInterface = $this->getOutputInterface($this->options);
  68. $this->outputInterface->dump();
  69. }
  70. /**
  71. * covers the module values settings
  72. */
  73. abstract public function testSetModuleValues():void;
  74. /*
  75. * additional, non-essential, potentially inaccurate coverage tests
  76. */
  77. /**
  78. * @see testStringOutput()
  79. * @return string[][]
  80. * @internal
  81. */
  82. abstract public function types():array;
  83. /**
  84. * coverage of the built-in output modules
  85. *
  86. * @dataProvider types
  87. */
  88. public function testStringOutput(string $type):void{
  89. $this->options->outputType = $type;
  90. $this->options->cachefile = $this->builddir.'/test.'.$type;
  91. $this->options->imageBase64 = false;
  92. $this->outputInterface = $this->getOutputInterface($this->options);
  93. $data = $this->outputInterface->dump(); // creates the cache file
  94. $this::assertSame($data, file_get_contents($this->options->cachefile));
  95. }
  96. /**
  97. * covers the built-in output modules, tests against pre-rendered data
  98. *
  99. * @dataProvider types
  100. */
  101. public function testRenderImage(string $type):void{
  102. // may fail on CI, different PHP (platform) versions produce different output
  103. // the samples were generated on php-7.4.3-Win32-vc15-x64
  104. if(
  105. PHP_OS_FAMILY !== 'Windows' && (
  106. $type === QRCode::OUTPUT_IMAGE_JPG
  107. || $type === QRCode::OUTPUT_IMAGICK
  108. || $type === QRCode::OUTPUT_MARKUP_SVG
  109. )){
  110. $this::markTestSkipped('may fail on CI');
  111. /** @noinspection PhpUnreachableStatementInspection */
  112. return;
  113. }
  114. $this->options->outputType = $type;
  115. $this::assertSame(
  116. trim(file_get_contents(__DIR__.'/samples/'.$type)),
  117. trim((new QRCode($this->options))->render('test'))
  118. );
  119. }
  120. }