ExceptionFormatterTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Exception;
  13. use Spipu\Html2Pdf\Exception\ExceptionFormatter;
  14. use Spipu\Html2Pdf\Exception\Html2PdfException;
  15. use Spipu\Html2Pdf\Exception\HtmlParsingException;
  16. use Spipu\Html2Pdf\Exception\ImageException;
  17. use Spipu\Html2Pdf\Exception\LongSentenceException;
  18. use Spipu\Html2Pdf\Tests\CrossVersionCompatibility\ExceptionFormatterTestCase;
  19. /**
  20. * Class ExceptionFormaterTest
  21. */
  22. class ExceptionFormatterTest extends ExceptionFormatterTestCase
  23. {
  24. /**
  25. * Test the formatter / generic exception
  26. */
  27. public function testGeneric()
  28. {
  29. $exception = new Html2PdfException('My Message');
  30. $formatter = new ExceptionFormatter($exception);
  31. $messages = [
  32. $formatter->getMessage(),
  33. $formatter->getHtmlMessage()
  34. ];
  35. foreach ($messages as $message) {
  36. $this->assertContains('Html2Pdf Error ['.Html2PdfException::ERROR_CODE.']', $message);
  37. $this->assertContains('My Message', $message);
  38. }
  39. }
  40. /**
  41. * Test the formatter / parsing exception
  42. */
  43. public function testParsing()
  44. {
  45. $exception = new HtmlParsingException('My Message');
  46. $exception->setInvalidTag('my_tag');
  47. $exception->setHtmlLine(42);
  48. $formatter = new ExceptionFormatter($exception);
  49. $messages = [
  50. $formatter->getMessage(),
  51. $formatter->getHtmlMessage()
  52. ];
  53. foreach ($messages as $message) {
  54. $this->assertContains('Html2Pdf Error ['.HtmlParsingException::ERROR_CODE.']', $message);
  55. $this->assertContains('My Message', $message);
  56. $this->assertContains('my_tag', $message);
  57. $this->assertContains('42', $message);
  58. }
  59. }
  60. /**
  61. * Test the formatter / image exception
  62. */
  63. public function testImage()
  64. {
  65. $exception = new ImageException('My Message');
  66. $exception->setImage('my_image.png');
  67. $formatter = new ExceptionFormatter($exception);
  68. $messages = [
  69. $formatter->getMessage(),
  70. $formatter->getHtmlMessage()
  71. ];
  72. foreach ($messages as $message) {
  73. $this->assertContains('Html2Pdf Error ['.ImageException::ERROR_CODE.']', $message);
  74. $this->assertContains('My Message', $message);
  75. $this->assertContains('my_image.png', $message);
  76. }
  77. }
  78. /**
  79. * Test the formatter / long sentence exception
  80. */
  81. public function testLongSentence()
  82. {
  83. $exception = new LongSentenceException('My Message');
  84. $exception->setSentence('my sentence');
  85. $exception->setLength(142);
  86. $exception->setWidthBox(242);
  87. $formatter = new ExceptionFormatter($exception);
  88. $messages = [
  89. $formatter->getMessage(),
  90. $formatter->getHtmlMessage()
  91. ];
  92. foreach ($messages as $message) {
  93. $this->assertContains('Html2Pdf Error ['.LongSentenceException::ERROR_CODE.']', $message);
  94. $this->assertContains('My Message', $message);
  95. $this->assertContains('my sentence', $message);
  96. $this->assertContains('142', $message);
  97. $this->assertContains('242', $message);
  98. }
  99. }
  100. }