* @copyright 2015-2024 Nicola Asuni - Tecnick.com LTD * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) * @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-2024 Nicola Asuni - Tecnick.com LTD * @license http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT) * @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(); } public function testGetBarcodeObjException(): void { $this->bcExpectException('\\' . \Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj( 'ERROR', '01001100011100001111,10110011100011110000', -2, -2, 'purple' ); } public function testSetPaddingException(): void { $this->bcExpectException('\\' . \Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj( 'LRAW,AB,12,E3F', '01001100011100001111,10110011100011110000', -2, -2, 'purple', [10] //@phpstan-ignore-line ); } public function testEmptyColumns(): void { $this->bcExpectException('\\' . \Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('LRAW', ''); } public function testEmptyInput(): void { $this->bcExpectException('\\' . \Com\Tecnick\Barcode\Exception::class); $barcode = $this->getTestObject(); $barcode->getBarcodeObj('LRAW', ''); } 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']); } 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()); } 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] ); } 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)); } 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 = xdebug_get_headers(); $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 = xdebug_get_headers(); $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 = xdebug_get_headers(); $this->assertEquals( 'Content-Disposition: inline; filename="test_SVG_filename-001.svg";', $headers[5] ); } 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 = xdebug_get_headers(); $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 = xdebug_get_headers(); $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 = xdebug_get_headers(); $this->assertEquals( 'Content-Disposition: inline; filename="test_PNG_filename-001.png";', $headers[5] ); } }