Skip to content

Commit a902537

Browse files
committed
test the reader with the writer
1 parent 5c84adf commit a902537

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed

Diff for: docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Bump dompdf/dompdf from 2.0.4 to 3.0.0 by [@dependabot](https://github.com/dependabot) fixing [#2621](https://github.com/PHPOffice/PHPWord/issues/2621) in [#2666](https://github.com/PHPOffice/PHPWord/pull/2666)
3636
- Add test case to make sure vMerge defaults to 'continue' by [@SpraxDev](https://github.com/SpraxDev) in [#2677](https://github.com/PHPOffice/PHPWord/pull/2677)
3737
- Adding the possibility to use iterate search and replace with setValues by [@moghwan](https://github.com/moghwan) in [#2632](https://github.com/PHPOffice/PHPWord/pull/2640)
38+
- Add test cases that test the ODTText and Word2007 reader using the corresponding writer, increasing test coverage by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2745](https://github.com/PHPOffice/PHPWord/pull/2745)
3839

3940
### Deprecations
4041
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`

Diff for: tests/PhpWordTests/WriteReadback/ODTextTest.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\Writer\ODText;
25+
26+
/**
27+
* Test class for PhpOffice\PhpWord\Reader\ODText and PhpOffice\PhpWord\Writer\ODText.
28+
*
29+
* @coversDefaultClass \PhpOffice\PhpWord\Reader\ODText
30+
*
31+
* @runTestsInSeparateProcesses
32+
*/
33+
class ODTextTest extends \PHPUnit\Framework\TestCase
34+
{
35+
/**
36+
* Test a document with one section and text.
37+
*/
38+
public function testOneSectionWithText(): void
39+
{
40+
$phpWordWriter = new PhpWord();
41+
$testText = 'Hello World!';
42+
$sectionWriter = $phpWordWriter->addSection();
43+
$sectionWriter->addText($testText);
44+
45+
$writer = new ODText($phpWordWriter);
46+
$file = __DIR__ . '/../_files/temp.odt';
47+
$writer->save($file);
48+
49+
self::assertFileExists($file);
50+
51+
$phpWordReader = IOFactory::load($file, 'ODText');
52+
53+
self::assertCount(1, $phpWordReader->getSections());
54+
self::assertCount(1, $phpWordReader->getSections()[0]->getElements());
55+
self::assertInstanceOf(TextRun::class, $phpWordReader->getSections()[0]->getElements()[0]);
56+
self::assertEquals($testText, $phpWordReader->getSections()[0]->getElements()[0]->getText());
57+
unlink($file);
58+
}
59+
}

Diff for: tests/PhpWordTests/WriteReadback/Word2007Test.php

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)