|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 5 | + * word processing documents. |
| 6 | + * |
| 7 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 8 | + * General Public License version 3 as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please read the LICENSE |
| 11 | + * file that was distributed with this source code. For the full list of |
| 12 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 13 | + * |
| 14 | + * @see https://github.com/PHPOffice/PHPWord |
| 15 | + * |
| 16 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace PhpOffice\PhpWordTests\WriteReadback; |
| 20 | + |
| 21 | +use PhpOffice\PhpWord\Element\TextRun; |
| 22 | +use PhpOffice\PhpWord\IOFactory; |
| 23 | +use PhpOffice\PhpWord\PhpWord; |
| 24 | +use PhpOffice\PhpWord\Style\Font; |
| 25 | +use PhpOffice\PhpWord\Writer\Word2007; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test class for PhpOffice\PhpWord\Reader\Word2007 and PhpOffice\PhpWord\Writer\Word2007. |
| 29 | + * |
| 30 | + * @coversDefaultClass \PhpOffice\PhpWord\Reader\Word2007 |
| 31 | + * |
| 32 | + * @runTestsInSeparateProcesses |
| 33 | + */ |
| 34 | +class Word2007Test extends \PHPUnit\Framework\TestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * Test default font name. |
| 38 | + */ |
| 39 | + public function testDefaultFontName(): void |
| 40 | + { |
| 41 | + $phpWordWriter = new PhpWord(); |
| 42 | + $testDefaultFontName = 'Times New Roman'; |
| 43 | + $phpWordWriter->setDefaultFontName($testDefaultFontName); |
| 44 | + |
| 45 | + $writer = new Word2007($phpWordWriter); |
| 46 | + $file = __DIR__ . '/../_files/temp.docx'; |
| 47 | + $writer->save($file); |
| 48 | + |
| 49 | + self::assertFileExists($file); |
| 50 | + |
| 51 | + $phpWordReader = IOFactory::load($file, 'Word2007'); |
| 52 | + |
| 53 | + self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultFontName()); |
| 54 | + |
| 55 | + unlink($file); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test default Asian font name. |
| 60 | + */ |
| 61 | + public function testDefaultAsianFontName(): void |
| 62 | + { |
| 63 | + $phpWordWriter = new PhpWord(); |
| 64 | + $testDefaultFontName = '標楷體'; |
| 65 | + $phpWordWriter->setDefaultAsianFontName($testDefaultFontName); |
| 66 | + |
| 67 | + $writer = new Word2007($phpWordWriter); |
| 68 | + $file = __DIR__ . '/../_files/temp.docx'; |
| 69 | + $writer->save($file); |
| 70 | + |
| 71 | + self::assertFileExists($file); |
| 72 | + |
| 73 | + $phpWordReader = IOFactory::load($file, 'Word2007'); |
| 74 | + |
| 75 | + self::assertEquals($testDefaultFontName, $phpWordReader->getDefaultAsianFontName()); |
| 76 | + |
| 77 | + unlink($file); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test default font size. |
| 82 | + */ |
| 83 | + public function testDefaulFontSize(): void |
| 84 | + { |
| 85 | + $phpWordWriter = new PhpWord(); |
| 86 | + $testDefaultFontSize = 144; |
| 87 | + $phpWordWriter->setDefaultFontSize($testDefaultFontSize); |
| 88 | + |
| 89 | + $writer = new Word2007($phpWordWriter); |
| 90 | + $file = __DIR__ . '/../_files/temp.docx'; |
| 91 | + $writer->save($file); |
| 92 | + |
| 93 | + self::assertFileExists($file); |
| 94 | + |
| 95 | + $phpWordReader = IOFactory::load($file, 'Word2007'); |
| 96 | + |
| 97 | + self::assertEquals($testDefaultFontSize, $phpWordReader->getDefaultFontSize()); |
| 98 | + |
| 99 | + unlink($file); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test default font color. |
| 104 | + */ |
| 105 | + public function testDefaultFontColor(): void |
| 106 | + { |
| 107 | + $phpWordWriter = new PhpWord(); |
| 108 | + $testDefaultFontColor = '00FF00'; |
| 109 | + $phpWordWriter->setDefaultFontColor($testDefaultFontColor); |
| 110 | + |
| 111 | + $writer = new Word2007($phpWordWriter); |
| 112 | + $file = __DIR__ . '/../_files/temp.docx'; |
| 113 | + $writer->save($file); |
| 114 | + |
| 115 | + self::assertFileExists($file); |
| 116 | + |
| 117 | + $phpWordReader = IOFactory::load($file, 'Word2007'); |
| 118 | + |
| 119 | + self::assertEquals($testDefaultFontColor, $phpWordReader->getDefaultFontColor()); |
| 120 | + |
| 121 | + unlink($file); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Test Zoom. |
| 126 | + */ |
| 127 | + public function testZoom(): void |
| 128 | + { |
| 129 | + $phpWordWriter = new PhpWord(); |
| 130 | + $zoomLevel = 75; |
| 131 | + $phpWordWriter->getSettings()->setZoom($zoomLevel); |
| 132 | + |
| 133 | + $writer = new Word2007($phpWordWriter); |
| 134 | + $file = __DIR__ . '/../_files/temp.docx'; |
| 135 | + $writer->save($file); |
| 136 | + |
| 137 | + self::assertFileExists($file); |
| 138 | + |
| 139 | + $phpWordReader = IOFactory::load($file, 'Word2007'); |
| 140 | + |
| 141 | + self::assertEquals($zoomLevel, $phpWordReader->getSettings()->getZoom()); |
| 142 | + |
| 143 | + unlink($file); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test a document with one section and text. |
| 148 | + */ |
| 149 | + public function testOneSectionWithText(): void |
| 150 | + { |
| 151 | + $phpWordWriter = new PhpWord(); |
| 152 | + $testText = 'Hello World!'; |
| 153 | + $sectionWriter = $phpWordWriter->addSection(); |
| 154 | + $sectionWriter->addText($testText); |
| 155 | + |
| 156 | + $writer = new Word2007($phpWordWriter); |
| 157 | + $file = __DIR__ . '/../_files/temp.docx'; |
| 158 | + $writer->save($file); |
| 159 | + |
| 160 | + self::assertFileExists($file); |
| 161 | + |
| 162 | + $phpWordReader = IOFactory::load($file, 'Word2007'); |
| 163 | + |
| 164 | + self::assertCount(1, $phpWordReader->getSections()); |
| 165 | + self::assertCount(1, $phpWordReader->getSections()[0]->getElements()); |
| 166 | + self::assertInstanceOf(TextRun::class, $phpWordReader->getSections()[0]->getElements()[0]); |
| 167 | + self::assertEquals($testText, $phpWordReader->getSections()[0]->getElements()[0]->getText()); |
| 168 | + unlink($file); |
| 169 | + } |
| 170 | +} |
0 commit comments