QROptionsTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\{Test, TestWith};
  17. use PHPUnit\Framework\TestCase;
  18. /**
  19. * QROptions test
  20. */
  21. final class QROptionsTest extends TestCase{
  22. /**
  23. * Tests the $version clamping
  24. */
  25. #[Test]
  26. #[TestWith([42, 40], 'values > 40 should be clamped to 40')]
  27. #[TestWith([-42, 1], 'values < 1 should be clamped to 1')]
  28. #[TestWith([21, 21], 'values in between should not be touched')]
  29. #[TestWith([Version::AUTO, -1], 'value -1 should be treated as is (default)')]
  30. public function versionClamp(int $version, int $expected):void{
  31. $o = new QROptions(['version' => $version]);
  32. $this::assertSame($expected, $o->version);
  33. }
  34. /**
  35. * Tests the $versionMin/$versionMax clamping
  36. */
  37. #[Test]
  38. #[TestWith([5, 10, 5, 10], 'normal clamp')]
  39. #[TestWith([-42, 42, 1, 40], 'exceeding values')]
  40. #[TestWith([10, 5, 5, 10], 'min > max' )]
  41. #[TestWith([42, -42, 1, 40], 'min > max, exceeding')]
  42. public function versionMinMaxClamp(int $versionMin, int $versionMax, int $expectedMin, int $expectedMax):void{
  43. $o = new QROptions(['versionMin' => $versionMin, 'versionMax' => $versionMax]);
  44. $this::assertSame($expectedMin, $o->versionMin);
  45. $this::assertSame($expectedMax, $o->versionMax);
  46. }
  47. /**
  48. * Tests setting the ECC level from string or int
  49. *
  50. * @phan-suppress PhanTypeMismatchPropertyProbablyReal
  51. */
  52. #[Test]
  53. public function setEccLevel():void{
  54. $o = new QROptions(['eccLevel' => EccLevel::H]);
  55. $this::assertSame(EccLevel::H, $o->eccLevel);
  56. /** @phpstan-ignore-next-line */
  57. $o->eccLevel = 'q';
  58. $this::assertSame(EccLevel::Q, $o->eccLevel);
  59. }
  60. /**
  61. * Tests if an exception is thrown when attempting to set an invalid ECC level integer
  62. */
  63. #[Test]
  64. public function setEccLevelFromIntException():void{
  65. $this->expectException(QRCodeException::class);
  66. $this->expectExceptionMessage('Invalid ECC level: "42"');
  67. new QROptions(['eccLevel' => 42]);
  68. }
  69. /**
  70. * Tests if an exception is thrown when attempting to set an invalid ECC level string
  71. */
  72. #[Test]
  73. public function setEccLevelFromStringException():void{
  74. $this->expectException(QRCodeException::class);
  75. $this->expectExceptionMessage('Invalid ECC level: "FOO"');
  76. new QROptions(['eccLevel' => 'foo']);
  77. }
  78. /**
  79. * Tests the clamping (between 0 and 177) of the logo space values
  80. */
  81. #[Test]
  82. #[TestWith([ -1, 0], 'negative')]
  83. #[TestWith([ 0, 0], 'zero')]
  84. #[TestWith([ 69, 69], 'normal')]
  85. #[TestWith([177, 177], 'max')]
  86. #[TestWith([178, 177], 'exceed')]
  87. public function clampLogoSpaceValue(int $value, int $expected):void{
  88. $o = new QROptions;
  89. foreach(['logoSpaceWidth', 'logoSpaceHeight', 'logoSpaceStartX', 'logoSpaceStartY'] as $prop){
  90. $o->{$prop} = $value;
  91. $this::assertSame($expected, $o->{$prop});
  92. }
  93. }
  94. /**
  95. * Tests if the optional logo space start values are nullable
  96. */
  97. #[Test]
  98. public function logoSpaceStartNullable():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. * Tests clamping of the circle radius
  112. */
  113. #[Test]
  114. #[TestWith([0.0, 0.1], 'min')]
  115. #[TestWith([0.5, 0.5], 'no clamp')]
  116. #[TestWith([1.5, 0.75], 'max')]
  117. public function clampCircleRadius(float $value, float $expected):void{
  118. $o = new QROptions(['circleRadius' => $value]);
  119. $this::assertSame($expected, $o->circleRadius);
  120. }
  121. }