DatainterfaceTestAbstract.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $this::assertSame($this->expected, $maskECC->invoke($this->dataInterface));
  52. }
  53. /**
  54. * @see testInitMatrix()
  55. * @internal
  56. * @return int[][]
  57. */
  58. public function MaskPatternProvider():array{
  59. return [[0], [1], [2], [3], [4], [5], [6], [7]];
  60. }
  61. /**
  62. * Tests initializing the data matrix
  63. *
  64. * @dataProvider MaskPatternProvider
  65. */
  66. public function testInitMatrix(int $maskPattern):void{
  67. $this->dataInterface->setData([$this->testdata]);
  68. $matrix = $this->dataInterface->initMatrix($maskPattern);
  69. $this::assertInstanceOf(QRMatrix::class, $matrix);
  70. $this::assertSame($maskPattern, $matrix->maskPattern());
  71. }
  72. /**
  73. * Tests getting the minimum QR version for the given data
  74. */
  75. public function testGetMinimumVersion():void{
  76. $this->dataInterface->setData([$this->testdata]);
  77. $getMinimumVersion = $this->reflection->getMethod('getMinimumVersion');
  78. $getMinimumVersion->setAccessible(true);
  79. $this::assertSame(1, $getMinimumVersion->invoke($this->dataInterface));
  80. }
  81. /**
  82. * Tests if an exception is thrown when the data exceeds the maximum version while auto detecting
  83. */
  84. public function testGetMinimumVersionException():void{
  85. $this->expectException(QRCodeDataException::class);
  86. $this->expectExceptionMessage('data exceeds');
  87. [$class, $data] = $this->testdata;
  88. $this->dataInterface = new QRData(
  89. new QROptions(['version' => QRCode::VERSION_AUTO]),
  90. [[$class, str_repeat($data, 1337)]]
  91. );
  92. }
  93. /**
  94. * Tests if an exception is thrown on data overflow
  95. */
  96. public function testCodeLengthOverflowException():void{
  97. $this->expectException(QRCodeDataException::class);
  98. $this->expectExceptionMessage('code length overflow');
  99. [$class, $data] = $this->testdata;
  100. $this->dataInterface->setData([[$class, str_repeat($data, 1337)]]);
  101. }
  102. }