DatainterfaceTestAbstract.php 3.5 KB

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