DatainterfaceTestAbstract.php 3.5 KB

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