RichTextTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * This file is part of PHPPresentation - A pure PHP library for reading and writing
  4. * presentations documents.
  5. *
  6. * PHPPresentation is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
  12. *
  13. * @copyright 2009-2015 PHPPresentation contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. * @link https://github.com/PHPOffice/PHPPresentation
  16. */
  17. namespace PhpOffice\PhpPresentation\Tests\Shape;
  18. use PhpOffice\PhpPresentation\Shape\RichText;
  19. use PhpOffice\PhpPresentation\Shape\RichText\TextElement;
  20. use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
  21. /**
  22. * Test class for RichText element
  23. *
  24. * @coversDefaultClass PhpOffice\PhpPresentation\Shape\RichText
  25. */
  26. class RichTextTest extends \PHPUnit_Framework_TestCase
  27. {
  28. public function testConstruct()
  29. {
  30. $object = new RichText();
  31. $this->assertEquals(0, $object->getActiveParagraphIndex());
  32. $this->assertCount(1, $object->getParagraphs());
  33. }
  34. public function testActiveParagraph()
  35. {
  36. $object = new RichText();
  37. $this->assertEquals(0, $object->getActiveParagraphIndex());
  38. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->createParagraph());
  39. $this->assertCount(2, $object->getParagraphs());
  40. $value = rand(0, 1);
  41. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setActiveParagraph($value));
  42. $this->assertEquals($value, $object->getActiveParagraphIndex());
  43. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->getActiveParagraph());
  44. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->getParagraph());
  45. $value = rand(0, 1);
  46. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->getParagraph($value));
  47. }
  48. /**
  49. * @expectedException \Exception
  50. * expectedExceptionMessage Invalid paragraph count.
  51. */
  52. public function testActiveParagraphException()
  53. {
  54. $object = new RichText();
  55. $object->setActiveParagraph(1000);
  56. }
  57. /**
  58. * @expectedException \Exception
  59. * expectedExceptionMessage Invalid paragraph count.
  60. */
  61. public function testGetParagraphException()
  62. {
  63. $object = new RichText();
  64. $object->getParagraph(1000);
  65. }
  66. public function testColumns()
  67. {
  68. $object = new RichText();
  69. $value = rand(1, 16);
  70. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setColumns($value));
  71. $this->assertEquals($value, $object->getColumns());
  72. }
  73. /**
  74. * @expectedException \Exception
  75. * expectedExceptionMessage Number of columns should be 1-16
  76. */
  77. public function testColumnsException()
  78. {
  79. $object = new RichText();
  80. $object->setColumns(1000);
  81. }
  82. public function testParagraphs()
  83. {
  84. $object = new RichText();
  85. $array = array(
  86. new Paragraph(),
  87. new Paragraph(),
  88. new Paragraph(),
  89. );
  90. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setParagraphs($array));
  91. $this->assertCount(3, $object->getParagraphs());
  92. $this->assertEquals(2, $object->getActiveParagraphIndex());
  93. }
  94. /**
  95. * @expectedException \Exception
  96. * expectedExceptionMessage Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.
  97. */
  98. public function testParagraphsException()
  99. {
  100. $object = new RichText();
  101. $object->setParagraphs(1000);
  102. }
  103. public function testText()
  104. {
  105. $object = new RichText();
  106. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->addText());
  107. $this->assertCount(1, $object->getActiveParagraph()->getRichTextElements());
  108. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->addText(new TextElement()));
  109. $this->assertCount(2, $object->getActiveParagraph()->getRichTextElements());
  110. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText());
  111. $this->assertCount(3, $object->getActiveParagraph()->getRichTextElements());
  112. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText('ALPHA'));
  113. $this->assertCount(4, $object->getActiveParagraph()->getRichTextElements());
  114. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $object->createBreak());
  115. $this->assertCount(5, $object->getActiveParagraph()->getRichTextElements());
  116. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun());
  117. $this->assertCount(6, $object->getActiveParagraph()->getRichTextElements());
  118. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun('BETA'));
  119. $this->assertCount(7, $object->getActiveParagraph()->getRichTextElements());
  120. $this->assertEquals('ALPHA'."\r\n".'BETA', $object->getPlainText());
  121. $this->assertEquals('ALPHA'."\r\n".'BETA', (string) $object);
  122. }
  123. public function testGetSetAutoFit()
  124. {
  125. $object = new RichText();
  126. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoFit());
  127. $this->assertEquals(RichText::AUTOFIT_DEFAULT, $object->getAutoFit());
  128. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoFit(RichText::AUTOFIT_NORMAL));
  129. $this->assertEquals(RichText::AUTOFIT_NORMAL, $object->getAutoFit());
  130. }
  131. public function testGetSetHAutoShrink()
  132. {
  133. $object = new RichText();
  134. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkHorizontal());
  135. $this->assertNull($object->hasAutoShrinkHorizontal());
  136. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkHorizontal(2));
  137. $this->assertNull($object->hasAutoShrinkHorizontal());
  138. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkHorizontal(true));
  139. $this->assertTrue($object->hasAutoShrinkHorizontal());
  140. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkHorizontal(false));
  141. $this->assertFalse($object->hasAutoShrinkHorizontal());
  142. }
  143. public function testGetSetVAutoShrink()
  144. {
  145. $object = new RichText();
  146. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkVertical());
  147. $this->assertNull($object->hasAutoShrinkVertical());
  148. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkVertical(2));
  149. $this->assertNull($object->hasAutoShrinkVertical());
  150. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkVertical(true));
  151. $this->assertTrue($object->hasAutoShrinkVertical());
  152. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setAutoShrinkVertical(false));
  153. $this->assertFalse($object->hasAutoShrinkVertical());
  154. }
  155. public function testGetSetHOverflow()
  156. {
  157. $object = new RichText();
  158. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setHorizontalOverflow());
  159. $this->assertEquals(RichText::OVERFLOW_OVERFLOW, $object->getHorizontalOverflow());
  160. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setHorizontalOverflow(RichText::OVERFLOW_CLIP));
  161. $this->assertEquals(RichText::OVERFLOW_CLIP, $object->getHorizontalOverflow());
  162. }
  163. public function testGetSetInset()
  164. {
  165. $object = new RichText();
  166. // Default
  167. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetBottom());
  168. $this->assertEquals(4.8, $object->getInsetBottom());
  169. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetLeft());
  170. $this->assertEquals(9.6, $object->getInsetLeft());
  171. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetRight());
  172. $this->assertEquals(9.6, $object->getInsetRight());
  173. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetTop());
  174. $this->assertEquals(4.8, $object->getInsetTop());
  175. // Value
  176. $value = rand(1, 100);
  177. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetBottom($value));
  178. $this->assertEquals($value, $object->getInsetBottom());
  179. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetLeft($value));
  180. $this->assertEquals($value, $object->getInsetLeft());
  181. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetRight($value));
  182. $this->assertEquals($value, $object->getInsetRight());
  183. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setInsetTop($value));
  184. $this->assertEquals($value, $object->getInsetTop());
  185. }
  186. public function testGetSetUpright()
  187. {
  188. $object = new RichText();
  189. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setUpright());
  190. $this->assertFalse($object->isUpright());
  191. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setUpright(true));
  192. $this->assertTrue($object->isUpright());
  193. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setUpright(false));
  194. $this->assertFalse($object->isUpright());
  195. }
  196. public function testGetSetVertical()
  197. {
  198. $object = new RichText();
  199. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setVertical());
  200. $this->assertFalse($object->isVertical());
  201. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setVertical(true));
  202. $this->assertTrue($object->isVertical());
  203. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setVertical(false));
  204. $this->assertFalse($object->isVertical());
  205. }
  206. public function testGetSetVOverflow()
  207. {
  208. $object = new RichText();
  209. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setVerticalOverflow());
  210. $this->assertEquals(RichText::OVERFLOW_OVERFLOW, $object->getVerticalOverflow());
  211. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setVerticalOverflow(RichText::OVERFLOW_CLIP));
  212. $this->assertEquals(RichText::OVERFLOW_CLIP, $object->getVerticalOverflow());
  213. }
  214. public function testGetSetWrap()
  215. {
  216. $object = new RichText();
  217. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setWrap());
  218. $this->assertEquals(RichText::WRAP_SQUARE, $object->getWrap());
  219. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText', $object->setWrap(RichText::WRAP_NONE));
  220. $this->assertEquals(RichText::WRAP_NONE, $object->getWrap());
  221. }
  222. public function testHashCode()
  223. {
  224. $object = new RichText();
  225. $hash = $object->getActiveParagraph()->getHashCode();
  226. $hash .= RichText::WRAP_SQUARE.RichText::AUTOFIT_DEFAULT.RichText::OVERFLOW_OVERFLOW.RichText::OVERFLOW_OVERFLOW.'0014.89.69.64.8';
  227. $hash .= md5('00000' . $object->getFill()->getHashCode() . $object->getShadow()->getHashCode() . '' . get_parent_class($object));
  228. $hash .= get_class($object);
  229. $this->assertEquals(md5($hash), $object->getHashCode());
  230. }
  231. }