QROptionsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\{QRCodeException, QROptions};
  14. use chillerlan\QRCode\Common\Version;
  15. use PHPUnit\Framework\TestCase;
  16. /**
  17. * QROptions test
  18. */
  19. final class QROptionsTest extends TestCase{
  20. /**
  21. * @return int[][]
  22. */
  23. public static function VersionProvider():array{
  24. return [
  25. 'values > 40 should be clamped to 40' => [42, 40],
  26. 'values < 1 should be clamped to 1' => [-42, 1],
  27. 'values in between shold not be touched' => [21, 21],
  28. 'value -1 should be treated as is (default)' => [Version::AUTO, -1],
  29. ];
  30. }
  31. /**
  32. * Tests the $version clamping
  33. *
  34. * @dataProvider VersionProvider
  35. */
  36. public function testVersionClamp(int $version, int $expected):void{
  37. $o = new QROptions(['version' => $version]);
  38. $this::assertSame($expected, $o->version);
  39. }
  40. /**
  41. * @return int[][]
  42. */
  43. public static function VersionMinMaxProvider():array{
  44. return [
  45. 'normal clamp' => [5, 10, 5, 10],
  46. 'exceeding values' => [-42, 42, 1, 40],
  47. 'min > max' => [10, 5, 5, 10],
  48. 'min > max, exceeding' => [42, -42, 1, 40],
  49. ];
  50. }
  51. /**
  52. * Tests the $versionMin/$versionMax clamping
  53. *
  54. * @dataProvider VersionMinMaxProvider
  55. */
  56. public function testVersionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
  57. $o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
  58. $this::assertSame($expectedMin, $o->versionMin);
  59. $this::assertSame($expectedMax, $o->versionMax);
  60. }
  61. /**
  62. * @return int[][][]
  63. */
  64. public static function RGBProvider():array{
  65. return [
  66. 'exceeding values' => [[-1, 0, 999], [0, 0 ,255]],
  67. 'too few values' => [[1, 2], [255, 255, 255]],
  68. 'too many values' => [[1, 2, 3, 4, 5], [1, 2, 3]],
  69. ];
  70. }
  71. /**
  72. * @return int[][]
  73. */
  74. public static function logoSpaceValueProvider():array{
  75. return [
  76. 'negative' => [ -1, 0],
  77. 'zero' => [ 0, 0],
  78. 'normal' => [ 69, 69],
  79. 'max' => [177, 177],
  80. 'exceed' => [178, 177],
  81. ];
  82. }
  83. /**
  84. * Tests the clamping (between 0 and 177) of the logo space values
  85. *
  86. * @dataProvider logoSpaceValueProvider
  87. */
  88. public function testClampLogoSpaceValue(int $value, int $expected):void{
  89. $o = new QROptions;
  90. foreach(['logoSpaceWidth', 'logoSpaceHeight', 'logoSpaceStartX', 'logoSpaceStartY'] as $prop){
  91. $o->{$prop} = $value;
  92. $this::assertSame($expected, $o->{$prop});
  93. }
  94. }
  95. /**
  96. * Tests if the optional logo space start values are nullable
  97. */
  98. public function testLogoSpaceStartNullable():void{
  99. $o = new QROptions([
  100. 'logoSpaceStartX' => 42,
  101. 'logoSpaceStartY' => 69,
  102. ]);
  103. $this::assertSame(42, $o->logoSpaceStartX);
  104. $this::assertSame(69, $o->logoSpaceStartY);
  105. $o->logoSpaceStartX = null;
  106. $o->logoSpaceStartY = null;
  107. $this::assertNull($o->logoSpaceStartX);
  108. $this::assertNull($o->logoSpaceStartY);
  109. }
  110. /**
  111. * @return float[][]
  112. */
  113. public static function circleRadiusProvider():array{
  114. return [
  115. [0.0, 0.1],
  116. [0.5, 0.5],
  117. [1.5, 0.75],
  118. ];
  119. }
  120. /**
  121. * Tests clamping of the circle radius
  122. *
  123. * @dataProvider circleRadiusProvider
  124. */
  125. public function testClampCircleRadius(float $value, float $expected):void{
  126. $o = new QROptions(['circleRadius' => $value]);
  127. $this::assertSame($expected, $o->circleRadius);
  128. }
  129. }