HtmlLexerTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Html2Pdf Library - Tests
  4. *
  5. * HTML => PDF converter
  6. * distributed under the OSL-3.0 License
  7. *
  8. * @package Html2pdf
  9. * @author Laurent MINGUET <webmaster@html2pdf.fr>
  10. * @copyright 2023 Laurent MINGUET
  11. */
  12. namespace Spipu\Html2Pdf\Tests\Parsing;
  13. use PHPUnit_Framework_TestCase;
  14. use Spipu\Html2Pdf\Parsing\HtmlLexer;
  15. /**
  16. * Class HtmlLexerTest
  17. */
  18. class HtmlLexerTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * Test: tokenize
  22. *
  23. * @param string $html html to test
  24. * @param array $expectedTokens expected token
  25. *
  26. * @dataProvider tokenizeProvider
  27. */
  28. public function testTokenize($html, $expectedTokens)
  29. {
  30. $lexer = new HtmlLexer();
  31. $tokens = $lexer->tokenize($html);
  32. $this->assertEquals(count($expectedTokens), count($tokens));
  33. for ($i = 0; $i < count($tokens); $i++) {
  34. $this->assertEquals($expectedTokens[$i][0], $tokens[$i]->getType());
  35. $this->assertEquals($expectedTokens[$i][1], $tokens[$i]->getData());
  36. $this->assertEquals($expectedTokens[$i][2], $tokens[$i]->getLine());
  37. }
  38. }
  39. /**
  40. * provider: tokenize
  41. *
  42. * @return array
  43. */
  44. public function tokenizeProvider()
  45. {
  46. return array(
  47. array(
  48. '<p>test</p>',
  49. array(
  50. array('code', '<p>', 1),
  51. array('txt', 'test', -1),
  52. array('code', '</p>', 1),
  53. )
  54. ),
  55. array(
  56. "<a><!-- comment -->\n<b><c>test",
  57. array(
  58. array('code', '<a>', 1),
  59. array('txt', "\n", -1),
  60. array('code', '<b>', 2),
  61. array('code', '<c>', 2),
  62. array('txt', "test", -1),
  63. )
  64. )
  65. );
  66. }
  67. }