PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/PHPPdf/Test/Core/Node/ParagraphTest.php

http://github.com/psliwa/PHPPdf
PHP | 317 lines | 221 code | 63 blank | 33 comment | 8 complexity | 537e02185f98780d99bc7f70e6dac9dc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace PHPPdf\Test\Core\Node;
  3. use PHPPdf\Core\Node\Page;
  4. use PHPPdf\Core\Node\Container;
  5. use PHPPdf\Core\Node\Node;
  6. use PHPPdf\Core\DrawingTask;
  7. use PHPPdf\Core\DrawingTaskHeap;
  8. use PHPPdf\Core\Node\Paragraph\LinePart;
  9. use PHPPdf\Core\Node\Paragraph\Line;
  10. use PHPPdf\Core\Point;
  11. use PHPPdf\Core\Document;
  12. use PHPPdf\Core\Node\Text;
  13. use PHPPdf\Core\Node\Paragraph;
  14. class ParagraphTest extends \PHPPdf\PHPUnit\Framework\TestCase
  15. {
  16. private $paragraph;
  17. public function setUp()
  18. {
  19. $this->paragraph = new Paragraph();
  20. }
  21. /**
  22. * @test
  23. * @dataProvider textProvider
  24. */
  25. public function trimTextElementsThatTextElementsAreSeparatedAtMostByOneSpace(array $texts)
  26. {
  27. foreach($texts as $text)
  28. {
  29. $this->paragraph->add(new Text($text));
  30. }
  31. $isPreviousTextEndsWithWhiteChars = false;
  32. foreach($this->paragraph->getChildren() as $textNode)
  33. {
  34. $isStartsWithWhiteChars = ltrim($textNode->getText()) != $textNode->getText();
  35. $this->assertFalse($isStartsWithWhiteChars && $isPreviousTextEndsWithWhiteChars);
  36. $isPreviousTextEndsWithWhiteChars = rtrim($textNode->getText()) != $textNode->getText();
  37. }
  38. $firstText = $this->paragraph->getChild(0);
  39. $this->assertTrue($firstText->getText() == ltrim($firstText->getText()), 'first text element isnt left trimmed');
  40. }
  41. public function textProvider()
  42. {
  43. return array(
  44. array(
  45. array('some text ', ' some another text'),
  46. ),
  47. array(
  48. array(' some text ', ' some another text ', ' some another text'),
  49. ),
  50. );
  51. }
  52. /**
  53. * @test
  54. */
  55. public function dontTrimAllWhiteSpacesFromTheMiddleText()
  56. {
  57. $firstText = new Text('abc');
  58. $emptyText = new Text(' ');
  59. $lastText = new Text('abc');
  60. foreach(array($firstText, $emptyText, $lastText) as $text)
  61. {
  62. $this->paragraph->add($text);
  63. }
  64. $this->assertEquals(' ', $emptyText->getText());
  65. }
  66. /**
  67. * @test
  68. */
  69. public function formatLinesWhileGettingTasks()
  70. {
  71. $documentStub = $this->createDocumentStub();
  72. $expectedTasks = array();
  73. for($i=0; $i<3; $i++)
  74. {
  75. $line = $this->getMockBuilder('PHPPdf\Core\Node\Paragraph\Line')
  76. ->setMethods(array('format'))
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $line->expects($this->once())
  80. ->method('format');
  81. $this->paragraph->addLine($line);
  82. }
  83. $tasks = new DrawingTaskHeap();
  84. $this->paragraph->collectOrderedDrawingTasks($documentStub, $tasks);
  85. }
  86. /**
  87. * @test
  88. */
  89. public function getDrawingTasksFromTextObjects()
  90. {
  91. $documentStub = $this->createDocumentStub();
  92. $expectedTasks = array();
  93. $tasks = new DrawingTaskHeap();
  94. for($i=0; $i<3; $i++)
  95. {
  96. $text = $this->getMockBuilder('PHPPdf\Core\Node\Text')
  97. ->setMethods(array('collectOrderedDrawingTasks'))
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $taskStub = new DrawingTask(function(){});
  101. $expectedTasks[] = $taskStub;
  102. $text->expects($this->once())
  103. ->method('collectOrderedDrawingTasks')
  104. ->with($documentStub, $this->isInstanceOf('PHPPdf\Core\DrawingTaskHeap'))
  105. ->will($this->returnCallback(function() use($tasks, $taskStub){
  106. $tasks->insert($taskStub);
  107. }));
  108. $this->paragraph->add($text);
  109. }
  110. $this->paragraph->collectOrderedDrawingTasks($documentStub, $tasks);
  111. $this->assertEquals(count($expectedTasks), count($tasks));
  112. }
  113. /**
  114. * @test
  115. * @dataProvider propagateParagraphWidthToParentExceptPageProvider
  116. */
  117. public function propagateParagraphWidthToParentExceptPage($isParentPage, $parentWidth, $paragraphWidth, $expectsParentWidth)
  118. {
  119. $parent = $isParentPage ? new Page() : new Container();
  120. $parent->setWidth($parentWidth);
  121. $this->paragraph->setParent($parent);
  122. $this->paragraph->setWidth($paragraphWidth);
  123. $this->assertEquals($expectsParentWidth, $parent->getWidth());
  124. }
  125. public function propagateParagraphWidthToParentExceptPageProvider()
  126. {
  127. return array(
  128. array(false, 100, 200, 200),
  129. array(true, 100, 200, 100),
  130. );
  131. }
  132. /**
  133. * _____________________________
  134. * | |
  135. * | ______________|
  136. * |_____________| | <- breaking here
  137. * | |
  138. * |____________________________|
  139. *
  140. * @test
  141. */
  142. public function breaking()
  143. {
  144. $x = 0;
  145. $y = 500;
  146. $width = 500;
  147. $height = 500;
  148. $firstPoint = Point::getInstance($x, $y);
  149. $paragraphParent = new Container();
  150. $paragraphParent->setWidth($width);
  151. $paragraphParent->setHeight($height);
  152. $paragraph = new Paragraph();
  153. $paragraph->setParent($paragraphParent);
  154. $text1 = new Text();
  155. $text1->getBoundary()->setNext($firstPoint)
  156. ->setNext($firstPoint->translate($width, 0))
  157. ->setNext($firstPoint->translate($width, 200))
  158. ->setNext($firstPoint->translate($width/2, 200))
  159. ->setNext($firstPoint->translate($width/2, 250))
  160. ->setNext($firstPoint->translate(0, 250))
  161. ->close();
  162. $text2 = new Text();
  163. $text2->getBoundary()->setNext($firstPoint->translate($width/2, 200))
  164. ->setNext($firstPoint->translate($width, 200))
  165. ->setNext($firstPoint->translate($width, 500))
  166. ->setNext($firstPoint->translate(0, 500))
  167. ->setNext($firstPoint->translate(0, 250))
  168. ->setNext($firstPoint->translate($width/2, 250))
  169. ->close();
  170. $text1->setWidth(500);
  171. $text1->setHeight(250);
  172. $text2->setWidth(500);
  173. $text2->setHeight(300);
  174. $text1->setAttribute('line-height', 100);
  175. $text2->setAttribute('line-height', 100);
  176. for($i=0; $i<2; $i++)
  177. {
  178. $line = new Line($paragraph, 0, $i*100);
  179. $part = new LinePart('', 500, 0, $text1);
  180. $line->addPart($part);
  181. $paragraph->addLine($line);
  182. }
  183. $line = new Line($paragraph, 0, 200);
  184. $line->addPart(new LinePart('', $width/2, 0, $text1));
  185. $line->addPart(new LinePart('', $width/2, 250, $text2));
  186. $paragraph->addLine($line);
  187. for($i=0; $i<2; $i++)
  188. {
  189. $line = new Line($paragraph, 0, ($i+3)*100);
  190. $part = new LinePart('', 500, 0, $text2);
  191. $line->addPart($part);
  192. $paragraph->addLine($line);
  193. }
  194. $paragraph->add($text1);
  195. $paragraph->add($text2);
  196. $paragraph->getBoundary()->setNext($firstPoint)
  197. ->setNext($firstPoint->translate($width, 0))
  198. ->setNext($firstPoint->translate($width, 500))
  199. ->setNext($firstPoint->translate(0, 500))
  200. ->close();
  201. $paragraph->setHeight(500);
  202. $paragraphProduct = $paragraph->breakAt(225);
  203. $this->assertEquals(200, $paragraph->getHeight());
  204. $this->assertEquals(200, $paragraph->getFirstPoint()->getY() - $paragraph->getDiagonalPoint()->getY());
  205. $this->assertEquals(300, $paragraphProduct->getHeight());
  206. $this->assertEquals(300, $paragraphProduct->getFirstPoint()->getY() - $paragraphProduct->getDiagonalPoint()->getY());
  207. $this->assertTrue($paragraphProduct !== null);
  208. $this->assertEquals(200, $paragraph->getHeight());
  209. $this->assertEquals(2, count($paragraph->getLines()));
  210. $this->assertEquals(3, count($paragraphProduct->getLines()));
  211. $this->assertEquals(1, count($paragraph->getChildren()));
  212. $this->assertEquals(2, count($paragraphProduct->getChildren()));
  213. foreach($paragraphProduct->getLines() as $i => $line)
  214. {
  215. $this->assertEquals($i*100, $line->getYTranslation());
  216. foreach($line->getParts() as $part)
  217. {
  218. // $this->assertTrue($part->getText() !== $text1);
  219. // $this->assertTrue($part->getText() !== $text2);
  220. }
  221. }
  222. }
  223. /**
  224. * @test
  225. * @dataProvider linesWidthsProvider
  226. */
  227. public function minWidthIsMaxOfLineMinWidth(array $linesWidths)
  228. {
  229. foreach($linesWidths as $width)
  230. {
  231. $line = $this->getMockBuilder('PHPPdf\Core\Node\Paragraph\Line')
  232. ->setMethods(array('getTotalWidth'))
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $line->expects($this->atLeastOnce())
  236. ->method('getTotalWidth')
  237. ->will($this->returnValue($width));
  238. $this->paragraph->addLine($line);
  239. }
  240. $expectedMinWidth = max($linesWidths);
  241. $this->assertEquals($expectedMinWidth, $this->paragraph->getMinWidth());
  242. }
  243. public function linesWidthsProvider()
  244. {
  245. return array(
  246. array(array(
  247. 10, 20 ,30
  248. )),
  249. array(array(
  250. 20, 20 ,10
  251. )),
  252. );
  253. }
  254. }