DatainterfaceTestAbstract.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. protected ReflectionClass $reflection;
  23. protected QRData $QRData;
  24. protected string $FQN;
  25. protected string $testdata;
  26. protected function setUp():void{
  27. $this->QRData = new QRData(new QROptions(['version' => 4]));
  28. $this->reflection = new ReflectionClass($this->QRData);
  29. }
  30. /**
  31. * Verifies the data interface instance
  32. */
  33. public function testInstance():void{
  34. $this::assertInstanceOf(QRData::class, $this->QRData);
  35. }
  36. /**
  37. * @see testInitMatrix()
  38. * @return int[][]
  39. */
  40. public function maskPatternProvider():array{
  41. return [[0], [1], [2], [3], [4], [5], [6], [7]];
  42. }
  43. /**
  44. * Tests initializing the data matrix
  45. *
  46. * @dataProvider maskPatternProvider
  47. */
  48. public function testInitMatrix(int $maskPattern):void{
  49. $this->QRData->setData([new $this->FQN($this->testdata)]);
  50. $matrix = $this->QRData->writeMatrix(new MaskPattern($maskPattern));
  51. $this::assertInstanceOf(QRMatrix::class, $matrix);
  52. $this::assertSame($maskPattern, $matrix->maskPattern()->getPattern());
  53. }
  54. /**
  55. * Tests getting the minimum QR version for the given data
  56. */
  57. public function testGetMinimumVersion():void{
  58. $this->QRData->setData([new $this->FQN($this->testdata)]);
  59. $getMinimumVersion = $this->reflection->getMethod('getMinimumVersion');
  60. $getMinimumVersion->setAccessible(true);
  61. $this::assertSame(1, $getMinimumVersion->invoke($this->QRData));
  62. }
  63. abstract public function stringValidateProvider():array;
  64. /**
  65. * @dataProvider stringValidateProvider
  66. */
  67. public function testValidateString(string $string, bool $expected):void{
  68. /** @noinspection PhpUndefinedMethodInspection */
  69. $this::assertSame($expected, $this->FQN::validateString($string));
  70. }
  71. /**
  72. * Tests if an exception is thrown when the data exceeds the maximum version while auto detecting
  73. */
  74. public function testGetMinimumVersionException():void{
  75. $this->expectException(QRCodeDataException::class);
  76. $this->expectExceptionMessage('data exceeds');
  77. $this->QRData = new QRData(
  78. new QROptions(['version' => QRCode::VERSION_AUTO]),
  79. [new $this->FQN(str_repeat($this->testdata, 1337))]
  80. );
  81. }
  82. /**
  83. * Tests if an exception is thrown on data overflow
  84. */
  85. public function testCodeLengthOverflowException():void{
  86. $this->expectException(QRCodeDataException::class);
  87. $this->expectExceptionMessage('code length overflow');
  88. $this->QRData->setData([new $this->FQN(str_repeat($this->testdata, 1337))]);
  89. }
  90. /**
  91. * Tests if an exception is thrown when an invalid character is encountered
  92. */
  93. public function testInvalidDataException():void{
  94. $this->expectException(QRCodeDataException::class);
  95. $this->expectExceptionMessage('invalid data');
  96. $this->QRData->setData([new $this->FQN('##')]);
  97. }
  98. /**
  99. * Tests if an exception is thrown if the given string is empty
  100. */
  101. public function testInvalidDataOnEmptyException():void{
  102. $this->expectException(QRCodeDataException::class);
  103. $this->expectExceptionMessage('invalid data');
  104. $this->QRData->setData([new $this->FQN('')]);
  105. }
  106. }