DatainterfaceTestAbstract.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, QRData, QRMatrix};
  17. use ReflectionClass;
  18. use function str_repeat;
  19. /**
  20. * The data interface test abstract
  21. */
  22. abstract class DatainterfaceTestAbstract extends TestCase{
  23. /** @internal */
  24. protected ReflectionClass $reflection;
  25. /** @internal */
  26. protected QRData $dataInterface;
  27. /** @internal */
  28. protected array $testdata;
  29. /** @internal */
  30. protected array $expected;
  31. /**
  32. * @internal
  33. */
  34. protected function setUp():void{
  35. $this->dataInterface = new QRData(new QROptions(['version' => 4]), []);
  36. $this->reflection = new ReflectionClass($this->dataInterface);
  37. }
  38. /**
  39. * Verifies the data interface instance
  40. */
  41. public function testInstance():void{
  42. $this::assertInstanceOf(QRData::class, $this->dataInterface);
  43. }
  44. /**
  45. * Tests ecc masking and verifies against a sample
  46. */
  47. /* public function testMaskEcc():void{
  48. $this->dataInterface->setData([$this->testdata]);
  49. $maskECC = $this->reflection->getMethod('maskECC');
  50. $maskECC->setAccessible(true);
  51. $bitBuffer = $this->reflection->getProperty('bitBuffer');
  52. $bitBuffer->setAccessible(true);
  53. $bb = $bitBuffer->getValue($this->dataInterface);
  54. $this::assertSame($this->expected, $maskECC->invokeArgs($this->dataInterface, [$bb->getBuffer()]));
  55. }*/
  56. /**
  57. * @see testInitMatrix()
  58. * @internal
  59. * @return int[][]
  60. */
  61. public function MaskPatternProvider():array{
  62. return [[0], [1], [2], [3], [4], [5], [6], [7]];
  63. }
  64. /**
  65. * Tests initializing the data matrix
  66. *
  67. * @dataProvider MaskPatternProvider
  68. */
  69. public function testInitMatrix(int $maskPattern):void{
  70. $this->dataInterface->setData([$this->testdata]);
  71. $matrix = $this->dataInterface->writeMatrix($maskPattern);
  72. $this::assertInstanceOf(QRMatrix::class, $matrix);
  73. $this::assertSame($maskPattern, $matrix->maskPattern());
  74. }
  75. /**
  76. * Tests getting the minimum QR version for the given data
  77. */
  78. public function testGetMinimumVersion():void{
  79. $this->dataInterface->setData([$this->testdata]);
  80. $getMinimumVersion = $this->reflection->getMethod('getMinimumVersion');
  81. $getMinimumVersion->setAccessible(true);
  82. $this::assertSame(1, $getMinimumVersion->invoke($this->dataInterface));
  83. }
  84. /**
  85. * Tests if an exception is thrown when the data exceeds the maximum version while auto detecting
  86. */
  87. public function testGetMinimumVersionException():void{
  88. $this->expectException(QRCodeDataException::class);
  89. $this->expectExceptionMessage('data exceeds');
  90. [$class, $data] = $this->testdata;
  91. $this->dataInterface = new QRData(
  92. new QROptions(['version' => QRCode::VERSION_AUTO]),
  93. [[$class, str_repeat($data, 1337)]]
  94. );
  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. [$class, $data] = $this->testdata;
  103. $this->dataInterface->setData([[$class, str_repeat($data, 1337)]]);
  104. }
  105. }