CellTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Table;
  18. use PhpOffice\PhpPresentation\Shape\Table\Cell;
  19. use PhpOffice\PhpPresentation\Shape\RichText\Paragraph;
  20. use PhpOffice\PhpPresentation\Shape\RichText\TextElement;
  21. use PhpOffice\PhpPresentation\Style\Borders;
  22. use PhpOffice\PhpPresentation\Style\Fill;
  23. /**
  24. * Test class for Cell element
  25. *
  26. * @coversDefaultClass PhpOffice\PhpPresentation\Shape\Cell
  27. */
  28. class CellTest extends \PHPUnit_Framework_TestCase
  29. {
  30. /**
  31. * Test can read
  32. */
  33. public function testConstruct()
  34. {
  35. $object = new Cell();
  36. $this->assertEquals(0, $object->getActiveParagraphIndex());
  37. $this->assertCount(1, $object->getParagraphs());
  38. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Fill', $object->getFill());
  39. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Borders', $object->getBorders());
  40. }
  41. public function testActiveParagraph()
  42. {
  43. $object = new Cell();
  44. $this->assertEquals(0, $object->getActiveParagraphIndex());
  45. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->createParagraph());
  46. $this->assertCount(2, $object->getParagraphs());
  47. $value = rand(0, 1);
  48. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->setActiveParagraph($value));
  49. $this->assertEquals($value, $object->getActiveParagraphIndex());
  50. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->getActiveParagraph());
  51. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->getParagraph());
  52. $value = rand(0, 1);
  53. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->getParagraph($value));
  54. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setParagraphs(array()));
  55. $this->assertCount(0, $object->getParagraphs());
  56. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Paragraph', $object->createParagraph());
  57. $this->assertCount(1, $object->getParagraphs());
  58. }
  59. /**
  60. * @expectedException \Exception
  61. * expectedExceptionMessage Invalid paragraph count.
  62. */
  63. public function testActiveParagraphException()
  64. {
  65. $object = new Cell();
  66. $object->setActiveParagraph(1000);
  67. }
  68. /**
  69. * @expectedException \Exception
  70. * expectedExceptionMessage Invalid paragraph count.
  71. */
  72. public function testGetParagraphException()
  73. {
  74. $object = new Cell();
  75. $object->getParagraph(1000);
  76. }
  77. /**
  78. * Test get/set hash index
  79. */
  80. public function testSetGetHashIndex()
  81. {
  82. $object = new Cell();
  83. $value = rand(1, 100);
  84. $object->setHashIndex($value);
  85. $this->assertEquals($value, $object->getHashIndex());
  86. }
  87. public function testText()
  88. {
  89. $object = new Cell();
  90. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->addText());
  91. $this->assertCount(1, $object->getActiveParagraph()->getRichTextElements());
  92. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->addText(new TextElement()));
  93. $this->assertCount(2, $object->getActiveParagraph()->getRichTextElements());
  94. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText());
  95. $this->assertCount(3, $object->getActiveParagraph()->getRichTextElements());
  96. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText('ALPHA'));
  97. $this->assertCount(4, $object->getActiveParagraph()->getRichTextElements());
  98. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $object->createBreak());
  99. $this->assertCount(5, $object->getActiveParagraph()->getRichTextElements());
  100. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun());
  101. $this->assertCount(6, $object->getActiveParagraph()->getRichTextElements());
  102. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun('BETA'));
  103. $this->assertCount(7, $object->getActiveParagraph()->getRichTextElements());
  104. $this->assertEquals('ALPHA'."\r\n".'BETA', $object->getPlainText());
  105. $this->assertEquals('ALPHA'."\r\n".'BETA', (string) $object);
  106. }
  107. public function testParagraphs()
  108. {
  109. $object = new Cell();
  110. $array = array(
  111. new Paragraph(),
  112. new Paragraph(),
  113. new Paragraph(),
  114. );
  115. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setParagraphs($array));
  116. $this->assertCount(3, $object->getParagraphs());
  117. $this->assertEquals(2, $object->getActiveParagraphIndex());
  118. }
  119. /**
  120. * @expectedException \Exception
  121. * expectedExceptionMessage Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed.
  122. */
  123. public function testParagraphsException()
  124. {
  125. $object = new Cell();
  126. $object->setParagraphs(1000);
  127. }
  128. public function testGetSetBorders()
  129. {
  130. $object = new Cell();
  131. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setBorders(new Borders()));
  132. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Borders', $object->getBorders());
  133. }
  134. public function testGetSetColspan()
  135. {
  136. $object = new Cell();
  137. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setColSpan());
  138. $this->assertEquals(0, $object->getColSpan());
  139. $value = rand(1, 100);
  140. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setColSpan($value));
  141. $this->assertEquals($value, $object->getColSpan());
  142. }
  143. public function testGetSetFill()
  144. {
  145. $object = new Cell();
  146. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setFill(new Fill()));
  147. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Fill', $object->getFill());
  148. }
  149. public function testGetSetRowspan()
  150. {
  151. $object = new Cell();
  152. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setRowSpan());
  153. $this->assertEquals(0, $object->getRowSpan());
  154. $value = rand(1, 100);
  155. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setRowSpan($value));
  156. $this->assertEquals($value, $object->getRowSpan());
  157. }
  158. public function testGetSetWidth()
  159. {
  160. $object = new Cell();
  161. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setWidth());
  162. $this->assertEquals(0, $object->getWidth());
  163. $value = rand(1, 100);
  164. $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->setWidth($value));
  165. $this->assertEquals($value, $object->getWidth());
  166. }
  167. }