QROptionsTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(){
  20. $this->assertSame(40, (new QROptions(['version' => 42]))->version);
  21. $this->assertSame(1, (new QROptions(['version' => -42]))->version);
  22. $this->assertSame(21, (new QROptions(['version' => 21]))->version);
  23. $this->assertSame(QRCode::VERSION_AUTO, (new QROptions)->version); // QRCode::VERSION_AUTO = -1, default
  24. }
  25. public function testVersionMinMaxClamp(){
  26. // normal clamp
  27. $o = new QROptions(['versionMin' => 5, 'versionMax' => 10]);
  28. $this->assertSame(5, $o->versionMin);
  29. $this->assertSame(10, $o->versionMax);
  30. // exceeding values
  31. $o = new QROptions(['versionMin' => -42, 'versionMax' => 42]);
  32. $this->assertSame(1, $o->versionMin);
  33. $this->assertSame(40, $o->versionMax);
  34. // min > max
  35. $o = new QROptions(['versionMin' => 10, 'versionMax' => 5]);
  36. $this->assertSame(5, $o->versionMin);
  37. $this->assertSame(10, $o->versionMax);
  38. $o = new QROptions(['versionMin' => 42, 'versionMax' => -42]);
  39. $this->assertSame(1, $o->versionMin);
  40. $this->assertSame(40, $o->versionMax);
  41. }
  42. public function testMaskPatternClamp(){
  43. $this->assertSame(7, (new QROptions(['maskPattern' => 42]))->maskPattern);
  44. $this->assertSame(0, (new QROptions(['maskPattern' => -42]))->maskPattern);
  45. $this->assertSame(QRCode::MASK_PATTERN_AUTO, (new QROptions)->maskPattern); // QRCode::MASK_PATTERN_AUTO = -1, default
  46. }
  47. public function testInvalidEccLevelException(){
  48. $this->expectException(QRCodeException::class);
  49. $this->expectExceptionMessage('Invalid error correct level: 42');
  50. new QROptions(['eccLevel' => 42]);
  51. }
  52. public function testClampRGBValues(){
  53. $o = new QROptions(['imageTransparencyBG' => [-1, 0, 999]]);
  54. $this->assertSame(0, $o->imageTransparencyBG[0]);
  55. $this->assertSame(0, $o->imageTransparencyBG[1]);
  56. $this->assertSame(255, $o->imageTransparencyBG[2]);
  57. }
  58. public function testInvalidRGBValueException(){
  59. $this->expectException(QRCodeException::class);
  60. $this->expectExceptionMessage('Invalid RGB value.');
  61. new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
  62. }
  63. }