QROptionsTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Class QROptionsTest
  4. *
  5. * @filesource QROptionsTest.php
  6. * @created 08.11.2018
  7. * @package chillerlan\QRCodeTest
  8. * @author smiley <smiley@chillerlan.net>
  9. * @copyright 2018 smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCodeTest;
  13. use chillerlan\Settings\SettingsContainerInterface;
  14. use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
  15. use PHPUnit\Framework\TestCase;
  16. class QROptionsTest extends TestCase{
  17. /** @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions */
  18. protected SettingsContainerInterface $options;
  19. public function testVersionClamp():void{
  20. $o = new QROptions(['version' => 42]);
  21. $this::assertSame(40, $o->version);
  22. $o = new QROptions(['version' => -42]);
  23. $this::assertSame(1, $o->version);
  24. $o = new QROptions(['version' => 21]);
  25. $this::assertSame(21, $o->version);
  26. // QRCode::VERSION_AUTO = -1, default
  27. $o = new QROptions;
  28. $this::assertSame(QRCode::VERSION_AUTO, $o->version);
  29. }
  30. public function testVersionMinMaxClamp():void{
  31. // normal clamp
  32. $o = new QROptions(['versionMin' => 5, 'versionMax' => 10]);
  33. $this::assertSame(5, $o->versionMin);
  34. $this::assertSame(10, $o->versionMax);
  35. // exceeding values
  36. $o = new QROptions(['versionMin' => -42, 'versionMax' => 42]);
  37. $this::assertSame(1, $o->versionMin);
  38. $this::assertSame(40, $o->versionMax);
  39. // min > max
  40. $o = new QROptions(['versionMin' => 10, 'versionMax' => 5]);
  41. $this::assertSame(5, $o->versionMin);
  42. $this::assertSame(10, $o->versionMax);
  43. $o = new QROptions(['versionMin' => 42, 'versionMax' => -42]);
  44. $this::assertSame(1, $o->versionMin);
  45. $this::assertSame(40, $o->versionMax);
  46. }
  47. public function testMaskPatternClamp():void{
  48. $o = new QROptions(['maskPattern' => 42]);
  49. $this::assertSame(7, $o->maskPattern);
  50. $o = new QROptions(['maskPattern' => -42]);
  51. $this::assertSame(0, $o->maskPattern);
  52. // QRCode::MASK_PATTERN_AUTO = -1, default
  53. $o = new QROptions;
  54. $this::assertSame(QRCode::MASK_PATTERN_AUTO, $o->maskPattern);
  55. }
  56. public function testInvalidEccLevelException():void{
  57. $this->expectException(QRCodeException::class);
  58. $this->expectExceptionMessage('Invalid error correct level: 42');
  59. /** @noinspection PhpUnusedLocalVariableInspection */
  60. $o = new QROptions(['eccLevel' => 42]);
  61. }
  62. public function testClampRGBValues():void{
  63. $o = new QROptions(['imageTransparencyBG' => [-1, 0, 999]]);
  64. $this::assertSame(0, $o->imageTransparencyBG[0]);
  65. $this::assertSame(0, $o->imageTransparencyBG[1]);
  66. $this::assertSame(255, $o->imageTransparencyBG[2]);
  67. }
  68. public function testInvalidRGBValueException():void{
  69. $this->expectException(QRCodeException::class);
  70. $this->expectExceptionMessage('Invalid RGB value.');
  71. /** @noinspection PhpUnusedLocalVariableInspection */
  72. $o = new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
  73. }
  74. }