DatainterfaceTestAbstract.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Class DatainterfaceTestAbstract
  4. *
  5. * @created 24.11.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Data;
  11. use chillerlan\QRCode\Common\MaskPattern;
  12. use chillerlan\QRCode\QRCode;
  13. use chillerlan\QRCode\QROptions;
  14. use PHPUnit\Framework\TestCase;
  15. use chillerlan\QRCode\Data\{QRCodeDataException, QRData, QRMatrix};
  16. use ReflectionClass;
  17. use function str_repeat;
  18. /**
  19. * The data interface test abstract
  20. */
  21. abstract class DatainterfaceTestAbstract extends TestCase{
  22. /** @internal */
  23. protected ReflectionClass $reflection;
  24. /** @internal */
  25. protected QRData $dataInterface;
  26. /** @internal */
  27. protected array $testdata;
  28. /** @internal */
  29. protected array $expected;
  30. /**
  31. * @internal
  32. */
  33. protected function setUp():void{
  34. $this->dataInterface = new QRData(new QROptions(['version' => 4]), []);
  35. $this->reflection = new ReflectionClass($this->dataInterface);
  36. }
  37. protected function setTestData():void{
  38. [$class, $data] = $this->testdata;
  39. $this->dataInterface->setData([new $class($data)]);
  40. }
  41. /**
  42. * Verifies the data interface instance
  43. */
  44. public function testInstance():void{
  45. $this::assertInstanceOf(QRData::class, $this->dataInterface);
  46. }
  47. /**
  48. * Tests ecc masking and verifies against a sample
  49. */
  50. /* public function testMaskEcc():void{
  51. $this->dataInterface->setData([$this->testdata]);
  52. $maskECC = $this->reflection->getMethod('maskECC');
  53. $maskECC->setAccessible(true);
  54. $bitBuffer = $this->reflection->getProperty('bitBuffer');
  55. $bitBuffer->setAccessible(true);
  56. $bb = $bitBuffer->getValue($this->dataInterface);
  57. $this::assertSame($this->expected, $maskECC->invokeArgs($this->dataInterface, [$bb->getBuffer()]));
  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->setTestData();
  74. $matrix = $this->dataInterface->writeMatrix(new MaskPattern($maskPattern));
  75. $this::assertInstanceOf(QRMatrix::class, $matrix);
  76. $this::assertSame($maskPattern, $matrix->maskPattern()->getPattern());
  77. }
  78. /**
  79. * Tests getting the minimum QR version for the given data
  80. */
  81. public function testGetMinimumVersion():void{
  82. $this->setTestData();
  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. [$class, $data] = $this->testdata;
  94. $this->dataInterface = new QRData(
  95. new QROptions(['version' => QRCode::VERSION_AUTO]),
  96. [new $class(str_repeat($data, 1337))]
  97. );
  98. }
  99. /**
  100. * Tests if an exception is thrown on data overflow
  101. */
  102. public function testCodeLengthOverflowException():void{
  103. $this->expectException(QRCodeDataException::class);
  104. $this->expectExceptionMessage('code length overflow');
  105. [$class, $data] = $this->testdata;
  106. $this->testdata = [$class, str_repeat($data, 1337)];
  107. $this->setTestData();
  108. }
  109. }