QROptionsTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. declare(strict_types=1);
  13. namespace chillerlan\QRCodeTest;
  14. use chillerlan\QRCode\{QRCodeException, QROptions};
  15. use chillerlan\QRCode\Common\{EccLevel, Version};
  16. use PHPUnit\Framework\Attributes\DataProvider;
  17. use PHPUnit\Framework\TestCase;
  18. /**
  19. * QROptions test
  20. */
  21. final class QROptionsTest extends TestCase{
  22. /**
  23. * @return int[][]
  24. */
  25. public static function VersionProvider():array{
  26. return [
  27. 'values > 40 should be clamped to 40' => [42, 40],
  28. 'values < 1 should be clamped to 1' => [-42, 1],
  29. 'values in between shold not be touched' => [21, 21],
  30. 'value -1 should be treated as is (default)' => [Version::AUTO, -1],
  31. ];
  32. }
  33. /**
  34. * Tests the $version clamping
  35. */
  36. #[DataProvider('VersionProvider')]
  37. public function testVersionClamp(int $version, int $expected):void{
  38. $o = new QROptions(['version' => $version]);
  39. $this::assertSame($expected, $o->version);
  40. }
  41. /**
  42. * @return int[][]
  43. */
  44. public static function VersionMinMaxProvider():array{
  45. return [
  46. 'normal clamp' => [5, 10, 5, 10],
  47. 'exceeding values' => [-42, 42, 1, 40],
  48. 'min > max' => [10, 5, 5, 10],
  49. 'min > max, exceeding' => [42, -42, 1, 40],
  50. ];
  51. }
  52. /**
  53. * Tests the $versionMin/$versionMax clamping
  54. */
  55. #[DataProvider('VersionMinMaxProvider')]
  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. * Tests setting the ECC level from string or int
  63. */
  64. public function testSetEccLevel():void{
  65. $o = new QROptions(['eccLevel' => EccLevel::H]);
  66. $this::assertSame(EccLevel::H, $o->eccLevel);
  67. /** @phpstan-ignore-next-line */
  68. $o->eccLevel = 'q';
  69. $this::assertSame(EccLevel::Q, $o->eccLevel);
  70. }
  71. /**
  72. * Tests if an exception is thrown when attempting to set an invalid ECC level integer
  73. */
  74. public function testSetEccLevelFromIntException():void{
  75. $this->expectException(QRCodeException::class);
  76. $this->expectExceptionMessage('Invalid ECC level: "42"');
  77. new QROptions(['eccLevel' => 42]);
  78. }
  79. /**
  80. * Tests if an exception is thrown when attempting to set an invalid ECC level string
  81. */
  82. public function testSetEccLevelFromStringException():void{
  83. $this->expectException(QRCodeException::class);
  84. $this->expectExceptionMessage('Invalid ECC level: "FOO"');
  85. new QROptions(['eccLevel' => 'foo']);
  86. }
  87. /**
  88. * @return int[][][]
  89. */
  90. public static 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. * @return int[][]
  99. */
  100. public static function logoSpaceValueProvider():array{
  101. return [
  102. 'negative' => [ -1, 0],
  103. 'zero' => [ 0, 0],
  104. 'normal' => [ 69, 69],
  105. 'max' => [177, 177],
  106. 'exceed' => [178, 177],
  107. ];
  108. }
  109. /**
  110. * Tests the clamping (between 0 and 177) of the logo space values
  111. */
  112. #[DataProvider('logoSpaceValueProvider')]
  113. public function testClampLogoSpaceValue(int $value, int $expected):void{
  114. $o = new QROptions;
  115. foreach(['logoSpaceWidth', 'logoSpaceHeight', 'logoSpaceStartX', 'logoSpaceStartY'] as $prop){
  116. $o->{$prop} = $value;
  117. $this::assertSame($expected, $o->{$prop});
  118. }
  119. }
  120. /**
  121. * Tests if the optional logo space start values are nullable
  122. */
  123. public function testLogoSpaceStartNullable():void{
  124. $o = new QROptions([
  125. 'logoSpaceStartX' => 42,
  126. 'logoSpaceStartY' => 69,
  127. ]);
  128. $this::assertSame(42, $o->logoSpaceStartX);
  129. $this::assertSame(69, $o->logoSpaceStartY);
  130. $o->logoSpaceStartX = null;
  131. $o->logoSpaceStartY = null;
  132. $this::assertNull($o->logoSpaceStartX);
  133. $this::assertNull($o->logoSpaceStartY);
  134. }
  135. /**
  136. * @return float[][]
  137. */
  138. public static function circleRadiusProvider():array{
  139. return [
  140. [0.0, 0.1],
  141. [0.5, 0.5],
  142. [1.5, 0.75],
  143. ];
  144. }
  145. /**
  146. * Tests clamping of the circle radius
  147. */
  148. #[DataProvider('circleRadiusProvider')]
  149. public function testClampCircleRadius(float $value, float $expected):void{
  150. $o = new QROptions(['circleRadius' => $value]);
  151. $this::assertSame($expected, $o->circleRadius);
  152. }
  153. }