QROutputTestAbstract.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\QRCodeTest\BuildDirTrait;
  12. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  15. use chillerlan\Settings\SettingsContainerInterface;
  16. use PHPUnit\Framework\Attributes\DataProvider;
  17. use PHPUnit\Framework\TestCase;
  18. use ReflectionObject;
  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. use BuildDirTrait;
  25. protected SettingsContainerInterface|QROptions $options;
  26. protected QROutputInterface $outputInterface;
  27. protected QRMatrix $matrix;
  28. protected const buildDir = 'output-test';
  29. /**
  30. * Attempts to create a directory under /.build and instances several required objects
  31. */
  32. protected function setUp():void{
  33. $this->createBuildDir($this::buildDir);
  34. $this->options = new QROptions;
  35. $this->matrix = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
  36. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  37. }
  38. abstract protected function getOutputInterface(
  39. SettingsContainerInterface|QROptions $options,
  40. QRMatrix $matrix,
  41. ):QROutputInterface;
  42. /**
  43. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  44. */
  45. public function testSaveException():void{
  46. $this->expectException(QRCodeOutputException::class);
  47. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  48. $this->outputInterface->dump('/foo/bar.test');
  49. }
  50. /**
  51. * @phpstan-return array<string, array{0: mixed, 1: bool}>
  52. */
  53. abstract public static function moduleValueProvider():array;
  54. #[DataProvider('moduleValueProvider')]
  55. public function testValidateModuleValues(mixed $value, bool $expected):void{
  56. $this::assertSame($expected, $this->outputInterface::moduleValueIsValid($value));
  57. }
  58. /*
  59. * additional, non-essential, potentially inaccurate coverage tests
  60. */
  61. /**
  62. * covers the module values settings
  63. */
  64. abstract public function testSetModuleValues():void;
  65. /**
  66. * coverage of the built-in output modules
  67. */
  68. public function testRenderToCacheFile():void{
  69. $this->options->outputBase64 = false;
  70. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  71. // create the cache file
  72. $name = (new ReflectionObject($this->outputInterface))->getShortName();
  73. $fileSubPath = $this::buildDir.'/test.output.'.$name;
  74. $data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
  75. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  76. }
  77. }