1 | <?php |
||
20 | class QRCodeTest extends \PHPUnit_Framework_TestCase{ |
||
21 | |||
22 | /** |
||
23 | * @var \chillerlan\QRCode\QROptions |
||
24 | */ |
||
25 | protected $options; |
||
26 | |||
27 | /** |
||
28 | * @var \chillerlan\QRCode\Output\QROutputInterface |
||
29 | */ |
||
30 | protected $output; |
||
31 | |||
32 | /** |
||
33 | * @var \ReflectionClass |
||
34 | */ |
||
35 | protected $reflectionClass; |
||
36 | |||
37 | protected function setUp(){ |
||
38 | $this->options = new QROptions; |
||
39 | $this->output = new QRString; |
||
40 | $this->reflectionClass = new ReflectionClass(QRCode::class); |
||
41 | } |
||
42 | |||
43 | public function testInstance(){ |
||
44 | $this->assertInstanceOf(QRString::class, $this->output); |
||
45 | $this->assertInstanceOf(QROptions::class, $this->options); |
||
46 | $this->assertInstanceOf(QRCode::class, $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options])); |
||
47 | } |
||
48 | |||
49 | public function stringDataProvider(){ |
||
50 | return [ |
||
51 | ['1234567890'], |
||
52 | ['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'], |
||
53 | ['#\\'], |
||
54 | ['茗荷'], |
||
55 | ]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @dataProvider stringDataProvider |
||
60 | */ |
||
61 | public function testDataCoverage($data){ |
||
62 | (new QRCode($data, $this->output))->getRawData(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @dataProvider stringDataProvider |
||
67 | */ |
||
68 | public function testTypeAndErrorcorrectlevelCoverage($data){ |
||
69 | foreach(QRConst::MAX_BITS as $type => $x){ |
||
70 | foreach(QRConst::RSBLOCK as $eclevel => $y){ |
||
71 | $this->options->typeNumber = $type; |
||
72 | $this->options->errorCorrectLevel = $eclevel; |
||
73 | $this->assertInstanceOf(QRCode::class, new QRCode($data, $this->output, $this->options)); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @dataProvider stringDataProvider |
||
80 | */ |
||
81 | public function testTypeAutoOverride($data){ |
||
82 | $this->options->typeNumber = QRCode::TYPE_05; |
||
83 | new QRCode($data, $this->output, $this->options); |
||
84 | } |
||
85 | |||
86 | |||
87 | public function getTypeNumberDataProvider(){ |
||
88 | return [ |
||
89 | [true, QRCode::TYPE_05, 'foobar'], |
||
90 | [false, QRCode::TYPE_05, 'foobar'], |
||
91 | [true, QRCode::TYPE_10, 'foobar'], |
||
92 | [false, QRCode::TYPE_10, 'foobar'], |
||
93 | [true, QRCode::TYPE_05, '1234567890'], |
||
94 | [false, QRCode::TYPE_05, '1234567890'], |
||
95 | [true, QRCode::TYPE_10, '1234567890'], |
||
96 | [false, QRCode::TYPE_10, '1234567890'], |
||
97 | [true, QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'], |
||
98 | [false, QRCode::TYPE_05, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'], |
||
99 | [true, QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'], |
||
100 | [false, QRCode::TYPE_10, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'], |
||
101 | [true, QRCode::TYPE_05, '茗荷'], |
||
102 | [false, QRCode::TYPE_05, '茗荷'], |
||
103 | [true, QRCode::TYPE_10, '茗荷'], |
||
104 | [false, QRCode::TYPE_10, '茗荷'], |
||
105 | ]; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @dataProvider getTypeNumberDataProvider |
||
110 | */ |
||
111 | public function testInternalGetTypeNumber($test, $type, $data){ |
||
112 | $method = $this->reflectionClass->getMethod('getMatrix'); |
||
113 | $method->setAccessible(true); |
||
114 | $this->options->typeNumber = $type; |
||
115 | |||
116 | for($i = 0; $i <= 7; $i++){ |
||
117 | $method->invokeArgs($this->reflectionClass->newInstanceArgs([$data, $this->output, $this->options]), [$test, $i]); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @expectedException \chillerlan\QRCode\QRCodeException |
||
123 | * @expectedExceptionMessage No data given. |
||
124 | */ |
||
125 | public function testNoDataException(){ |
||
126 | $this->reflectionClass->newInstanceArgs(['', $this->output]); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @expectedException \chillerlan\QRCode\QRCodeException |
||
131 | * @expectedExceptionMessage Invalid error correct level: 42 |
||
132 | */ |
||
133 | public function testErrorCorrectLevelException(){ |
||
134 | $this->options->errorCorrectLevel = 42; |
||
135 | $this->reflectionClass->newInstanceArgs(['foobar', $this->output, $this->options]); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @expectedException \chillerlan\QRCode\QRCodeException |
||
140 | * @expectedExceptionMessage code length overflow. (261 > 72bit) |
||
141 | */ |
||
142 | public function testCodeLengthOverflowException(){ |
||
143 | $this->options->typeNumber = QRCode::TYPE_01; |
||
144 | $this->options->errorCorrectLevel = QRCode::ERROR_CORRECT_LEVEL_H; |
||
145 | |||
146 | $method = $this->reflectionClass->getMethod('getRawData'); |
||
147 | $method->invoke($this->reflectionClass->newInstanceArgs(['ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', $this->output, $this->options])); |
||
148 | } |
||
149 | |||
150 | } |
||
151 |