QROptionsTest.php 4.5 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\{QRCodeException, QROptions};
  14. use chillerlan\QRCode\Common\{EccLevel, Version};
  15. use PHPUnit\Framework\Attributes\DataProvider;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * QROptions test
  19. */
  20. final class QROptionsTest extends TestCase{
  21. /**
  22. * @return int[][]
  23. */
  24. public static 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)' => [Version::AUTO, -1],
  30. ];
  31. }
  32. /**
  33. * Tests the $version clamping
  34. */
  35. #[DataProvider('VersionProvider')]
  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. 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. * Tests setting the ECC level from string or int
  62. */
  63. public function testSetEccLevel():void{
  64. $o = new QROptions(['eccLevel' => EccLevel::H]);
  65. $this::assertSame(EccLevel::H, $o->eccLevel);
  66. $o->eccLevel = 'q';
  67. $this::assertSame(EccLevel::Q, $o->eccLevel);
  68. }
  69. /**
  70. * Tests if an exception is thrown when attempting to set an invalid ECC level integer
  71. */
  72. public function testSetEccLevelFromIntException():void{
  73. $this->expectException(QRCodeException::class);
  74. $this->expectExceptionMessage('Invalid ECC level: "42"');
  75. /** @phan-suppress-next-line PhanNoopNew */
  76. new QROptions(['eccLevel' => 42]);
  77. }
  78. /**
  79. * Tests if an exception is thrown when attempting to set an invalid ECC level string
  80. */
  81. public function testSetEccLevelFromStringException():void{
  82. $this->expectException(QRCodeException::class);
  83. $this->expectExceptionMessage('Invalid ECC level: "FOO"');
  84. /** @phan-suppress-next-line PhanNoopNew */
  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. }