* @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD * @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE) * @link https://github.com/tecnickcom/tc-lib-barcode * * This file is part of tc-lib-barcode software library. */ namespace Test; /** * Barcode class test * * @since 2015-02-21 * @category Library * @package Barcode * @author Nicola Asuni * @copyright 2015-2026 Nicola Asuni - Tecnick.com LTD * @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE) * @link https://github.com/tecnickcom/tc-lib-barcode */ class BarcodeTest extends TestUtil { protected function getTestObject(): \Com\Tecnick\Barcode\Barcode { return new \Com\Tecnick\Barcode\Barcode(); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testGetBarcodeObjException(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('ERROR', '01001100011100001111,10110011100011110000', -2, -2, 'purple'); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testSetPaddingException(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); /** @var array{0: int, 1: int, 2: int, 3: int} $invalidPadding */ $invalidPadding = [10]; $barcode->getBarcodeObj( 'LRAW,AB,12,E3F', '01001100011100001111,10110011100011110000', -2, -2, 'purple', $invalidPadding, ); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testEmptyColumns(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('LRAW', ''); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testEmptyInput(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('LRAW', ''); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testSpotColor(): void { $barcode = $this->getTestObject(); $type = $barcode->getBarcodeObj('LRAW', '01001100011100001111,10110011100011110000', -2, -2, 'all', [ -2, 3, 0, 1, ]); $bobjarr = $type->getArray(); $this->assertEquals('#000000ff', $bobjarr['color_obj']->getRgbaHexColor()); $this->assertNUll($bobjarr['bg_color_obj']); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testBackgroundColor(): void { $barcode = $this->getTestObject(); $type = $barcode->getBarcodeObj('LRAW', '01001100011100001111,10110011100011110000', -2, -2, 'all', [ -2, 3, 0, 1, ])->setBackgroundColor('mediumaquamarine'); $bobjarr = $type->getArray(); $this->assertNotNull($bobjarr['bg_color_obj']); $this->assertEquals('#66cdaaff', $bobjarr['bg_color_obj']->getRgbaHexColor()); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testNoColorException(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('LRAW', '01001100011100001111,10110011100011110000', -2, -2, '', [-2, 3, 0, 1]); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testExportMethods(): void { $barcode = $this->getTestObject(); $type = $barcode->getBarcodeObj( 'LRAW,AB,12,E3F', '01001100011100001111,10110011100011110000', -2, -2, 'purple', [-2, 3, 0, 1], ); $this->assertEquals('01001100011100001111,10110011100011110000', $type->getExtendedCode()); $barr = $type->getArray(); $this->assertEquals('linear', $barr['type']); $this->assertEquals('LRAW', $barr['format']); $this->assertEquals(['AB', '12', 'E3F'], $barr['params']); $this->assertEquals('01001100011100001111,10110011100011110000', $barr['code']); $this->assertEquals('01001100011100001111,10110011100011110000', $barr['extcode']); $this->assertEquals(20, $barr['ncols']); $this->assertEquals(2, $barr['nrows']); $this->assertEquals(40, $barr['width']); $this->assertEquals(4, $barr['height']); $this->assertEquals(2, $barr['width_ratio']); $this->assertEquals(2, $barr['height_ratio']); $this->assertEquals( [ 'T' => 4, 'R' => 3, 'B' => 0, 'L' => 1, ], $barr['padding'], ); $this->assertEquals(44, $barr['full_width']); $this->assertEquals(8, $barr['full_height']); $expected = [ [1, 0, 1, 1], [4, 0, 2, 1], [9, 0, 3, 1], [16, 0, 4, 1], [0, 1, 1, 1], [2, 1, 2, 1], [6, 1, 3, 1], [12, 1, 4, 1], ]; $this->assertEquals($expected, $barr['bars']); $this->assertEquals('#800080ff', $barr['color_obj']->getRgbaHexColor()); $grid = $type->getGrid('A', 'B'); $expected = "ABAABBAAABBBAAAABBBB\nBABBAABBBAAABBBBAAAA\n"; $this->assertEquals($expected, $grid); $svg = $type->setBackgroundColor('yellow')->getSvgCode(); $expected = ' 01001100011100001111,10110011100011110000 '; $this->assertEquals($expected, $svg); $hdiv = $type->setBackgroundColor('lightcoral')->getHtmlDiv(); $expected = '
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
'; $this->assertEquals($expected, $hdiv); if (\extension_loaded('imagick')) { $pngik = $type->setBackgroundColor('white')->getPngData(true); $this->assertEquals('PNG', \substr($pngik, 1, 3)); } $pnggd = $type->setBackgroundColor('white')->getPngData(false); $this->assertEquals('PNG', \substr($pnggd, 1, 3)); $pnggd = $type->setBackgroundColor('')->getPngData(false); $this->assertEquals('PNG', \substr($pnggd, 1, 3)); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testGetSvg(): void { $barcode = $this->getTestObject(); $type = $barcode->getBarcodeObj( 'LRAW,AB,12,E3F', '01001100011100001111,10110011100011110000', -2, -2, 'purple', ); // empty filename \ob_start(); $type->getSvg(); $svg = \ob_get_clean(); $this->assertNotFalse($svg); $this->assertEquals('114f33435c265345f7c6cdf673922292', \md5($svg)); $headers = $this->getResponseHeaders(); $this->assertEquals( 'Content-Disposition: inline; filename="114f33435c265345f7c6cdf673922292.svg";', $headers[5] ?? '', ); // invalid filename \ob_start(); $type->getSvg('#~'); $svg = \ob_get_clean(); $this->assertNotFalse($svg); $this->assertEquals('114f33435c265345f7c6cdf673922292', \md5($svg)); $headers = $this->getResponseHeaders(); $this->assertEquals( 'Content-Disposition: inline; filename="114f33435c265345f7c6cdf673922292.svg";', $headers[5] ?? '', ); // valid filename \ob_start(); $type->getSvg('test_SVG_filename-001'); $svg = \ob_get_clean(); $this->assertNotFalse($svg); $this->assertEquals('114f33435c265345f7c6cdf673922292', \md5($svg)); $headers = $this->getResponseHeaders(); $this->assertEquals('Content-Disposition: inline; filename="test_SVG_filename-001.svg";', $headers[5] ?? ''); } /** * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testGetPng(): void { $barcode = $this->getTestObject(); $type = $barcode->getBarcodeObj( 'LRAW,AB,12,E3F', '01001100011100001111,10110011100011110000', -2, -2, 'purple', ); // empty filename \ob_start(); $type->getPng(); $png = \ob_get_clean(); $this->assertNotFalse($png); $this->assertEquals('PNG', \substr($png, 1, 3)); $headers = $this->getResponseHeaders(); $this->assertNotEmpty($headers[5] ?? ''); // invalid filename \ob_start(); $type->getPng('#~'); $png = \ob_get_clean(); $this->assertNotFalse($png); $this->assertEquals('PNG', \substr($png, 1, 3)); $headers = $this->getResponseHeaders(); $this->assertNotEmpty($headers[5] ?? ''); // valid filename \ob_start(); $type->getPng('test_PNG_filename-001'); $png = \ob_get_clean(); $this->assertNotFalse($png); $this->assertEquals('PNG', \substr($png, 1, 3)); $headers = $this->getResponseHeaders(); $this->assertEquals('Content-Disposition: inline; filename="test_PNG_filename-001.png";', $headers[5] ?? ''); } /** * Regression: an over-long payload must be rejected early. * * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testPayloadTooLong(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('QRCODE', \str_pad('', 30001, 'A')); } /** * Regression: a pathological size multiplier must not trigger a huge image allocation. * * @throws \Com\Tecnick\Barcode\Exception * @throws \Com\Tecnick\Color\Exception */ public function testImageTooLarge(): void { $this->bcExpectException(\Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $type = $barcode->getBarcodeObj('QRCODE', 'test', -100000, -100000); $type->getGd(); } }