QROutputTestAbstract.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\QRCode\QROptions;
  12. use chillerlan\QRCode\Common\MaskPattern;
  13. use chillerlan\QRCode\Data\{Byte, QRData, QRMatrix};
  14. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  15. use PHPUnit\Framework\TestCase;
  16. use function file_exists, mkdir;
  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. /** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
  23. protected QROptions $options;
  24. protected QROutputInterface $outputInterface;
  25. protected QRMatrix $matrix;
  26. protected string $builddir = __DIR__.'/../../.build/output_test';
  27. protected string $FQN;
  28. protected string $type;
  29. /**
  30. * Attempts to create a directory under /.build and instances several required objects
  31. */
  32. protected function setUp():void{
  33. if(!file_exists($this->builddir)){
  34. mkdir($this->builddir, 0777, true);
  35. }
  36. $this->options = new QROptions;
  37. $this->options->outputType = $this->type;
  38. $this->matrix = (new QRData($this->options, [new Byte('testdata')]))
  39. ->writeMatrix(new MaskPattern(MaskPattern::PATTERN_010));
  40. $this->outputInterface = new $this->FQN($this->options, $this->matrix);
  41. }
  42. /**
  43. * Validate the instance of the interface
  44. */
  45. public function testInstance():void{
  46. $this::assertInstanceOf(QROutputInterface::class, $this->outputInterface);
  47. }
  48. /**
  49. * Tests if an exception is thrown when trying to write a cache file to an invalid destination
  50. */
  51. public function testSaveException():void{
  52. $this->expectException(QRCodeOutputException::class);
  53. $this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
  54. $this->outputInterface = new $this->FQN($this->options, $this->matrix);
  55. $this->outputInterface->dump('/foo/bar.test');
  56. }
  57. abstract public static function moduleValueProvider():array;
  58. /**
  59. * @param mixed $value
  60. * @param bool $expected
  61. *
  62. * @dataProvider moduleValueProvider
  63. */
  64. public function testValidateModuleValues($value, bool $expected):void{
  65. /** @noinspection PhpUndefinedMethodInspection */
  66. $this::assertSame($expected, $this->FQN::moduleValueIsValid($value));
  67. }
  68. /*
  69. * additional, non-essential, potentially inaccurate coverage tests
  70. */
  71. /**
  72. * covers the module values settings
  73. */
  74. abstract public function testSetModuleValues():void;
  75. /**
  76. * coverage of the built-in output modules
  77. */
  78. public function testRenderToCacheFile():void{
  79. $this->options->imageBase64 = false;
  80. $this->outputInterface = new $this->FQN($this->options, $this->matrix);
  81. // create the cache file
  82. $file = $this->builddir.'/test.output.'.$this->type;
  83. $data = $this->outputInterface->dump($file);
  84. $this::assertSame($data, file_get_contents($file));
  85. }
  86. }