QROutputTestAbstract.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. declare(strict_types=1);
  11. namespace chillerlan\QRCodeTest\Output;
  12. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  15. use chillerlan\QRCodeTest\Traits\BuildDirTrait;
  16. use chillerlan\Settings\SettingsContainerInterface;
  17. use PHPUnit\Framework\TestCase;
  18. use PHPUnit\Framework\Attributes\{DataProvider, Test};
  19. use ReflectionObject;
  20. /**
  21. * Test abstract for the several (built-in) output modules,
  22. * should also be used to test custom output modules
  23. */
  24. abstract class QROutputTestAbstract extends TestCase{
  25. use BuildDirTrait;
  26. protected SettingsContainerInterface|QROptions $options;
  27. protected QROutputInterface $outputInterface;
  28. protected QRMatrix $matrix;
  29. protected const buildDir = 'output-test';
  30. /**
  31. * Attempts to create a directory under /.build and instances several required objects
  32. */
  33. protected function setUp():void{
  34. $this->createBuildDir($this::buildDir);
  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. #[Test]
  47. public function saveException():void{
  48. $this->expectException(QRCodeOutputException::class);
  49. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  50. $this->outputInterface->dump('/foo/bar.test');
  51. }
  52. /**
  53. * @phpstan-return array<string, array{0: mixed, 1: bool}>
  54. */
  55. abstract public static function moduleValueProvider():array;
  56. #[Test]
  57. #[DataProvider('moduleValueProvider')]
  58. public function validateModuleValues(mixed $value, bool $expected):void{
  59. $this::assertSame($expected, $this->outputInterface::moduleValueIsValid($value));
  60. }
  61. /*
  62. * additional, non-essential, potentially inaccurate coverage tests
  63. */
  64. /**
  65. * covers the module values settings
  66. */
  67. #[Test]
  68. abstract public function setModuleValues():void;
  69. /**
  70. * coverage of the built-in output modules
  71. */
  72. #[Test]
  73. public function renderToCacheFile():void{
  74. $this->options->outputBase64 = false;
  75. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  76. // create the cache file
  77. $name = (new ReflectionObject($this->outputInterface))->getShortName();
  78. $fileSubPath = $this::buildDir.'/test.output.'.$name;
  79. $data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
  80. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  81. }
  82. }