QROutputTestAbstract.php 3.1 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. namespace chillerlan\QRCodeTest\Output;
  11. use chillerlan\QRCode\QRCode;
  12. use chillerlan\QRCode\QROptions;
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\Settings\SettingsContainerInterface;
  15. use PHPUnit\Framework\Attributes\DataProvider;
  16. use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
  17. use PHPUnit\Framework\TestCase;
  18. use function file_exists, mkdir;
  19. use function str_replace;
  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. protected SettingsContainerInterface|QROptions $options;
  26. protected QROutputInterface $outputInterface;
  27. protected QRMatrix $matrix;
  28. protected string $builddir = __DIR__.'/../../.build/output-test';
  29. protected string $FQN;
  30. /**
  31. * Attempts to create a directory under /.build and instances several required objects
  32. */
  33. protected function setUp():void{
  34. if(!file_exists($this->builddir)){
  35. mkdir($this->builddir, 0777, true);
  36. }
  37. $this->options = new QROptions;
  38. $this->options->outputInterface = $this->FQN;
  39. $this->matrix = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
  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. #[DataProvider('moduleValueProvider')]
  59. public function testValidateModuleValues(mixed $value, bool $expected):void{
  60. /** @noinspection PhpUndefinedMethodInspection */
  61. $this::assertSame($expected, $this->FQN::moduleValueIsValid($value));
  62. }
  63. /*
  64. * additional, non-essential, potentially inaccurate coverage tests
  65. */
  66. /**
  67. * covers the module values settings
  68. */
  69. abstract public function testSetModuleValues():void;
  70. /**
  71. * coverage of the built-in output modules
  72. */
  73. public function testRenderToCacheFile():void{
  74. $this->options->outputBase64 = false;
  75. $this->outputInterface = new $this->FQN($this->options, $this->matrix);
  76. // create the cache file
  77. $file = $this->builddir.'/test.output.'.str_replace('chillerlan\\QRCode\\Output\\', '', $this->FQN);
  78. $data = $this->outputInterface->dump($file);
  79. $this::assertSame($data, file_get_contents($file));
  80. }
  81. }