QROutputTestAbstract.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\QRCodeTest\Traits\BuildDirTrait;
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  16. use chillerlan\Settings\SettingsContainerInterface;
  17. use PHPUnit\Framework\Attributes\DataProvider;
  18. use PHPUnit\Framework\TestCase;
  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. 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. /**
  52. * @phpstan-return array<string, array{0: mixed, 1: bool}>
  53. */
  54. abstract public static function moduleValueProvider():array;
  55. #[DataProvider('moduleValueProvider')]
  56. public function testValidateModuleValues(mixed $value, bool $expected):void{
  57. $this::assertSame($expected, $this->outputInterface::moduleValueIsValid($value));
  58. }
  59. /*
  60. * additional, non-essential, potentially inaccurate coverage tests
  61. */
  62. /**
  63. * covers the module values settings
  64. */
  65. abstract public function testSetModuleValues():void;
  66. /**
  67. * coverage of the built-in output modules
  68. */
  69. public function testRenderToCacheFile():void{
  70. $this->options->outputBase64 = false;
  71. $this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
  72. // create the cache file
  73. $name = (new ReflectionObject($this->outputInterface))->getShortName();
  74. $fileSubPath = $this::buildDir.'/test.output.'.$name;
  75. $data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
  76. $this::assertSame($data, $this->getBuildFileContent($fileSubPath));
  77. }
  78. }