DatainterfaceTestAbstract.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Class DatainterfaceTestAbstract
  4. *
  5. * @filesource DatainterfaceTestAbstract.php
  6. * @created 24.11.2017
  7. * @package chillerlan\QRCodeTest\Data
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2017 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCodeTest\Data;
  13. use chillerlan\QRCode\QRCode;
  14. use chillerlan\QRCode\QROptions;
  15. use PHPUnit\Framework\TestCase;
  16. use chillerlan\QRCode\Data\{QRCodeDataException, QRDataInterface, QRMatrix};
  17. use ReflectionClass;
  18. use function str_repeat;
  19. /**
  20. * Te data interface test abstract
  21. */
  22. abstract class DatainterfaceTestAbstract extends TestCase{
  23. /** @internal */
  24. protected ReflectionClass $reflection;
  25. /** @internal */
  26. protected QRDataInterface $dataInterface;
  27. /** @internal */
  28. protected string $testdata;
  29. /** @internal */
  30. protected array $expected;
  31. /**
  32. * @internal
  33. */
  34. protected function setUp():void{
  35. $this->dataInterface = $this->getDataInterfaceInstance(new QROptions(['version' => 4]));
  36. $this->reflection = new ReflectionClass($this->dataInterface);
  37. }
  38. /**
  39. * Returns a data interface instance
  40. *
  41. * @internal
  42. */
  43. abstract protected function getDataInterfaceInstance(QROptions $options):QRDataInterface;
  44. /**
  45. * Verifies the data interface instance
  46. */
  47. public function testInstance():void{
  48. $this::assertInstanceOf(QRDataInterface::class, $this->dataInterface);
  49. }
  50. /**
  51. * Tests ecc masking and verifies against a sample
  52. */
  53. public function testMaskEcc():void{
  54. $this->dataInterface->setData($this->testdata);
  55. $maskECC = $this->reflection->getMethod('maskECC');
  56. $maskECC->setAccessible(true);
  57. $this::assertSame($this->expected, $maskECC->invoke($this->dataInterface));
  58. }
  59. /**
  60. * @see testInitMatrix()
  61. * @internal
  62. * @return int[][]
  63. */
  64. public function MaskPatternProvider():array{
  65. return [[0], [1], [2], [3], [4], [5], [6], [7]];
  66. }
  67. /**
  68. * Tests initializing the data matrix
  69. *
  70. * @dataProvider MaskPatternProvider
  71. */
  72. public function testInitMatrix(int $maskPattern):void{
  73. $this->dataInterface->setData($this->testdata);
  74. $matrix = $this->dataInterface->initMatrix($maskPattern);
  75. $this::assertInstanceOf(QRMatrix::class, $matrix);
  76. $this::assertSame($maskPattern, $matrix->maskPattern());
  77. }
  78. /**
  79. * Tests getting the minimum QR version for the given data
  80. */
  81. public function testGetMinimumVersion():void{
  82. $this->dataInterface->setData($this->testdata);
  83. $getMinimumVersion = $this->reflection->getMethod('getMinimumVersion');
  84. $getMinimumVersion->setAccessible(true);
  85. $this::assertSame(1, $getMinimumVersion->invoke($this->dataInterface));
  86. }
  87. /**
  88. * Tests if an exception is thrown when the data exceeds the maximum version while auto detecting
  89. */
  90. public function testGetMinimumVersionException():void{
  91. $this->expectException(QRCodeDataException::class);
  92. $this->expectExceptionMessage('data exceeds');
  93. $this->dataInterface = $this->getDataInterfaceInstance(new QROptions(['version' => QRCode::VERSION_AUTO]));
  94. $this->dataInterface->setData(str_repeat($this->testdata, 1337));
  95. }
  96. /**
  97. * Tests if an exception is thrown on data overflow
  98. */
  99. public function testCodeLengthOverflowException():void{
  100. $this->expectException(QRCodeDataException::class);
  101. $this->expectExceptionMessage('code length overflow');
  102. $this->dataInterface->setData(str_repeat($this->testdata, 1337));
  103. }
  104. }