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

/tests/PhpWord/Tests/Element/TextRunTest.php

https://github.com/cyrillkalita/PHPWord
PHP | 156 lines | 83 code | 23 blank | 50 comment | 0 complexity | a606ceb12b02172aea83aecd64870c88 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\TextRun;
  19. use PhpOffice\PhpWord\PhpWord;
  20. /**
  21. * Test class for PhpOffice\PhpWord\Element\TextRun
  22. *
  23. * @runTestsInSeparateProcesses
  24. */
  25. class TextRunTest extends \PHPUnit_Framework_TestCase
  26. {
  27. /**
  28. * New instance
  29. */
  30. public function testConstructNull()
  31. {
  32. $oTextRun = new TextRun();
  33. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
  34. $this->assertCount(0, $oTextRun->getElements());
  35. $this->assertEquals($oTextRun->getParagraphStyle(), null);
  36. }
  37. /**
  38. * New instance with string
  39. */
  40. public function testConstructString()
  41. {
  42. $oTextRun = new TextRun('pStyle');
  43. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
  44. $this->assertCount(0, $oTextRun->getElements());
  45. $this->assertEquals($oTextRun->getParagraphStyle(), 'pStyle');
  46. }
  47. /**
  48. * New instance with array
  49. */
  50. public function testConstructArray()
  51. {
  52. $oTextRun = new TextRun(array('spacing' => 100));
  53. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\TextRun', $oTextRun);
  54. $this->assertCount(0, $oTextRun->getElements());
  55. $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', $oTextRun->getParagraphStyle());
  56. }
  57. /**
  58. * Add text
  59. */
  60. public function testAddText()
  61. {
  62. $oTextRun = new TextRun();
  63. $element = $oTextRun->addText('text');
  64. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
  65. $this->assertCount(1, $oTextRun->getElements());
  66. $this->assertEquals($element->getText(), 'text');
  67. }
  68. /**
  69. * Add text non-UTF8
  70. */
  71. public function testAddTextNotUTF8()
  72. {
  73. $oTextRun = new TextRun();
  74. $element = $oTextRun->addText(utf8_decode('ééé'));
  75. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Text', $element);
  76. $this->assertCount(1, $oTextRun->getElements());
  77. $this->assertEquals($element->getText(), 'ééé');
  78. }
  79. /**
  80. * Add link
  81. */
  82. public function testAddLink()
  83. {
  84. $oTextRun = new TextRun();
  85. $element = $oTextRun->addLink('http://www.google.fr');
  86. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
  87. $this->assertCount(1, $oTextRun->getElements());
  88. $this->assertEquals($element->getTarget(), 'http://www.google.fr');
  89. }
  90. /**
  91. * Add link with name
  92. */
  93. public function testAddLinkWithName()
  94. {
  95. $oTextRun = new TextRun();
  96. $element = $oTextRun->addLink('http://www.google.fr', utf8_decode('ééé'));
  97. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Link', $element);
  98. $this->assertCount(1, $oTextRun->getElements());
  99. $this->assertEquals($element->getTarget(), 'http://www.google.fr');
  100. $this->assertEquals($element->getText(), 'ééé');
  101. }
  102. /**
  103. * Add text break
  104. */
  105. public function testAddTextBreak()
  106. {
  107. $oTextRun = new TextRun();
  108. $oTextRun->addTextBreak(2);
  109. $this->assertCount(2, $oTextRun->getElements());
  110. }
  111. /**
  112. * Add image
  113. */
  114. public function testAddImage()
  115. {
  116. $src = __DIR__ . "/../_files/images/earth.jpg";
  117. $oTextRun = new TextRun();
  118. $element = $oTextRun->addImage($src);
  119. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $element);
  120. $this->assertCount(1, $oTextRun->getElements());
  121. }
  122. /**
  123. * Add footnote
  124. */
  125. public function testCreateFootnote()
  126. {
  127. $oTextRun = new TextRun();
  128. $oTextRun->setPhpWord(new PhpWord());
  129. $element = $oTextRun->addFootnote();
  130. $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Footnote', $element);
  131. $this->assertCount(1, $oTextRun->getElements());
  132. }
  133. }