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 PHPUnit\Framework\TestCase;
  16. use ReflectionClass;
  17. /**
  18. * Test abstract for the several (built-in) output modules,
  19. * should also be used to test custom output modules
  20. */
  21. abstract class QROutputTestAbstract extends TestCase{
  22. use BuildDirTrait;
  23. /** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
  24. protected QROptions $options;
  25. protected QROutputInterface $outputInterface;
  26. protected QRMatrix $matrix;
  27. protected string $type;
  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->options->outputType = $this->type;
  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(QROptions $options, QRMatrix $matrix):QROutputInterface;
  40. /**
  41. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  42. */
  43. public function testSaveException():void{
  44. $this->expectException(QRCodeOutputException::class);
  45. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  46. $this->outputInterface->dump('/foo/bar.test');
  47. }
  48. abstract public static function moduleValueProvider():array;
  49. /**
  50. * @param mixed $value
  51. * @param bool $expected
  52. *
  53. * @dataProvider moduleValueProvider
  54. */
  55. public function testValidateModuleValues($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 ReflectionClass($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. }