DatainterfaceTestAbstract.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  64. * Tests if an exception is thrown when the data exceeds the maximum version while auto detecting
  65. */
  66. public function testGetMinimumVersionException():void{
  67. $this->expectException(QRCodeDataException::class);
  68. $this->expectExceptionMessage('data exceeds');
  69. $this->QRData = new QRData(
  70. new QROptions(['version' => QRCode::VERSION_AUTO]),
  71. [new $this->FQN(str_repeat($this->testdata, 1337))]
  72. );
  73. }
  74. /**
  75. * Tests if an exception is thrown on data overflow
  76. */
  77. public function testCodeLengthOverflowException():void{
  78. $this->expectException(QRCodeDataException::class);
  79. $this->expectExceptionMessage('code length overflow');
  80. $this->QRData->setData([new $this->FQN(str_repeat($this->testdata, 1337))]);
  81. }
  82. /**
  83. * Tests if an exception is thrown when an invalid character is encountered
  84. */
  85. public function testInvalidDataException():void{
  86. $this->expectException(QRCodeDataException::class);
  87. $this->expectExceptionMessage('invalid data');
  88. $this->QRData->setData([new $this->FQN('##')]);
  89. }
  90. /**
  91. * Tests if an exception is thrown if the given string is empty
  92. */
  93. public function testInvalidDataOnEmptyException():void{
  94. $this->expectException(QRCodeDataException::class);
  95. $this->expectExceptionMessage('invalid data');
  96. $this->QRData->setData([new $this->FQN('')]);
  97. }
  98. }