QROptionsTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = -1, default
  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. $this->assertSame(7, (new QROptions(['maskPattern' => 42]))->maskPattern);
  45. $this->assertSame(0, (new QROptions(['maskPattern' => -42]))->maskPattern);
  46. $this->assertSame(QRCode::MASK_PATTERN_AUTO, (new QROptions)->maskPattern); // QRCode::MASK_PATTERN_AUTO = -1, default
  47. }
  48. /**
  49. * @expectedException \chillerlan\QRCode\QRCodeException
  50. * @expectedExceptionMessage Invalid error correct level: 42
  51. */
  52. public function testInvalidEccLevelException(){
  53. new QROptions(['eccLevel' => 42]);
  54. }
  55. public function testClampRGBValues(){
  56. $o = new QROptions(['imageTransparencyBG' => [-1, 0, 999]]);
  57. $this->assertSame(0, $o->imageTransparencyBG[0]);
  58. $this->assertSame(0, $o->imageTransparencyBG[1]);
  59. $this->assertSame(255, $o->imageTransparencyBG[2]);
  60. }
  61. /**
  62. * @expectedException \chillerlan\QRCode\QRCodeException
  63. * @expectedExceptionMessage Invalid RGB value.
  64. */
  65. public function testInvalidRGBValueException(){
  66. new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
  67. }
  68. }