2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class QRCodeTest
|
|
|
|
*
|
|
|
|
* @filesource QRCodeTest.php
|
|
|
|
* @created 17.11.2017
|
|
|
|
* @package chillerlan\QRCodeTest
|
|
|
|
* @author Smiley <smiley@chillerlan.net>
|
|
|
|
* @copyright 2017 Smiley
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace chillerlan\QRCodeTest;
|
|
|
|
|
|
|
|
use chillerlan\QRCode\{QROptions, QRCode};
|
2022-07-02 14:01:51 +00:00
|
|
|
use chillerlan\QRCode\Data\{AlphaNum, Byte, Kanji, Number, QRCodeDataException};
|
2021-02-26 16:16:17 +00:00
|
|
|
use chillerlan\QRCode\Output\QRCodeOutputException;
|
2022-07-02 14:01:51 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
use function random_bytes;
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* Tests basic functions of the QRCode class
|
|
|
|
*/
|
|
|
|
class QRCodeTest extends TestCase{
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/** @internal */
|
|
|
|
protected QRCode $qrcode;
|
|
|
|
/** @internal */
|
|
|
|
protected QROptions $options;
|
2021-02-26 16:16:17 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-02 14:01:51 +00:00
|
|
|
* invoke test instances
|
|
|
|
*
|
|
|
|
* @internal
|
2021-02-26 16:16:17 +00:00
|
|
|
*/
|
|
|
|
protected function setUp():void{
|
2022-07-02 14:01:51 +00:00
|
|
|
$this->qrcode = new QRCode;
|
|
|
|
$this->options = new QROptions;
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* isNumber() should pass on any number and fail on anything else
|
|
|
|
*/
|
|
|
|
public function testIsNumber():void{
|
|
|
|
$this::assertTrue($this->qrcode->isNumber('0123456789'));
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertFalse($this->qrcode->isNumber('ABC123'));
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* isAlphaNum() should pass on the 45 defined characters and fail on anything else (e.g. lowercase)
|
|
|
|
*/
|
|
|
|
public function testIsAlphaNum():void{
|
|
|
|
$this::assertTrue($this->qrcode->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:'));
|
|
|
|
|
|
|
|
$this::assertFalse($this->qrcode->isAlphaNum('abc'));
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* isKanji() should pass on Kanji/SJIS characters and fail on everything else
|
|
|
|
*/
|
|
|
|
public function testIsKanji():void{
|
|
|
|
$this::assertTrue($this->qrcode->isKanji('茗荷'));
|
|
|
|
|
|
|
|
$this::assertFalse($this->qrcode->isKanji('Ã'));
|
|
|
|
$this::assertFalse($this->qrcode->isKanji('ABC'));
|
|
|
|
$this::assertFalse($this->qrcode->isKanji('123'));
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-02 14:01:51 +00:00
|
|
|
* isByte() passses any binary string and only fails on empty strings
|
2021-02-26 16:16:17 +00:00
|
|
|
*/
|
2022-07-02 14:01:51 +00:00
|
|
|
public function testIsByte():void{
|
|
|
|
$this::assertTrue($this->qrcode->isByte("\x01\x02\x03"));
|
|
|
|
$this::assertTrue($this->qrcode->isByte(' ')); // not empty!
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertFalse($this->qrcode->isByte(''));
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* tests if an exception is thrown when an invalid (built-in) output type is specified
|
|
|
|
*/
|
|
|
|
public function testInitDataInterfaceException():void{
|
2021-02-26 16:16:17 +00:00
|
|
|
$this->expectException(QRCodeOutputException::class);
|
|
|
|
$this->expectExceptionMessage('invalid output type');
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this->options->outputType = 'foo';
|
|
|
|
|
|
|
|
(new QRCode($this->options))->render('test');
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* tests if an exception is thrown when trying to call getMatrix() without data (empty string, no data set)
|
|
|
|
*/
|
|
|
|
public function testGetMatrixException():void{
|
2021-02-26 16:16:17 +00:00
|
|
|
$this->expectException(QRCodeDataException::class);
|
|
|
|
$this->expectExceptionMessage('QRCode::getMatrix() No data given.');
|
|
|
|
|
|
|
|
$this->qrcode->getMatrix('');
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* test whether stings are trimmed (they are not) - i'm still torn on that (see isByte)
|
|
|
|
*/
|
|
|
|
public function testAvoidTrimming():void{
|
|
|
|
$m1 = $this->qrcode->getMatrix('hello')->matrix();
|
|
|
|
$m2 = $this->qrcode->getMatrix('hello ')->matrix(); // added space
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertNotSame($m1, $m2);
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* tests if the data mode is overriden if QROptions::$dataModeOverride is set to a valid value
|
|
|
|
*
|
|
|
|
* @see https://github.com/chillerlan/php-qrcode/issues/39
|
|
|
|
*/
|
|
|
|
public function testDataModeOverride():void{
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
// no (or invalid) value set - auto detection
|
|
|
|
$this->options->dataModeOverride = 'foo';
|
|
|
|
$this->qrcode = new QRCode;
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertInstanceOf(Number::class, $this->qrcode->initDataInterface('123'));
|
|
|
|
$this::assertInstanceOf(AlphaNum::class, $this->qrcode->initDataInterface('ABC123'));
|
|
|
|
$this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface(random_bytes(32)));
|
|
|
|
$this::assertInstanceOf(Kanji::class, $this->qrcode->initDataInterface('茗荷'));
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
// data mode set: force the given data mode
|
|
|
|
$this->options->dataModeOverride = 'Byte';
|
|
|
|
$this->qrcode = new QRCode($this->options);
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('123'));
|
|
|
|
$this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('ABC123'));
|
|
|
|
$this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface(random_bytes(32)));
|
|
|
|
$this::assertInstanceOf(Byte::class, $this->qrcode->initDataInterface('茗荷'));
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
/**
|
|
|
|
* tests if an exception is thrown when an invalid character occurs when forcing a data mode other than Byte
|
|
|
|
*/
|
|
|
|
public function testDataModeOverrideError():void{
|
2021-02-26 16:16:17 +00:00
|
|
|
$this->expectException(QRCodeDataException::class);
|
|
|
|
$this->expectExceptionMessage('illegal char:');
|
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
$this->options->dataModeOverride = 'AlphaNum';
|
2021-02-26 16:16:17 +00:00
|
|
|
|
2022-07-02 14:01:51 +00:00
|
|
|
(new QRCode($this->options))->initDataInterface(random_bytes(32));
|
2021-02-26 16:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|