forked from endroid/qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQrCodeTest.php
executable file
·128 lines (107 loc) · 3.75 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
<?php
/*
* (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 Endroid\QrCode\Writer\BinaryWriter;
use Endroid\QrCode\Writer\DebugWriter;
use Endroid\QrCode\Writer\EpsWriter;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use PHPUnit\Framework\TestCase;
class QrCodeTest extends TestCase
{
public function testReadable()
{
$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',
'有限公司'
];
foreach ($messages as $message) {
$qrCode = new QrCode($message);
$qrCode->setSize(300);
$qrCode->setValidateResult(true);
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
}
}
public function testFactory()
{
$qrCodeFactory = new QrCodeFactory();
$qrCode = $qrCodeFactory->create('QR Code', [
'writer' => 'png',
'size' => 300,
'margin' => 10
]);
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
}
public function testWriteQrCode()
{
$qrCode = new QrCode('QrCode');
$qrCode->setWriterByName('binary');
$binData = $qrCode->writeString();
$this->assertTrue(is_string($binData));
$qrCode->setWriterByName('debug');
$debugData = $qrCode->writeString();
$this->assertTrue(is_string($debugData));
$qrCode->setWriterByName('eps');
$epsData = $qrCode->writeString();
$this->assertTrue(is_string($epsData));
$qrCode->setWriterByName('png');
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
$pngDataUriData = $qrCode->writeDataUri();
$this->assertTrue(strpos($pngDataUriData, 'data:image/png;base64') === 0);
$qrCode->setWriterByName('svg');
$svgData = $qrCode->writeString();
$this->assertTrue(is_string($svgData));
$svgDataUriData = $qrCode->writeDataUri();
$this->assertTrue(strpos($svgDataUriData, 'data:image/svg+xml;base64') === 0);
}
public function testSetSize()
{
$size = 400;
$margin = 10;
$qrCode = new QrCode('QrCode');
$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()
{
$qrCode = new QrCode('QrCode');
$qrCode
->setSize(300)
->setLabel('Scan the code', 15)
;
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
}
public function testSetLogo()
{
$qrCode = new QrCode('QrCode');
$qrCode
->setSize(400)
->setLogoPath(__DIR__.'/../assets/symfony.png')
->setLogoWidth(150)
->setValidateResult(true);
;
$pngData = $qrCode->writeString();
$this->assertTrue(is_string($pngData));
}
}