forked from endroid/qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QrCodeTest.php
executable file
·187 lines (156 loc) · 5.52 KB
/
QrCodeTest.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
declare(strict_types=1);
/*
* (c) Jeroen van den Enden <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Endroid\QrCode\Tests;
use Endroid\QrCode\Factory\QrCodeFactory;
use Endroid\QrCode\QrCode;
use PHPUnit\Framework\TestCase;
use Zxing\QrReader;
class QrCodeTest extends TestCase
{
public function testReadable(): void
{
$messages = [
'Tiny',
'This one has spaces',
'd2llMS9uU01BVmlvalM2YU9BUFBPTTdQMmJabHpqdndt',
'http://this.is.an/url?with=query&string=attached',
'11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111',
'{"i":"serialized.data","v":1,"t":1,"d":"4AEPc9XuIQ0OjsZoSRWp9DRWlN6UyDvuMlyOYy8XjOw="}',
'Spëci&al ch@ract3rs',
'有限公司',
];
$qrCode = new QrCode();
$qrCode->setSize(300);
foreach ($messages as $message) {
$qrCode->setText($message);
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertEquals($message, $reader->text());
}
}
public function testFactory(): void
{
$qrCodeFactory = new QrCodeFactory();
$qrCode = $qrCodeFactory->create('QR Code', [
'writer' => 'png',
'size' => 300,
'margin' => 10,
]);
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertEquals('QR Code', $reader->text());
}
/**
* @dataProvider writerNamesProvider
*/
public function testWriteQrCodeByWriterName($writerName, $fileContent): void
{
$qrCode = new QrCode('QR Code');
$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
$qrCode->setLogoWidth(100);
$qrCode->setWriterByName($writerName);
$data = $qrCode->writeString();
$this->assertTrue(is_string($data));
if (null !== $fileContent) {
$uriData = $qrCode->writeDataUri();
$this->assertTrue(0 === strpos($uriData, $fileContent));
}
}
public function writerNamesProvider()
{
return [
['binary', null],
['debug', null],
['eps', null],
['png', 'data:image/png;base64'],
['svg', 'data:image/svg+xml;base64'],
];
}
/**
* @dataProvider extensionsProvider
*/
public function testWriteQrCodeByWriterExtension($extension, $fileContent): void
{
$qrCode = new QrCode('QR Code');
$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
$qrCode->setLogoWidth(100);
$qrCode->setWriterByExtension($extension);
$data = $qrCode->writeString();
$this->assertTrue(is_string($data));
if (null !== $fileContent) {
$uriData = $qrCode->writeDataUri();
$this->assertTrue(0 === strpos($uriData, $fileContent));
}
}
public function extensionsProvider()
{
return [
['bin', null],
['txt', null],
['eps', null],
['png', 'data:image/png;base64'],
['svg', 'data:image/svg+xml;base64'],
];
}
public function testSetSize(): void
{
$size = 400;
$margin = 10;
$qrCode = new QrCode('QR Code');
$qrCode->setSize($size);
$qrCode->setMargin($margin);
$pngData = $qrCode->writeString();
$image = imagecreatefromstring($pngData);
$this->assertTrue(imagesx($image) === $size + 2 * $margin);
$this->assertTrue(imagesy($image) === $size + 2 * $margin);
}
public function testSetLabel(): void
{
$qrCode = new QrCode('QR Code');
$qrCode->setSize(300);
$qrCode->setLabel('Scan the code', 15);
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
$reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB);
$this->assertEquals('QR Code', $reader->text());
}
public function testSetLogo(): void
{
$qrCode = new QrCode('QR Code');
$qrCode->setSize(500);
$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
$qrCode->setLogoWidth(100);
$qrCode->setValidateResult(true);
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
}
public function testWriteFile(): void
{
$filename = __DIR__.'/output/qr-code.png';
$qrCode = new QrCode('QR Code');
$qrCode->writeFile($filename);
$image = imagecreatefromstring(file_get_contents($filename));
$this->assertTrue(is_resource($image));
}
public function testData(): void
{
$qrCode = new QrCode('QR Code');
$data = $qrCode->getData();
$this->assertArrayHasKey('block_count', $data);
$this->assertArrayHasKey('block_size', $data);
$this->assertArrayHasKey('inner_width', $data);
$this->assertArrayHasKey('inner_height', $data);
$this->assertArrayHasKey('outer_width', $data);
$this->assertArrayHasKey('outer_height', $data);
$this->assertArrayHasKey('margin_left', $data);
$this->assertArrayHasKey('margin_right', $data);
}
}