QROptionsTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\QRCode\{QRCode,QROptions};
  14. use PHPUnit\Framework\TestCase;
  15. class QROptionsTest extends TestCase{
  16. /**
  17. * @var \chillerlan\QRCode\QROptions
  18. */
  19. protected $options;
  20. public function testVersionClamp(){
  21. $this->assertSame(40, (new QROptions(['version' => 42]))->version);
  22. $this->assertSame(1, (new QROptions(['version' => -42]))->version);
  23. $this->assertSame(21, (new QROptions(['version' => 21]))->version);
  24. $this->assertSame(QRCode::VERSION_AUTO, (new QROptions(['version' => QRCode::VERSION_AUTO]))->version); // -1
  25. }
  26. public function testVersionMinMaxClamp(){
  27. // normal clamp
  28. $o = new QROptions(['versionMin' => 5, 'versionMax' => 10]);
  29. $this->assertSame(5, $o->versionMin);
  30. $this->assertSame(10, $o->versionMax);
  31. // exceeding values
  32. $o = new QROptions(['versionMin' => -42, 'versionMax' => 42]);
  33. $this->assertSame(1, $o->versionMin);
  34. $this->assertSame(40, $o->versionMax);
  35. // min > max
  36. $o = new QROptions(['versionMin' => 10, 'versionMax' => 5]);
  37. $this->assertSame(5, $o->versionMin);
  38. $this->assertSame(10, $o->versionMax);
  39. $o = new QROptions(['versionMin' => 42, 'versionMax' => -42]);
  40. $this->assertSame(1, $o->versionMin);
  41. $this->assertSame(40, $o->versionMax);
  42. }
  43. public function testMaskPatternClamp(){
  44. $o = new QROptions(['maskPattern' => 42]);
  45. $this->assertSame(7, $o->maskPattern);
  46. $o = new QROptions(['maskPattern' => -42]);
  47. $this->assertSame(0, $o->maskPattern);
  48. $o = new QROptions(['maskPattern' => QRCode::MASK_PATTERN_AUTO]); // -1
  49. $this->assertSame(QRCode::MASK_PATTERN_AUTO, $o->maskPattern);
  50. }
  51. /**
  52. * @expectedException \chillerlan\QRCode\QRCodeException
  53. * @expectedExceptionMessage Invalid error correct level: 42
  54. */
  55. public function testInvalidEccLevelException(){
  56. new QROptions(['eccLevel' => 42]);
  57. }
  58. public function testClampRGBValues(){
  59. $o = new QROptions(['imageTransparencyBG' => [-1, 0, 999]]);
  60. $this->assertSame(0, $o->imageTransparencyBG[0]);
  61. $this->assertSame(0, $o->imageTransparencyBG[1]);
  62. $this->assertSame(255, $o->imageTransparencyBG[2]);
  63. }
  64. /**
  65. * @expectedException \chillerlan\QRCode\QRCodeException
  66. * @expectedExceptionMessage Invalid RGB value.
  67. */
  68. public function testInvalidRGBValueException(){
  69. new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
  70. }
  71. }