diff options
Diffstat (limited to 'vendor/chillerlan/php-qrcode/tests/QRCodeTest.php')
-rwxr-xr-x[-rw-r--r--] | vendor/chillerlan/php-qrcode/tests/QRCodeTest.php | 161 |
1 files changed, 86 insertions, 75 deletions
diff --git a/vendor/chillerlan/php-qrcode/tests/QRCodeTest.php b/vendor/chillerlan/php-qrcode/tests/QRCodeTest.php index 8613c961f..523d7eb8c 100644..100755 --- a/vendor/chillerlan/php-qrcode/tests/QRCodeTest.php +++ b/vendor/chillerlan/php-qrcode/tests/QRCodeTest.php @@ -13,128 +13,139 @@ namespace chillerlan\QRCodeTest; use chillerlan\QRCode\{QROptions, QRCode}; -use chillerlan\QRCode\Data\{AlphaNum, Byte, Number, QRCodeDataException}; +use chillerlan\QRCode\Data\{AlphaNum, Byte, Kanji, Number, QRCodeDataException}; use chillerlan\QRCode\Output\QRCodeOutputException; -use chillerlan\QRCodeExamples\MyCustomOutput; +use PHPUnit\Framework\TestCase; use function random_bytes; -class QRCodeTest extends QRTestAbstract{ +/** + * Tests basic functions of the QRCode class + */ +class QRCodeTest extends TestCase{ - protected $FQCN = QRCode::class; + /** @internal */ + protected QRCode $qrcode; + /** @internal */ + protected QROptions $options; /** - * @var \chillerlan\QRCode\QRCode + * invoke test instances + * + * @internal */ - protected $qrcode; - protected function setUp():void{ - parent::setUp(); - - $this->qrcode = $this->reflection->newInstance(); + $this->qrcode = new QRCode; + $this->options = new QROptions; } - public function testIsNumber(){ - $this->assertTrue($this->qrcode->isNumber('0123456789')); - $this->assertFalse($this->qrcode->isNumber('ABC')); - } + /** + * isNumber() should pass on any number and fail on anything else + */ + public function testIsNumber():void{ + $this::assertTrue($this->qrcode->isNumber('0123456789')); - public function testIsAlphaNum(){ - $this->assertTrue($this->qrcode->isAlphaNum('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:')); - $this->assertFalse($this->qrcode->isAlphaNum('abc')); + $this::assertFalse($this->qrcode->isNumber('ABC123')); } - public function testIsKanji(){ - $this->assertTrue($this->qrcode->isKanji('茗荷')); - $this->assertFalse($this->qrcode->isKanji('Ã')); + /** + * 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')); } - // coverage - - public function typeDataProvider(){ - return [ - 'png' => [QRCode::OUTPUT_IMAGE_PNG, 'data:image/png;base64,'], - 'gif' => [QRCode::OUTPUT_IMAGE_GIF, 'data:image/gif;base64,'], - 'jpg' => [QRCode::OUTPUT_IMAGE_JPG, 'data:image/jpg;base64,'], - 'svg' => [QRCode::OUTPUT_MARKUP_SVG, 'data:image/svg+xml;base64,'], - 'html' => [QRCode::OUTPUT_MARKUP_HTML, '<div><span style="background:'], - 'text' => [QRCode::OUTPUT_STRING_TEXT, '⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕⭕'.PHP_EOL], - 'json' => [QRCode::OUTPUT_STRING_JSON, '[[18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18],'], - ]; + /** + * 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')); } /** - * @dataProvider typeDataProvider - * @param $type + * isByte() passses any binary string and only fails on empty strings */ - public function testRenderImage($type, $expected){ - $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['outputType' => $type])]); + public function testIsByte():void{ + $this::assertTrue($this->qrcode->isByte("\x01\x02\x03")); + $this::assertTrue($this->qrcode->isByte(' ')); // not empty! - $this->assertStringContainsString($expected, $this->qrcode->render('test')); + $this::assertFalse($this->qrcode->isByte('')); } - public function testInitDataInterfaceException(){ + /** + * tests if an exception is thrown when an invalid (built-in) output type is specified + */ + public function testInitDataInterfaceException():void{ $this->expectException(QRCodeOutputException::class); $this->expectExceptionMessage('invalid output type'); - (new QRCode(new QROptions(['outputType' => 'foo'])))->render('test'); + $this->options->outputType = 'foo'; + + (new QRCode($this->options))->render('test'); } - public function testGetMatrixException(){ + /** + * tests if an exception is thrown when trying to call getMatrix() without data (empty string, no data set) + */ + public function testGetMatrixException():void{ $this->expectException(QRCodeDataException::class); $this->expectExceptionMessage('QRCode::getMatrix() No data given.'); $this->qrcode->getMatrix(''); } - public function testTrim() { - $m1 = $this->qrcode->getMatrix('hello'); - $m2 = $this->qrcode->getMatrix('hello '); // added space - - $this->assertNotEquals($m1, $m2); - } - - public function testImageTransparencyBGDefault(){ - $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['imageTransparencyBG' => 'foo'])]); + /** + * 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 - $this->assertSame([255,255,255], $this->getProperty('options')->getValue($this->qrcode)->imageTransparencyBG); + $this::assertNotSame($m1, $m2); } - public function testCustomOutput(){ - - $options = new QROptions([ - 'version' => 5, - 'eccLevel' => QRCode::ECC_L, - 'outputType' => QRCode::OUTPUT_CUSTOM, - 'outputInterface' => MyCustomOutput::class, - ]); - - $expected = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110111010000101111010000011111110000000010000010111000001101011000001010000010000000010111010101101011000001101011010111010000000010111010110100111110010100111010111010000000010111010000001101011000001101010111010000000010000010100111110010100111110010000010000000011111110101010101010101010101011111110000000000000000010010100111110010100000000000000000011001110000101111010000101111001011110000000000000000111010000101111010000111100010000000001011010100111110010100111110011001010000000010000101111101011000001101011110011110000000000011010100011000001101011000101110100000000011001100001001101011000001101010011010000000010110111110000001101011000001100110100000000010000100100010100111110010100001100100000000011111110111101111010000101111010100110000000011010000111010000101111010000111100100000000010101111111111110010100111110011001000000000010110001110101011000001101011110011010000000001001111100011000001101011000101110010000000011000100110001101011000001101010011100000000001000011001000001101011000001100110000000000011101001011010100111110010100001100000000000010111010001101111010000101111010100110000000011100000001010000101111010000111100000000000000001110110111110010100111110011001000000000000011001011101011000001101011110011100000000011111110101011000001101011001111110110000000000000000110001101011000001101000111100000000011111110001000001101011000011010110000000000010000010101010100111110010101000100100000000010111010111101111010000101111111100110000000010111010011010000101111010001101100010000000010111010000111110010100111100101101100000000010000010101101011000001101001100111100000000011111110101011000001101011000110010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; - - $this->assertSame($expected, $this->reflection->newInstanceArgs([$options])->render('test')); - } + /** + * 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{ - public function testDataModeOverride(){ - $this->qrcode = $this->reflection->newInstance(); + // no (or invalid) value set - auto detection + $this->options->dataModeOverride = 'foo'; + $this->qrcode = new QRCode; - $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(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('茗荷')); - $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['dataMode' => 'Byte'])]); + // data mode set: force the given data mode + $this->options->dataModeOverride = 'Byte'; + $this->qrcode = new QRCode($this->options); - $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('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('茗荷')); } - public function testDataModeOverrideError(){ + /** + * tests if an exception is thrown when an invalid character occurs when forcing a data mode other than Byte + */ + public function testDataModeOverrideError():void{ $this->expectException(QRCodeDataException::class); $this->expectExceptionMessage('illegal char:'); - $this->qrcode = $this->reflection->newInstanceArgs([new QROptions(['dataMode' => 'AlphaNum'])]); + $this->options->dataModeOverride = 'AlphaNum'; - $this->qrcode->initDataInterface(random_bytes(32)); + (new QRCode($this->options))->initDataInterface(random_bytes(32)); } } |