QROutputTestAbstract.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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']]))->initMatrix(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. $this->expectException(QRCodeOutputException::class);
  62. $this->expectExceptionMessage('Could not write data to cache file: /foo');
  63. $this->options->cachefile = '/foo';
  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' && (
  103. $type === QRCode::OUTPUT_IMAGE_JPG
  104. || $type === QRCode::OUTPUT_IMAGICK
  105. || $type === QRCode::OUTPUT_MARKUP_SVG
  106. )){
  107. $this::markTestSkipped('may fail on CI');
  108. /** @noinspection PhpUnreachableStatementInspection */
  109. return;
  110. }
  111. $this->options->outputType = $type;
  112. $this::assertSame(
  113. trim(file_get_contents(__DIR__.'/samples/'.$type)),
  114. trim((new QRCode($this->options))->render('test'))
  115. );
  116. }
  117. }