html2pdf mit composer hinzugefügt

This commit is contained in:
aschwarz
2023-02-16 09:19:46 +01:00
parent 3dcc93a65d
commit d1ab7f3763
613 changed files with 134681 additions and 0 deletions

View File

@ -0,0 +1,73 @@
<?php
/**
* Html2Pdf Library - Tests
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Tests\Parsing;
use PHPUnit_Framework_TestCase;
use Spipu\Html2Pdf\Parsing\HtmlLexer;
/**
* Class HtmlLexerTest
*/
class HtmlLexerTest extends PHPUnit_Framework_TestCase
{
/**
* Test: tokenize
*
* @param string $html html to test
* @param array $expectedTokens expected token
*
* @dataProvider tokenizeProvider
*/
public function testTokenize($html, $expectedTokens)
{
$lexer = new HtmlLexer();
$tokens = $lexer->tokenize($html);
$this->assertEquals(count($expectedTokens), count($tokens));
for ($i = 0; $i < count($tokens); $i++) {
$this->assertEquals($expectedTokens[$i][0], $tokens[$i]->getType());
$this->assertEquals($expectedTokens[$i][1], $tokens[$i]->getData());
$this->assertEquals($expectedTokens[$i][2], $tokens[$i]->getLine());
}
}
/**
* provider: tokenize
*
* @return array
*/
public function tokenizeProvider()
{
return array(
array(
'<p>test</p>',
array(
array('code', '<p>', 1),
array('txt', 'test', -1),
array('code', '</p>', 1),
)
),
array(
"<a><!-- comment -->\n<b><c>test",
array(
array('code', '<a>', 1),
array('txt', "\n", -1),
array('code', '<b>', 2),
array('code', '<c>', 2),
array('txt', "test", -1),
)
)
);
}
}

View File

@ -0,0 +1,61 @@
<?php
/**
* Html2Pdf Library - Tests
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Tests\Parsing;
use Spipu\Html2Pdf\Tests\CrossVersionCompatibility\HtmlTestCase;
/**
* Class HtmlTest
*/
class HtmlTest extends HtmlTestCase
{
/**
* mock of prepareTxt method
*
* @param $txt
* @param bool $spaces
* @return mixed
*/
public function mockPrepareTxt($txt, $spaces = true)
{
return $txt;
}
/**
* Test the prepareHtml method
*/
public function testPrepareHtml()
{
$result = $this->object->prepareHtml('Hello [[date_y]]-[[date_m]]-[[date_d]] World');
$this->assertSame('Hello '.date('Y-m-d').' World', $result);
$result = $this->object->prepareHtml('Hello [[date_h]]:[[date_i]]:[[date_s]] World');
$this->assertSame('Hello '.date('H:i:s').' World', $result);
$html = '
<html>
<head>
<style type="text">.my-class { color: red; }</style>
<link type="text/css" href="my-style.css"/>
</head>
<body class="my-class"><p>Hello World</p></body>
</html>';
$expected='<style type="text">.my-class { color: red; }</style>'.
'<link type="text/css" href="my-style.css"/>'.
'<page class="my-class"><p>Hello World</p></page>';
$result = $this->object->prepareHtml($html);
$this->assertSame($expected, $result);
}
}

View File

@ -0,0 +1,100 @@
<?php
/**
* Html2Pdf Library - Tests
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Tests\Parsing;
use Spipu\Html2Pdf\Exception\HtmlParsingException;
use Spipu\Html2Pdf\Tests\AbstractTest;
/**
* Class ParsingTest
*/
class ParsingTest extends AbstractTest
{
/**
* test: The tag is unknown
*
* @return void
*/
public function testUnknownTag()
{
$this->expectException(HtmlParsingException::class);
$object = $this->getObject();
$object->writeHTML('<bad_tag>Hello World</bad_tag>');
$object->output('test.pdf', 'S');
}
/**
* test: Too many tag closures found
*
* @return void
*/
public function testTooManyClosuresFound()
{
$this->expectException(HtmlParsingException::class);
$object = $this->getObject();
$object->writeHTML('<i><u>Hello</u></i></b>');
$object->output('test.pdf', 'S');
}
/**
* test: Tags are closed in a wrong order
*
* @return void
*/
public function testWrongClosedOrder()
{
$this->expectException(HtmlParsingException::class);
$object = $this->getObject();
$object->writeHTML('<b><u><i>Hello</u></i></b>');
$object->output('test.pdf', 'S');
}
/**
* test: The following tag has not been closed
*
* @return void
*/
public function testNotClosed()
{
$this->expectException(HtmlParsingException::class);
$object = $this->getObject();
$object->writeHTML('<b><i>Hello</i>');
$object->output('test.pdf', 'S');
}
/**
* test: The following tags have not been closed
*
* @return void
*/
public function testNotClosedMore()
{
$this->expectException(HtmlParsingException::class);
$object = $this->getObject();
$object->writeHTML('<b><u><i>Hello</i>');
$object->output('test.pdf', 'S');
}
/**
* test: The HTML tag code provided is invalid
*
* @return void
*/
public function testInvalidCode()
{
$this->expectException(HtmlParsingException::class);
$object = $this->getObject();
$object->writeHTML('<az1-r_h>Hello</az1-r_h>');
$object->output('test.pdf', 'S');
}
}

View File

@ -0,0 +1,203 @@
<?php
/**
* Html2Pdf Library - Tests
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Tests\Parsing;
use Spipu\Html2Pdf\Exception\HtmlParsingException;
use Spipu\Html2Pdf\Parsing\Node;
use Spipu\Html2Pdf\Tests\CrossVersionCompatibility\TagParserTestCase;
/**
* Class TagParserTest
*/
class TagParserTest extends TagParserTestCase
{
/**
* mock of prepareTxt method
*
* @param $txt
* @param bool $spaces
* @return mixed
*/
public function mockPrepareTxt($txt, $spaces = true)
{
return $txt;
}
/**
* @param string $code
* @param array $expected
*
* @dataProvider tagAttributesProvider
*/
public function testExtractTagAttributes($code, $expected)
{
$result = $this->parser->extractTagAttributes($code);
$this->assertEquals($expected, $result);
}
/**
* @return array
*/
public function tagAttributesProvider()
{
return array(
array('attr=value', array('attr' => 'value')),
array('attr="value"', array('attr' => 'value')),
array('attr=\'value\'', array('attr' => 'value')),
array('attr="value with spaces"', array('attr' => 'value with spaces')),
array('attr="value with \'quotes"', array('attr' => 'value with \'quotes')),
array('my attr="value"', array('attr' => 'value')),
array('attr1=val1 attr2="value2"', array('attr1' => 'val1', 'attr2' => 'value2')),
);
}
/**
* Test if a bad tag is detected
*/
public function testAnalyzeTagBadTag()
{
$this->expectException(HtmlParsingException::class);
$this->parser->analyzeTag('test');
}
/**
* Test basic open, close, autoclose tags
*/
public function testBasicTags()
{
$result = $this->parser->analyzeTag('<my_tag/>');
$this->assertTrue($result instanceof Node);
$this->assertSame('my_tag', $result->getName());
$this->assertSame(true, $result->isAutoClose());
$this->assertSame(false, $result->isClose());
$result->setLine(10);
$this->assertSame(10, $result->getLine());
$result->setParam('attr', 'value');
$this->assertSame('value', $result->getParam('attr'));
$result = $this->parser->analyzeTag('<my_tag>');
$this->assertSame('my_tag', $result->getName());
$this->assertSame(false, $result->isAutoClose());
$this->assertSame(false, $result->isClose());
$this->assertSame(['style' => [], 'num' => 0], $result->getParams());
$this->assertSame('default', $result->getParam('attr', 'default'));
$result = $this->parser->analyzeTag('</my_tag>');
$this->assertSame('my_tag', $result->getName());
$this->assertSame(false, $result->isAutoClose());
$this->assertSame(true, $result->isClose());
}
/**
* Test styles
*/
public function testAnalyzeAttributes()
{
$result = $this->parser->analyzeTag('<img src="my_src" alt="my alt"/>');
$this->assertSame('my_src', $result->getParam('src'));
$this->assertSame('my alt', $result->getParam('alt'));
$result = $this->parser->analyzeTag('<a href="my_src" title="my title"/>');
$this->assertSame('my_src', $result->getParam('href'));
$this->assertSame('my title', $result->getParam('title'));
$result = $this->parser->analyzeTag('<input type="text" value="my value" class="my_class" />');
$this->assertSame('text', $result->getParam('type'));
$this->assertSame('my value', $result->getParam('value'));
$this->assertSame('my_class', $result->getParam('class'));
$result = $this->parser->analyzeTag('<my_tag width="10" height="20" align="center" valign="bottom" bgcolor="red">');
$this->assertSame('10px', $result->getStyle('width'));
$this->assertSame('20px', $result->getStyle('height'));
$this->assertSame('center', $result->getStyle('text-align'));
$this->assertSame('bottom', $result->getStyle('vertical-align'));
$this->assertSame('red', $result->getStyle('background'));
$result = $this->parser->analyzeTag('<img align="right">');
$this->assertSame('right', $result->getStyle('float'));
$result = $this->parser->analyzeTag('<table cellpadding="1" cellspacing="2">');
$this->assertSame('1px', $result->getParam('cellpadding'));
$this->assertSame('2px', $result->getParam('cellspacing'));
$result = $this->parser->analyzeTag('<td rowspan="0" colspan="2px">');
$this->assertSame(1, $result->getParam('rowspan'));
$this->assertSame(2, $result->getParam('colspan'));
$result = $this->parser->analyzeTag('<my_tag color="red">');
$this->assertSame('red', $result->getParam('color'));
$this->assertSame(null, $result->getStyle('color'));
$result = $this->parser->analyzeTag('<font color="red">');
$this->assertSame(null, $result->getParam('color'));
$this->assertSame('red', $result->getStyle('color'));
}
/**
* Test borders
*/
public function testBorders()
{
$result = $this->parser->analyzeTag('<div border="1" bordercolor="red" />');
$this->assertSame('div', $result->getName());
$this->assertSame('solid 1px red', $result->getParam('border'));
$this->assertSame('solid 1px red', $result->getStyle('border'));
$result = $this->parser->analyzeTag('<div border="0" bordercolor="red" />');
$this->assertSame('div', $result->getName());
$this->assertSame('none', $result->getParam('border'));
$this->assertSame('none', $result->getStyle('border'));
}
/**
* Test levels
*/
public function testLevels()
{
$result = $this->parser->analyzeTag('<basic_tag>');
$this->assertSame(0, $result->getParam('num'));
$result = $this->parser->analyzeTag('<table>');
$this->assertSame(1, $result->getParam('num'));
$result = $this->parser->analyzeTag('<ol>');
$this->assertSame(2, $result->getParam('num'));
$result = $this->parser->analyzeTag('<ul>');
$this->assertSame(3, $result->getParam('num'));
$result = $this->parser->analyzeTag('</ul>');
$this->assertSame('ul', $result->getName());
$this->assertSame(3, $result->getParam('num'));
$result = $this->parser->analyzeTag('</ol>');
$this->assertSame(2, $result->getParam('num'));
$result = $this->parser->analyzeTag('</table>');
$this->assertSame(1, $result->getParam('num'));
$result = $this->parser->analyzeTag('<basic_tag>');
$this->assertSame(0, $result->getParam('num'));
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Html2Pdf Library - Tests
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Tests\Parsing;
use Spipu\Html2Pdf\Tests\CrossVersionCompatibility\TextParserTestCase;
/**
* Class TextParserTest
*/
class TextParserTest extends TextParserTestCase
{
/**
* Test if it works
*/
public function testOk()
{
$result = $this->parser->prepareTxt('hello world', false);
$this->assertSame('hello world', $result);
$result = $this->parser->prepareTxt('hello world', true);
$this->assertSame('hello world', $result);
$result = $this->parser->prepareTxt('hello 10&euro; world');
$this->assertSame('hello 10€ world', $result);
$result = $this->parser->prepareTxt('hello &lt; world');
$this->assertSame('hello < world', $result);
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Html2Pdf Library - Tests
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Tests\Parsing;
use PHPUnit_Framework_TestCase;
use Spipu\Html2Pdf\Parsing\Token;
/**
* Class TokenTest
*/
class TokenTest extends PHPUnit_Framework_TestCase
{
/**
* Test if it works
*/
public function testOk()
{
$token = new Token('hello', 'world', 45);
$this->assertSame('hello', $token->getType());
$this->assertSame('world', $token->getData());
$this->assertSame(45, $token->getLine());
}
}