QROptionsTest.php 4.5 KB

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