暫無描述

PhpWordTest.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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;
  18. use PhpOffice\PhpWord\Metadata\DocInfo;
  19. use PhpOffice\PhpWord\PhpWord;
  20. use PhpOffice\PhpWord\Settings;
  21. use PhpOffice\PhpWord\Style;
  22. /**
  23. * Test class for PhpOffice\PhpWord\PhpWord
  24. *
  25. * @runTestsInSeparateProcesses
  26. */
  27. class PhpWordTest extends \PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * Test object creation
  31. */
  32. public function testConstruct()
  33. {
  34. $phpWord = new PhpWord();
  35. $this->assertEquals(new DocInfo(), $phpWord->getDocInfo());
  36. $this->assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
  37. $this->assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
  38. }
  39. /**
  40. * Test create/get section
  41. */
  42. public function testCreateGetSections()
  43. {
  44. $phpWord = new PhpWord();
  45. $phpWord->addSection();
  46. $this->assertCount(1, $phpWord->getSections());
  47. }
  48. /**
  49. * Test set/get default font name
  50. */
  51. public function testSetGetDefaultFontName()
  52. {
  53. $phpWord = new PhpWord();
  54. $fontName = 'Times New Roman';
  55. $this->assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName());
  56. $phpWord->setDefaultFontName($fontName);
  57. $this->assertEquals($fontName, $phpWord->getDefaultFontName());
  58. }
  59. /**
  60. * Test set/get default font size
  61. */
  62. public function testSetGetDefaultFontSize()
  63. {
  64. $phpWord = new PhpWord();
  65. $fontSize = 16;
  66. $this->assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize());
  67. $phpWord->setDefaultFontSize($fontSize);
  68. $this->assertEquals($fontSize, $phpWord->getDefaultFontSize());
  69. }
  70. /**
  71. * Test set default paragraph style
  72. */
  73. public function testSetDefaultParagraphStyle()
  74. {
  75. $phpWord = new PhpWord();
  76. $phpWord->setDefaultParagraphStyle(array());
  77. $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Paragraph', Style::getStyle('Normal'));
  78. }
  79. /**
  80. * Test add styles
  81. */
  82. public function testAddStyles()
  83. {
  84. $phpWord = new PhpWord();
  85. $styles = array(
  86. 'Paragraph' => 'Paragraph',
  87. 'Font' => 'Font',
  88. 'Table' => 'Table',
  89. 'Link' => 'Font',
  90. );
  91. foreach ($styles as $key => $value) {
  92. $method = "add{$key}Style";
  93. $styleId = "{$key} Style";
  94. $phpWord->$method($styleId, array());
  95. $this->assertInstanceOf("PhpOffice\\PhpWord\\Style\\{$value}", Style::getStyle($styleId));
  96. }
  97. }
  98. /**
  99. * Test add title style
  100. */
  101. public function testAddTitleStyle()
  102. {
  103. $phpWord = new PhpWord();
  104. $titleLevel = 1;
  105. $titleName = "Heading_{$titleLevel}";
  106. $phpWord->addTitleStyle($titleLevel, array());
  107. $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($titleName));
  108. }
  109. /**
  110. * Test load template
  111. *
  112. * @deprecated 0.12.0
  113. */
  114. public function testLoadTemplate()
  115. {
  116. $templateFqfn = __DIR__ . "/_files/templates/blank.docx";
  117. $phpWord = new PhpWord();
  118. $this->assertInstanceOf(
  119. 'PhpOffice\\PhpWord\\TemplateProcessor',
  120. $phpWord->loadTemplate($templateFqfn)
  121. );
  122. }
  123. /**
  124. * Test load template exception
  125. *
  126. * @deprecated 0.12.0
  127. *
  128. * @expectedException \PhpOffice\PhpWord\Exception\Exception
  129. */
  130. public function testLoadTemplateException()
  131. {
  132. $templateFqfn = join(
  133. DIRECTORY_SEPARATOR,
  134. array(PHPWORD_TESTS_BASE_DIR, 'PhpWord', 'Tests', '_files', 'templates', 'blanks.docx')
  135. );
  136. $phpWord = new PhpWord();
  137. $phpWord->loadTemplate($templateFqfn);
  138. }
  139. /**
  140. * Test save
  141. */
  142. public function testSave()
  143. {
  144. $phpWord = new PhpWord();
  145. $section = $phpWord->addSection();
  146. $section->addText('Hello world!');
  147. $this->assertTrue($phpWord->save('test.docx', 'Word2007', true));
  148. }
  149. /**
  150. * Test calling undefined method
  151. *
  152. * @expectedException \BadMethodCallException
  153. * @expectedExceptionMessage is not defined
  154. */
  155. public function testCallUndefinedMethod()
  156. {
  157. $phpWord = new PhpWord();
  158. $phpWord->undefinedMethod();
  159. }
  160. }