QROptionsTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Class QROptionsTest
  4. *
  5. * @filesource QROptionsTest.php
  6. * @created 08.11.2018
  7. * @package chillerlan\QRCodeTest
  8. * @author smiley <smiley@chillerlan.net>
  9. * @copyright 2018 smiley
  10. * @license MIT
  11. *
  12. * @noinspection PhpUnusedLocalVariableInspection
  13. */
  14. namespace chillerlan\QRCodeTest;
  15. use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * QROptions test
  19. */
  20. class QROptionsTest extends TestCase{
  21. /**
  22. * @see testVersionClamp()
  23. * @return int[][]
  24. * @internal
  25. */
  26. public function VersionProvider():array{
  27. return [
  28. 'values > 40 should be clamped to 40' => [42, 40],
  29. 'values < 1 should be clamped to 1' => [-42, 1],
  30. 'values in between shold not be touched' => [21, 21],
  31. 'value -1 should be treated as is (default)' => [QRCode::VERSION_AUTO, -1],
  32. ];
  33. }
  34. /**
  35. * Tests the $version clamping
  36. *
  37. * @dataProvider VersionProvider
  38. */
  39. public function testVersionClamp(int $version, int $expected):void{
  40. $o = new QROptions(['version' => $version]);
  41. $this::assertSame($expected, $o->version);
  42. }
  43. /**
  44. * @see testVersionMinMaxClamp()
  45. * @return int[][]
  46. * @internal
  47. */
  48. public function VersionMinMaxProvider():array{
  49. return [
  50. 'normal clamp' => [5, 10, 5, 10],
  51. 'exceeding values' => [-42, 42, 1, 40],
  52. 'min > max' => [10, 5, 5, 10],
  53. 'min > max, exceeding' => [42, -42, 1, 40],
  54. ];
  55. }
  56. /**
  57. * Tests the $versionMin/$versionMax clamping
  58. *
  59. * @dataProvider VersionMinMaxProvider
  60. */
  61. public function testVersionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
  62. $o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
  63. $this::assertSame($expectedMin, $o->versionMin);
  64. $this::assertSame($expectedMax, $o->versionMax);
  65. }
  66. /**
  67. * @see testMaskPatternClamp()
  68. * @return int[][]
  69. * @internal
  70. */
  71. public function MaskPatternProvider():array{
  72. return [
  73. 'exceed max' => [42, 7],
  74. 'exceed min' => [-42, 0],
  75. 'default (-1)' => [QRCode::MASK_PATTERN_AUTO, -1],
  76. ];
  77. }
  78. /**
  79. * Tests the $maskPattern clamping
  80. *
  81. * @dataProvider MaskPatternProvider
  82. */
  83. public function testMaskPatternClamp(int $maskPattern, int $expected):void{
  84. $o = new QROptions(['maskPattern' => $maskPattern]);
  85. $this::assertSame($expected, $o->maskPattern);
  86. }
  87. /**
  88. * Tests if an exception is thrown on an incorrect ECC level
  89. */
  90. public function testInvalidEccLevelException():void{
  91. $this->expectException(QRCodeException::class);
  92. $this->expectExceptionMessage('Invalid error correct level: 42');
  93. $o = new QROptions(['eccLevel' => 42]);
  94. }
  95. /**
  96. * @see testClampRGBValues()
  97. * @return int[][][]
  98. * @internal
  99. */
  100. public function RGBProvider():array{
  101. return [
  102. 'exceeding values' => [[-1, 0, 999], [0, 0 ,255]],
  103. 'too few values' => [[1, 2], [255, 255, 255]],
  104. 'too many values' => [[1, 2, 3, 4, 5], [1, 2, 3]],
  105. ];
  106. }
  107. /**
  108. * Tests clamping of the RGB values for $imageTransparencyBG
  109. *
  110. * @dataProvider RGBProvider
  111. */
  112. public function testClampRGBValues(array $rgb, array $expected):void{
  113. $o = new QROptions(['imageTransparencyBG' => $rgb]);
  114. $this::assertSame($expected, $o->imageTransparencyBG);
  115. }
  116. /**
  117. * Tests if an exception is thrown when a non-numeric RGB value was encoutered
  118. */
  119. public function testInvalidRGBValueException():void{
  120. $this->expectException(QRCodeException::class);
  121. $this->expectExceptionMessage('Invalid RGB value.');
  122. $o = new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
  123. }
  124. }