summaryrefslogtreecommitdiff
path: root/vendor/chillerlan/php-qrcode/tests/QROptionsTest.php
blob: 41bf69ced62d03be970cb4826bf5933e3652d553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
 * Class QROptionsTest
 *
 * @filesource   QROptionsTest.php
 * @created      08.11.2018
 * @package      chillerlan\QRCodeTest
 * @author       smiley <[email protected]>
 * @copyright    2018 smiley
 * @license      MIT
 */

namespace chillerlan\QRCodeTest;

use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
use PHPUnit\Framework\TestCase;

class QROptionsTest extends TestCase{

	/**
	 * @var \chillerlan\QRCode\QROptions
	 */
	protected $options;

	public function testVersionClamp(){
		$this->assertSame(40, (new QROptions(['version' => 42]))->version);
		$this->assertSame(1, (new QROptions(['version' => -42]))->version);
		$this->assertSame(21, (new QROptions(['version' => 21]))->version);
		$this->assertSame(QRCode::VERSION_AUTO, (new QROptions)->version); // QRCode::VERSION_AUTO = -1, default
	}

	public function testVersionMinMaxClamp(){
		// normal clamp
		$o = new QROptions(['versionMin' => 5, 'versionMax' => 10]);
		$this->assertSame(5, $o->versionMin);
		$this->assertSame(10, $o->versionMax);

		// exceeding values
		$o = new QROptions(['versionMin' => -42, 'versionMax' => 42]);
		$this->assertSame(1, $o->versionMin);
		$this->assertSame(40, $o->versionMax);

		// min > max
		$o = new QROptions(['versionMin' => 10, 'versionMax' => 5]);
		$this->assertSame(5, $o->versionMin);
		$this->assertSame(10, $o->versionMax);

		$o = new QROptions(['versionMin' => 42, 'versionMax' => -42]);
		$this->assertSame(1, $o->versionMin);
		$this->assertSame(40, $o->versionMax);
	}

	public function testMaskPatternClamp(){
		$this->assertSame(7, (new QROptions(['maskPattern' => 42]))->maskPattern);
		$this->assertSame(0, (new QROptions(['maskPattern' => -42]))->maskPattern);
		$this->assertSame(QRCode::MASK_PATTERN_AUTO, (new QROptions)->maskPattern); // QRCode::MASK_PATTERN_AUTO = -1, default
	}

	public function testInvalidEccLevelException(){
		$this->expectException(QRCodeException::class);
		$this->expectExceptionMessage('Invalid error correct level: 42');

		new QROptions(['eccLevel' => 42]);
	}

	public function testClampRGBValues(){
		$o = new QROptions(['imageTransparencyBG' => [-1, 0, 999]]);

		$this->assertSame(0, $o->imageTransparencyBG[0]);
		$this->assertSame(0, $o->imageTransparencyBG[1]);
		$this->assertSame(255, $o->imageTransparencyBG[2]);
	}

	public function testInvalidRGBValueException(){
		$this->expectException(QRCodeException::class);
		$this->expectExceptionMessage('Invalid RGB value.');

		new QROptions(['imageTransparencyBG' => ['r', 'g', 'b']]);
	}
}