QROutputTestAbstract.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, QRMatrix};
  15. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  16. use PHPUnit\Framework\TestCase;
  17. use function file_exists, mkdir;
  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 Byte($this->options, 'testdata'))->initMatrix(0);
  42. $this->outputInterface = $this->getOutputInterface($this->options);
  43. }
  44. /**
  45. * Returns a QROutputInterface instance with the given options and using $this->matrix
  46. *
  47. * @internal
  48. */
  49. abstract protected function getOutputInterface(QROptions $options):QROutputInterface;
  50. /**
  51. * Validate the instance of the interface
  52. */
  53. public function testInstance():void{
  54. $this::assertInstanceOf(QROutputInterface::class, $this->outputInterface);
  55. }
  56. /**
  57. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  58. */
  59. public function testSaveException():void{
  60. $this->expectException(QRCodeOutputException::class);
  61. $this->expectExceptionMessage('Could not write data to cache file: /foo');
  62. $this->options->cachefile = '/foo';
  63. $this->outputInterface = $this->getOutputInterface($this->options);
  64. $this->outputInterface->dump();
  65. }
  66. /**
  67. * covers the module values settings
  68. */
  69. abstract public function testSetModuleValues():void;
  70. /*
  71. * additional, non-essential, potentially inaccurate coverage tests
  72. */
  73. /**
  74. * @see testStringOutput()
  75. * @return string[][]
  76. * @internal
  77. */
  78. abstract public function types():array;
  79. /**
  80. * coverage of the built-in output modules
  81. *
  82. * @dataProvider types
  83. */
  84. public function testStringOutput(string $type):void{
  85. $this->options->outputType = $type;
  86. $this->options->cachefile = $this->builddir.'/test.'.$type;
  87. $this->options->imageBase64 = false;
  88. $this->outputInterface = $this->getOutputInterface($this->options);
  89. $data = $this->outputInterface->dump(); // creates the cache file
  90. $this::assertSame($data, file_get_contents($this->options->cachefile));
  91. }
  92. /**
  93. * covers the built-in output modules, tests against pre-rendered data
  94. *
  95. * @dataProvider types
  96. */
  97. public function testRenderImage(string $type):void{
  98. // may fail on CI, different PHP (platform) versions produce different output
  99. // the samples were generated on php-7.4.3-Win32-vc15-x64
  100. if(\PHP_OS_FAMILY !== 'Windows' && ($type === QRCode::OUTPUT_IMAGE_JPG || $type === QRCode::OUTPUT_IMAGICK)){
  101. $this::markTestSkipped('may fail on CI');
  102. return;
  103. }
  104. $this->options->outputType = $type;
  105. $this::assertSame(
  106. trim(file_get_contents(__DIR__.'/samples/'.$type)),
  107. trim((new QRCode($this->options))->render('test'))
  108. );
  109. }
  110. }