QROptionsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Common\EccLevel;
  14. use chillerlan\QRCode\QRCodeException;
  15. use chillerlan\QRCode\QROptions;
  16. use chillerlan\QRCode\Common\Version;
  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. */
  38. public function testVersionClamp(int $version, int $expected):void{
  39. $o = new QROptions(['version' => $version]);
  40. $this::assertSame($expected, $o->version);
  41. }
  42. /**
  43. * @return int[][]
  44. */
  45. public static function VersionMinMaxProvider():array{
  46. return [
  47. 'normal clamp' => [5, 10, 5, 10],
  48. 'exceeding values' => [-42, 42, 1, 40],
  49. 'min > max' => [10, 5, 5, 10],
  50. 'min > max, exceeding' => [42, -42, 1, 40],
  51. ];
  52. }
  53. /**
  54. * Tests the $versionMin/$versionMax clamping
  55. *
  56. * @dataProvider VersionMinMaxProvider
  57. */
  58. public function testVersionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
  59. $o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
  60. $this::assertSame($expectedMin, $o->versionMin);
  61. $this::assertSame($expectedMax, $o->versionMax);
  62. }
  63. /**
  64. * Tests setting the ECC level from string or int
  65. */
  66. public function testSetEccLevel():void{
  67. $o = new QROptions(['eccLevel' => EccLevel::H]);
  68. $this::assertSame(EccLevel::H, $o->eccLevel);
  69. $o->eccLevel = 'q';
  70. $this::assertSame(EccLevel::Q, $o->eccLevel);
  71. }
  72. /**
  73. * Tests if an exception is thrown when attempting to set an invalid ECC level integer
  74. */
  75. public function testSetEccLevelFromIntException():void{
  76. $this->expectException(QRCodeException::class);
  77. $this->expectExceptionMessage('Invalid ECC level: "42"');
  78. /** @phan-suppress-next-line PhanNoopNew */
  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. /** @phan-suppress-next-line PhanNoopNew */
  88. new QROptions(['eccLevel' => 'foo']);
  89. }
  90. /**
  91. * @return int[][][]
  92. */
  93. public static function RGBProvider():array{
  94. return [
  95. 'exceeding values' => [[-1, 0, 999], [0, 0 ,255]],
  96. 'too few values' => [[1, 2], [255, 255, 255]],
  97. 'too many values' => [[1, 2, 3, 4, 5], [1, 2, 3]],
  98. ];
  99. }
  100. /**
  101. * @return int[][]
  102. */
  103. public static function logoSpaceValueProvider():array{
  104. return [
  105. 'negative' => [ -1, 0],
  106. 'zero' => [ 0, 0],
  107. 'normal' => [ 69, 69],
  108. 'max' => [177, 177],
  109. 'exceed' => [178, 177],
  110. ];
  111. }
  112. /**
  113. * Tests the clamping (between 0 and 177) of the logo space values
  114. *
  115. * @dataProvider logoSpaceValueProvider
  116. */
  117. public function testClampLogoSpaceValue(int $value, int $expected):void{
  118. $o = new QROptions;
  119. foreach(['logoSpaceWidth', 'logoSpaceHeight', 'logoSpaceStartX', 'logoSpaceStartY'] as $prop){
  120. $o->{$prop} = $value;
  121. $this::assertSame($expected, $o->{$prop});
  122. }
  123. }
  124. /**
  125. * Tests if the optional logo space start values are nullable
  126. */
  127. public function testLogoSpaceStartNullable():void{
  128. $o = new QROptions([
  129. 'logoSpaceStartX' => 42,
  130. 'logoSpaceStartY' => 69,
  131. ]);
  132. $this::assertSame(42, $o->logoSpaceStartX);
  133. $this::assertSame(69, $o->logoSpaceStartY);
  134. $o->logoSpaceStartX = null;
  135. $o->logoSpaceStartY = null;
  136. $this::assertNull($o->logoSpaceStartX);
  137. $this::assertNull($o->logoSpaceStartY);
  138. }
  139. /**
  140. * @return float[][]
  141. */
  142. public static function circleRadiusProvider():array{
  143. return [
  144. [0.0, 0.1],
  145. [0.5, 0.5],
  146. [1.5, 0.75],
  147. ];
  148. }
  149. /**
  150. * Tests clamping of the circle radius
  151. *
  152. * @dataProvider circleRadiusProvider
  153. */
  154. public function testClampCircleRadius(float $value, float $expected):void{
  155. $o = new QROptions(['circleRadius' => $value]);
  156. $this::assertSame($expected, $o->circleRadius);
  157. }
  158. }