QROptionsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * @phan-suppress PhanTypeMismatchPropertyProbablyReal
  65. */
  66. public function testSetEccLevel():void{
  67. $o = new QROptions(['eccLevel' => EccLevel::H]);
  68. $this::assertSame(EccLevel::H, $o->eccLevel);
  69. /** @phpstan-ignore-next-line */
  70. $o->eccLevel = 'q';
  71. $this::assertSame(EccLevel::Q, $o->eccLevel);
  72. }
  73. /**
  74. * Tests if an exception is thrown when attempting to set an invalid ECC level integer
  75. */
  76. public function testSetEccLevelFromIntException():void{
  77. $this->expectException(QRCodeException::class);
  78. $this->expectExceptionMessage('Invalid ECC level: "42"');
  79. new QROptions(['eccLevel' => 42]);
  80. }
  81. /**
  82. * Tests if an exception is thrown when attempting to set an invalid ECC level string
  83. */
  84. public function testSetEccLevelFromStringException():void{
  85. $this->expectException(QRCodeException::class);
  86. $this->expectExceptionMessage('Invalid ECC level: "FOO"');
  87. new QROptions(['eccLevel' => 'foo']);
  88. }
  89. /**
  90. * @return int[][][]
  91. */
  92. public static function RGBProvider():array{
  93. return [
  94. 'exceeding values' => [[-1, 0, 999], [0, 0 ,255]],
  95. 'too few values' => [[1, 2], [255, 255, 255]],
  96. 'too many values' => [[1, 2, 3, 4, 5], [1, 2, 3]],
  97. ];
  98. }
  99. /**
  100. * @return int[][]
  101. */
  102. public static function logoSpaceValueProvider():array{
  103. return [
  104. 'negative' => [ -1, 0],
  105. 'zero' => [ 0, 0],
  106. 'normal' => [ 69, 69],
  107. 'max' => [177, 177],
  108. 'exceed' => [178, 177],
  109. ];
  110. }
  111. /**
  112. * Tests the clamping (between 0 and 177) of the logo space values
  113. */
  114. #[DataProvider('logoSpaceValueProvider')]
  115. public function testClampLogoSpaceValue(int $value, int $expected):void{
  116. $o = new QROptions;
  117. foreach(['logoSpaceWidth', 'logoSpaceHeight', 'logoSpaceStartX', 'logoSpaceStartY'] as $prop){
  118. $o->{$prop} = $value;
  119. $this::assertSame($expected, $o->{$prop});
  120. }
  121. }
  122. /**
  123. * Tests if the optional logo space start values are nullable
  124. */
  125. public function testLogoSpaceStartNullable():void{
  126. $o = new QROptions([
  127. 'logoSpaceStartX' => 42,
  128. 'logoSpaceStartY' => 69,
  129. ]);
  130. $this::assertSame(42, $o->logoSpaceStartX);
  131. $this::assertSame(69, $o->logoSpaceStartY);
  132. $o->logoSpaceStartX = null;
  133. $o->logoSpaceStartY = null;
  134. $this::assertNull($o->logoSpaceStartX);
  135. $this::assertNull($o->logoSpaceStartY);
  136. }
  137. /**
  138. * @return float[][]
  139. */
  140. public static function circleRadiusProvider():array{
  141. return [
  142. [0.0, 0.1],
  143. [0.5, 0.5],
  144. [1.5, 0.75],
  145. ];
  146. }
  147. /**
  148. * Tests clamping of the circle radius
  149. */
  150. #[DataProvider('circleRadiusProvider')]
  151. public function testClampCircleRadius(float $value, float $expected):void{
  152. $o = new QROptions(['circleRadius' => $value]);
  153. $this::assertSame($expected, $o->circleRadius);
  154. }
  155. }