QROutputTestAbstract.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Data\QRMatrix;
  13. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  14. use chillerlan\Settings\SettingsContainerInterface;
  15. use PHPUnit\Framework\Attributes\DataProvider;
  16. use PHPUnit\Framework\TestCase;
  17. use ReflectionClass;
  18. use function file_exists, file_get_contents, mkdir, realpath;
  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. protected SettingsContainerInterface|QROptions $options;
  25. protected QROutputInterface $outputInterface;
  26. protected QRMatrix $matrix;
  27. protected const 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 QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
  37. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  38. }
  39. abstract protected function getOutputInterface(
  40. SettingsContainerInterface|QROptions $options,
  41. QRMatrix $matrix
  42. ):QROutputInterface;
  43. /**
  44. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  45. */
  46. public function testSaveException():void{
  47. $this->expectException(QRCodeOutputException::class);
  48. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  49. $this->outputInterface->dump('/foo/bar.test');
  50. }
  51. abstract public static function moduleValueProvider():array;
  52. #[DataProvider('moduleValueProvider')]
  53. public function testValidateModuleValues(mixed $value, bool $expected):void{
  54. $this::assertSame($expected, $this->outputInterface::moduleValueIsValid($value));
  55. }
  56. /*
  57. * additional, non-essential, potentially inaccurate coverage tests
  58. */
  59. /**
  60. * covers the module values settings
  61. */
  62. abstract public function testSetModuleValues():void;
  63. /**
  64. * coverage of the built-in output modules
  65. */
  66. public function testRenderToCacheFile():void{
  67. $this->options->outputBase64 = false;
  68. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  69. // create the cache file
  70. $name = (new ReflectionClass($this->outputInterface))->getShortName();
  71. $file = realpath($this::buildDir).'test.output.'.$name;
  72. $data = $this->outputInterface->dump($file);
  73. $this::assertSame($data, file_get_contents($file));
  74. }
  75. }