QROutputTestAbstract.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. abstract public static function moduleValueProvider():array;
  51. #[DataProvider('moduleValueProvider')]
  52. public function testValidateModuleValues(mixed $value, bool $expected):void{
  53. $this::assertSame($expected, $this->outputInterface::moduleValueIsValid($value));
  54. }
  55. /*
  56. * additional, non-essential, potentially inaccurate coverage tests
  57. */
  58. /**
  59. * covers the module values settings
  60. */
  61. abstract public function testSetModuleValues():void;
  62. /**
  63. * coverage of the built-in output modules
  64. */
  65. public function testRenderToCacheFile():void{
  66. $this->options->outputBase64 = false;
  67. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  68. // create the cache file
  69. $name = (new ReflectionObject($this->outputInterface))->getShortName();
  70. $fileSubPath = $this::buildDir.'/test.output.'.$name;
  71. $data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
  72. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  73. }
  74. }