QROptionsTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, QRCodeException, 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. public function testInvalidEccLevelException(){
  49. $this->expectException(QRCodeException::class);
  50. $this->expectExceptionMessage('Invalid error correct level: 42');
  51. new QROptions(['eccLevel' => 42]);
  52. }
  53. public function testClampRGBValues(){
  54. $o = new QROptions(['imageTransparencyBG' => [-1, 0, 999]]);
  55. $this->assertSame(0, $o->imageTransparencyBG[0]);
  56. $this->assertSame(0, $o->imageTransparencyBG[1]);
  57. $this->assertSame(255, $o->imageTransparencyBG[2]);
  58. }
  59. public function testInvalidRGBValueException(){
  60. $this->expectException(QRCodeException::class);
  61. $this->expectExceptionMessage('Invalid RGB value.');
  62. new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
  63. }
  64. }