QROptionsTest.php 4.3 KB

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