QROutputTestAbstract.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Class QROutputTestAbstract
  4. *
  5. * @filesource QROutputTestAbstract.php
  6. * @created 24.12.2017
  7. * @package chillerlan\QRCodeTest\Output
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2017 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCodeTest\Output;
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use chillerlan\QRCode\Data\{Byte, QRData, QRMatrix};
  15. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  16. use PHPUnit\Framework\TestCase;
  17. use function file_exists, mkdir;
  18. use const PHP_OS_FAMILY;
  19. /**
  20. * Test abstract for the several (built-in) output modules,
  21. * should also be used to test custom output modules
  22. */
  23. abstract class QROutputTestAbstract extends TestCase{
  24. /** @internal */
  25. protected string $builddir = __DIR__.'/../../.build/output_test';
  26. /** @internal */
  27. protected QROutputInterface $outputInterface;
  28. /** @internal */
  29. protected QROptions $options;
  30. /** @internal */
  31. protected QRMatrix $matrix;
  32. /**
  33. * Attempts to create a directory under /.build and instances several required objects
  34. *
  35. * @internal
  36. */
  37. protected function setUp():void{
  38. if(!file_exists($this->builddir)){
  39. mkdir($this->builddir, 0777, true);
  40. }
  41. $this->options = new QROptions;
  42. $this->matrix = (new QRData($this->options, [[Byte::class, 'testdata']]))->writeMatrix(0);
  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. /** @noinspection PhpUnreachableStatementInspection */
  64. return;
  65. }
  66. $this->expectException(QRCodeOutputException::class);
  67. $this->expectExceptionMessage('Could not write data to cache file: /foo');
  68. $this->options->cachefile = '/foo';
  69. $this->outputInterface = $this->getOutputInterface($this->options);
  70. $this->outputInterface->dump();
  71. }
  72. /**
  73. * covers the module values settings
  74. */
  75. abstract public function testSetModuleValues():void;
  76. /*
  77. * additional, non-essential, potentially inaccurate coverage tests
  78. */
  79. /**
  80. * @see testStringOutput()
  81. * @return string[][]
  82. * @internal
  83. */
  84. abstract public function types():array;
  85. /**
  86. * coverage of the built-in output modules
  87. *
  88. * @dataProvider types
  89. */
  90. public function testStringOutput(string $type):void{
  91. $this->options->outputType = $type;
  92. $this->options->cachefile = $this->builddir.'/test.'.$type;
  93. $this->options->imageBase64 = false;
  94. $this->outputInterface = $this->getOutputInterface($this->options);
  95. $data = $this->outputInterface->dump(); // creates the cache file
  96. $this::assertSame($data, file_get_contents($this->options->cachefile));
  97. }
  98. /**
  99. * covers the built-in output modules, tests against pre-rendered data
  100. *
  101. * @dataProvider types
  102. */
  103. public function testRenderImage(string $type):void{
  104. // may fail on CI, different PHP (platform) versions produce different output
  105. // the samples were generated on php-7.4.3-Win32-vc15-x64
  106. if(
  107. PHP_OS_FAMILY !== 'Windows' && (
  108. $type === QRCode::OUTPUT_IMAGE_JPG
  109. || $type === QRCode::OUTPUT_IMAGICK
  110. || $type === QRCode::OUTPUT_MARKUP_SVG
  111. )){
  112. $this::markTestSkipped('may fail on CI');
  113. /** @noinspection PhpUnreachableStatementInspection */
  114. return;
  115. }
  116. $this->options->outputType = $type;
  117. $this::assertSame(
  118. trim(file_get_contents(__DIR__.'/samples/'.$type)),
  119. trim((new QRCode($this->options))->render('test'))
  120. );
  121. }
  122. }