PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PhpWord/Tests/Element/CellTest.php

https://github.com/cyrillkalita/PHPWord
PHP | 275 lines | 151 code | 40 blank | 84 comment | 0 complexity | cd119e8c747a4de2298b2bbee60ce169 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord 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/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Tests\Element;
  18. use PhpOffice\PhpWord\Element\Cell;
  19. /**
  20. * Test class for PhpOffice\PhpWord\Element\Cell
  21. *
  22. * @runTestsInSeparateProcesses
  23. */
  24. class CellTest extends \PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * New instance
  28. */
  29. public function testConstruct()
  30. {
  31. $oCell = new Cell();
  32. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Cell', $oCell);
  33. $this->assertEquals($oCell->getWidth(), null);
  34. }
  35. /**
  36. * New instance with array
  37. */
  38. public function testConstructWithStyleArray()
  39. {
  40. $oCell = new Cell(null, array('valign' => 'center'));
  41. $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Cell', $oCell->getStyle());
  42. $this->assertEquals($oCell->getWidth(), null);
  43. }
  44. /**
  45. * Add text
  46. */
  47. public function testAddText()
  48. {
  49. $oCell = new Cell();
  50. $element = $oCell->addText('text');
  51. $this->assertCount(1, $oCell->getElements());
  52. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
  53. }
  54. /**
  55. * Add non-UTF8
  56. */
  57. public function testAddTextNotUTF8()
  58. {
  59. $oCell = new Cell();
  60. $element = $oCell->addText(utf8_decode('ééé'));
  61. $this->assertCount(1, $oCell->getElements());
  62. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
  63. $this->assertEquals($element->getText(), 'ééé');
  64. }
  65. /**
  66. * Add link
  67. */
  68. public function testAddLink()
  69. {
  70. $oCell = new Cell();
  71. $element = $oCell->addLink(utf8_decode('ééé'), utf8_decode('ééé'));
  72. $this->assertCount(1, $oCell->getElements());
  73. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
  74. }
  75. /**
  76. * Add text break
  77. */
  78. public function testAddTextBreak()
  79. {
  80. $oCell = new Cell();
  81. $oCell->addTextBreak();
  82. $this->assertCount(1, $oCell->getElements());
  83. }
  84. /**
  85. * Add list item
  86. */
  87. public function testAddListItem()
  88. {
  89. $oCell = new Cell();
  90. $element = $oCell->addListItem('text');
  91. $this->assertCount(1, $oCell->getElements());
  92. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element);
  93. $this->assertEquals($element->getTextObject()->getText(), 'text');
  94. }
  95. /**
  96. * Add list item non-UTF8
  97. */
  98. public function testAddListItemNotUTF8()
  99. {
  100. $oCell = new Cell();
  101. $element = $oCell->addListItem(utf8_decode('ééé'));
  102. $this->assertCount(1, $oCell->getElements());
  103. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\ListItem', $element);
  104. $this->assertEquals($element->getTextObject()->getText(), 'ééé');
  105. }
  106. /**
  107. * Add image section
  108. */
  109. public function testAddImageSection()
  110. {
  111. $src = __DIR__ . "/../_files/images/earth.jpg";
  112. $oCell = new Cell();
  113. $element = $oCell->addImage($src);
  114. $this->assertCount(1, $oCell->getElements());
  115. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
  116. }
  117. /**
  118. * Add image header
  119. */
  120. public function testAddImageHeader()
  121. {
  122. $src = __DIR__ . "/../_files/images/earth.jpg";
  123. $oCell = new Cell('header', 1);
  124. $element = $oCell->addImage($src);
  125. $this->assertCount(1, $oCell->getElements());
  126. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
  127. }
  128. /**
  129. * Add image footer
  130. */
  131. public function testAddImageFooter()
  132. {
  133. $src = __DIR__ . "/../_files/images/earth.jpg";
  134. $oCell = new Cell('footer', 1);
  135. $element = $oCell->addImage($src);
  136. $this->assertCount(1, $oCell->getElements());
  137. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
  138. }
  139. /**
  140. * Add image section by URL
  141. */
  142. public function testAddImageSectionByUrl()
  143. {
  144. $oCell = new Cell();
  145. $element = $oCell->addImage(
  146. 'http://php.net/images/logos/php-med-trans-light.gif'
  147. );
  148. $this->assertCount(1, $oCell->getElements());
  149. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
  150. }
  151. /**
  152. * Add object
  153. */
  154. public function testAddObjectXLS()
  155. {
  156. $src = __DIR__ . "/../_files/documents/sheet.xls";
  157. $oCell = new Cell();
  158. $element = $oCell->addObject($src);
  159. $this->assertCount(1, $oCell->getElements());
  160. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Object', $element);
  161. }
  162. /**
  163. * Test add object exception
  164. *
  165. * @expectedException \PhpOffice\PhpWord\Exception\InvalidObjectException
  166. */
  167. public function testAddObjectException()
  168. {
  169. $src = __DIR__ . "/../_files/xsl/passthrough.xsl";
  170. $oCell = new Cell();
  171. $oCell->addObject($src);
  172. }
  173. /**
  174. * Add preserve text
  175. */
  176. public function testAddPreserveText()
  177. {
  178. $oCell = new Cell();
  179. $oCell->setDocPart('Header', 1);
  180. $element = $oCell->addPreserveText('text');
  181. $this->assertCount(1, $oCell->getElements());
  182. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
  183. }
  184. /**
  185. * Add preserve text non-UTF8
  186. */
  187. public function testAddPreserveTextNotUTF8()
  188. {
  189. $oCell = new Cell();
  190. $oCell->setDocPart('Header', 1);
  191. $element = $oCell->addPreserveText(utf8_decode('ééé'));
  192. $this->assertCount(1, $oCell->getElements());
  193. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\PreserveText', $element);
  194. $this->assertEquals($element->getText(), array('ééé'));
  195. }
  196. /**
  197. * Add preserve text exception
  198. *
  199. * @expectedException \BadMethodCallException
  200. */
  201. public function testAddPreserveTextException()
  202. {
  203. $oCell = new Cell();
  204. $oCell->setDocPart('Section', 1);
  205. $oCell->addPreserveText('text');
  206. }
  207. /**
  208. * Add text run
  209. */
  210. public function testCreateTextRun()
  211. {
  212. $oCell = new Cell();
  213. $element = $oCell->addTextRun();
  214. $this->assertCount(1, $oCell->getElements());
  215. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $element);
  216. }
  217. /**
  218. * Add check box
  219. */
  220. public function testAddCheckBox()
  221. {
  222. $oCell = new Cell();
  223. $element = $oCell->addCheckBox(utf8_decode('ééé'), utf8_decode('ééé'));
  224. $this->assertCount(1, $oCell->getElements());
  225. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\CheckBox', $element);
  226. }
  227. /**
  228. * Get elements
  229. */
  230. public function testGetElements()
  231. {
  232. $oCell = new Cell();
  233. $this->assertInternalType('array', $oCell->getElements());
  234. }
  235. }