ParagraphTest.p 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\RichText;
  18. use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
  19. use PhpOffice\PhpPresentation\Shape\RichText\TextElement;
  20. use PhpOffice\PhpPresentation\Style\Alignment;
  21. use PhpOffice\PhpPresentation\Style\Bullet;
  22. use PhpOffice\PhpPresentation\Style\Font;
  23. /**
  24. * Test class for Paragraph element
  25. *
  26. * @coversDefaultClass PhpOffice\PhpPresentation\Shape\RichText\Paragraph
  27. */
  28. class ParagraphTest extends \PHPUnit_Framework_TestCase
  29. {
  30. /**
  31. * Test can read
  32. */
  33. public function testConstruct()
  34. {
  35. $object = new Paragraph();
  36. $this->assertEmpty($object->getRichTextElements());
  37. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->getAlignment());
  38. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont());
  39. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Bullet', $object->getBulletStyle());
  40. }
  41. public function testAlignment()
  42. {
  43. $object = new Paragraph();
  44. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Alignment', $object->getAlignment());
  45. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setAlignment(new Alignment()));
  46. }
  47. /**
  48. * Test get/set bullet style
  49. */
  50. public function testBulletStyle()
  51. {
  52. $object = new Paragraph();
  53. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Bullet', $object->getBulletStyle());
  54. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setBulletStyle());
  55. $this->assertNull($object->getBulletStyle());
  56. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setBulletStyle(new Bullet()));
  57. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Bullet', $object->getBulletStyle());
  58. }
  59. /**
  60. * Test get/set font
  61. */
  62. public function testFont()
  63. {
  64. $object = new Paragraph();
  65. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont());
  66. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setFont());
  67. $this->assertNull($object->getFont());
  68. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setFont(new Font()));
  69. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Font', $object->getFont());
  70. }
  71. /**
  72. * Test get/set hashCode
  73. */
  74. public function testHashCode()
  75. {
  76. $object = new Paragraph();
  77. $oElement = new TextElement();
  78. $object->addText($oElement);
  79. $this->assertEquals(md5($oElement->getHashCode().$object->getFont()->getHashCode().get_class($object)), $object->getHashCode());
  80. }
  81. /**
  82. * Test get/set hashIndex
  83. */
  84. public function testHashIndex()
  85. {
  86. $object = new Paragraph();
  87. $value = rand(1, 100);
  88. $object->setHashIndex($value);
  89. $this->assertEquals($value, $object->getHashIndex());
  90. }
  91. /**
  92. * Test get/set linespacing
  93. */
  94. public function testLineSpacing()
  95. {
  96. $object = new Paragraph();
  97. $valueExpected = rand(1, 100);
  98. $this->assertEquals(100, $object->getLineSpacing());
  99. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setLineSpacing($valueExpected));
  100. $this->assertEquals($valueExpected, $object->getLineSpacing());
  101. }
  102. /**
  103. * Test get/set richTextElements
  104. */
  105. public function testRichTextElements()
  106. {
  107. $object = new Paragraph();
  108. $this->assertInternalType('array', $object->getRichTextElements());
  109. $this->assertEmpty($object->getRichTextElements());
  110. $object->createBreak();
  111. $this->assertCount(1, $object->getRichTextElements());
  112. $array = array(
  113. new TextElement(),
  114. new TextElement(),
  115. new TextElement(),
  116. );
  117. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setRichTextElements($array));
  118. $this->assertCount(3, $object->getRichTextElements());
  119. }
  120. /**
  121. * @expectedException \Exception
  122. * expectedExceptionMessage Invalid \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface[] array passed.
  123. */
  124. public function testRichTextElementsException()
  125. {
  126. $object = new Paragraph();
  127. $object->setRichTextElements(1);
  128. }
  129. /**
  130. * Test text methods
  131. */
  132. public function testText()
  133. {
  134. $object = new Paragraph();
  135. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->addText(new TextElement()));
  136. $this->assertcount(1, $object->getRichTextElements());
  137. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText());
  138. $this->assertcount(2, $object->getRichTextElements());
  139. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText('AAA'));
  140. $this->assertcount(3, $object->getRichTextElements());
  141. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $object->createBreak());
  142. $this->assertcount(4, $object->getRichTextElements());
  143. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun());
  144. $this->assertcount(5, $object->getRichTextElements());
  145. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun('BBB'));
  146. $this->assertcount(6, $object->getRichTextElements());
  147. $this->assertEquals('AAA'."\r\n".'BBB', $object->getPlainText());
  148. $this->assertEquals('AAA'."\r\n".'BBB', (string) $object);
  149. }
  150. }