Skip to content

Commit 20856fc

Browse files
authored
Merge pull request #1858 from oleibman/wordcoverage
Improve Word2007 Test Coverage
2 parents 7c7d7fd + 3738a68 commit 20856fc

File tree

7 files changed

+434
-49
lines changed

7 files changed

+434
-49
lines changed

src/PhpWord/Writer/Word2007/Style/AbstractStyle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function convertTwip($value, $default = 0)
9696
);
9797
$unit = Settings::getMeasurementUnit();
9898
$factor = 1;
99-
if (in_array($unit, $factors) && $value != $default) {
99+
if (array_key_exists($unit, $factors) && $value != $default) {
100100
$factor = $factors[$unit];
101101
}
102102

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\Word2007;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\Settings;
22+
use PhpOffice\PhpWord\TestHelperDOCX;
23+
24+
/**
25+
* Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace
26+
*/
27+
class ChartTest extends \PHPUnit\Framework\TestCase
28+
{
29+
private $outputEscapingEnabled;
30+
31+
/**
32+
* Executed before each method of the class
33+
*/
34+
public function setUp()
35+
{
36+
$this->outputEscapingEnabled = Settings::isOutputEscapingEnabled();
37+
}
38+
39+
/**
40+
* Executed after each method of the class
41+
*/
42+
public function tearDown()
43+
{
44+
Settings::setOutputEscapingEnabled($this->outputEscapingEnabled);
45+
TestHelperDOCX::clear();
46+
}
47+
48+
/**
49+
* Test chart elements
50+
*/
51+
public function testChartElements()
52+
{
53+
$phpWord = new PhpWord();
54+
$section = $phpWord->addSection();
55+
$style = array(
56+
'width' => 5000000,
57+
'height' => 5000000,
58+
'showAxisLabels' => true,
59+
'showGridX' => true,
60+
'showGridY' => true,
61+
'showLegend' => false,
62+
);
63+
64+
$chartTypes = array('pie', 'doughnut', 'bar', 'line', 'area', 'scatter', 'radar');
65+
$categories = array('A', 'B', 'C', 'D', 'E');
66+
$series1 = array(1, 3, 2, 5, 4);
67+
foreach ($chartTypes as $chartType) {
68+
$section->addChart($chartType, $categories, $series1, $style);
69+
}
70+
$colorArray = array('FFFFFF', '000000', 'FF0000', '00FF00', '0000FF');
71+
$numColor = count($colorArray);
72+
$chart = $section->addChart('pie', $categories, $series1, $style);
73+
$chart->getStyle()->setColors($colorArray)->setTitle('3d chart')->set3d(true);
74+
$chart = $section->addChart('stacked_bar', $categories, $series1, $style);
75+
$chart->getStyle()->setColors($colorArray)->setShowLegend(true);
76+
$chart = $section->addChart('scatter', $categories, $series1, $style);
77+
$chart->getStyle()->setMajorTickPosition('cross');
78+
$section->addChart('scatter', $categories, $series1, $style, 'seriesname');
79+
80+
$doc = TestHelperDOCX::getDocument($phpWord);
81+
82+
$index = 0;
83+
foreach ($chartTypes as $chartType) {
84+
++$index;
85+
$file = "word/charts/chart{$index}.xml";
86+
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart";
87+
self::assertTrue($doc->elementExists($path, $file), "chart type $chartType");
88+
}
89+
90+
$index = 11;
91+
$file = "word/charts/chart{$index}.xml";
92+
$doc->setDefaultFile($file);
93+
$chartType = 'scatter';
94+
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart";
95+
self::assertEquals('seriesname', $doc->getElement($path . '/c:ser/c:tx/c:strRef/c:strCache/c:pt/c:v')->nodeValue);
96+
97+
$index = 8;
98+
$file = "word/charts/chart{$index}.xml";
99+
$doc->setDefaultFile($file);
100+
$chartType = 'pie3D';
101+
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart";
102+
for ($idx = 0; $idx < $numColor; ++$idx) {
103+
$idxp1 = $idx + 1;
104+
$element = $path . "/c:ser/c:dPt[$idxp1]/c:spPr/a:solidFill/a:srgbClr";
105+
self::assertEquals($colorArray[$idx], $doc->getElementAttribute($element, 'val'), "pie3d chart idx=$idx");
106+
}
107+
108+
$index = 9;
109+
$file = "word/charts/chart{$index}.xml";
110+
$doc->setDefaultFile($file);
111+
$chartType = 'bar';
112+
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart";
113+
for ($idxp1 = 1; $idxp1 < $numColor; ++$idxp1) {
114+
$idx = $idxp1; // stacked bar chart is shifted
115+
$element = $path . "/c:ser/c:dPt[$idxp1]/c:spPr/a:solidFill/a:srgbClr";
116+
self::assertEquals($colorArray[$idx], $doc->getElementAttribute($element, 'val'), "bar chart idx=$idx");
117+
}
118+
}
119+
120+
public function testChartEscapingEnabled()
121+
{
122+
Settings::setOutputEscapingEnabled(true);
123+
$phpWord = new PhpWord();
124+
$section = $phpWord->addSection();
125+
$style = array(
126+
'width' => 5000000,
127+
'height' => 5000000,
128+
'showAxisLabels' => true,
129+
'showGridX' => true,
130+
'showGridY' => true,
131+
'showLegend' => false,
132+
'valueAxisTitle' => 'Values',
133+
);
134+
$categories = array('A&B', 'C<D>', 'E', 'F', 'G');
135+
$series1 = array(1, 3, 2, 5, 4);
136+
$section->addChart('bar', $categories, $series1, $style);
137+
$doc = TestHelperDOCX::getDocument($phpWord);
138+
139+
$index = 1;
140+
$file = "word/charts/chart{$index}.xml";
141+
$doc->setDefaultFile($file);
142+
$chartType = 'bar';
143+
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart/c:ser/c:cat/c:strLit";
144+
$element = "$path/c:pt[1]/c:v";
145+
self::assertEquals('A&B', $doc->getElement($element)->nodeValue);
146+
$element = "$path/c:pt[2]/c:v";
147+
self::assertEquals('C<D>', $doc->getElement($element)->nodeValue);
148+
}
149+
150+
public function testChartEscapingDisabled()
151+
{
152+
Settings::setOutputEscapingEnabled(false);
153+
$phpWord = new PhpWord();
154+
$section = $phpWord->addSection();
155+
$style = array(
156+
'width' => 5000000,
157+
'height' => 5000000,
158+
'showAxisLabels' => true,
159+
'showGridX' => true,
160+
'showGridY' => true,
161+
'showLegend' => false,
162+
'valueAxisTitle' => 'Values',
163+
);
164+
$categories = array('A&amp;B', 'C&lt;D&gt;', 'E', 'F', 'G');
165+
$series1 = array(1, 3, 2, 5, 4);
166+
$section->addChart('bar', $categories, $series1, $style);
167+
$doc = TestHelperDOCX::getDocument($phpWord);
168+
169+
$index = 1;
170+
$file = "word/charts/chart{$index}.xml";
171+
$doc->setDefaultFile($file);
172+
$chartType = 'bar';
173+
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart/c:ser/c:cat/c:strLit";
174+
$element = "$path/c:pt[1]/c:v";
175+
self::assertEquals('A&B', $doc->getElement($element)->nodeValue);
176+
$element = "$path/c:pt[2]/c:v";
177+
self::assertEquals('C<D>', $doc->getElement($element)->nodeValue);
178+
}
179+
180+
public function testValueAxisTitle()
181+
{
182+
$phpWord = new PhpWord();
183+
$section = $phpWord->addSection();
184+
$style = array(
185+
'width' => 5000000,
186+
'height' => 5000000,
187+
'showAxisLabels' => true,
188+
'showGridX' => true,
189+
'showGridY' => true,
190+
'showLegend' => false,
191+
'valueAxisTitle' => 'Values',
192+
);
193+
$chartType = 'line';
194+
$categories = array('A', 'B', 'C', 'D', 'E');
195+
$series1 = array(1, 3, 2, 5, 4);
196+
$section->addChart($chartType, $categories, $series1, $style);
197+
$doc = TestHelperDOCX::getDocument($phpWord);
198+
199+
$index = 1;
200+
$file = "word/charts/chart{$index}.xml";
201+
$doc->setDefaultFile($file);
202+
$chartType = 'line';
203+
$path = '/c:chartSpace/c:chart/c:plotArea';
204+
$element = "$path/c:{$chartType}Chart";
205+
self::assertTrue($doc->elementExists($path));
206+
$element = "$path/c:valAx";
207+
self::assertTrue($doc->elementExists($element));
208+
$element .= '/c:title/c:tx/c:rich/a:p/a:r/a:t';
209+
self::assertEquals('Values', $doc->getElement($element)->nodeValue);
210+
}
211+
212+
public function testNoAxisLabels()
213+
{
214+
$phpWord = new PhpWord();
215+
$section = $phpWord->addSection();
216+
$style = array(
217+
'width' => 5000000,
218+
'height' => 5000000,
219+
'showAxisLabels' => false,
220+
'showGridX' => true,
221+
'showGridY' => true,
222+
'showLegend' => false,
223+
'valueAxisTitle' => 'Values',
224+
);
225+
$chartType = 'line';
226+
$categories = array('A', 'B', 'C', 'D', 'E');
227+
$series1 = array(1, 3, 2, 5, 4);
228+
$section->addChart($chartType, $categories, $series1, $style);
229+
$doc = TestHelperDOCX::getDocument($phpWord);
230+
231+
$index = 1;
232+
$file = "word/charts/chart{$index}.xml";
233+
$doc->setDefaultFile($file);
234+
$chartType = 'line';
235+
$path = '/c:chartSpace/c:chart/c:plotArea';
236+
$element = "$path/c:{$chartType}Chart";
237+
$element = "$path/c:valAx";
238+
$element .= '/c:tickLblPos';
239+
self::assertEquals('none', $doc->getElementAttribute($element, 'val'));
240+
}
241+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\Word2007;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\TestHelperDOCX;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace
25+
*/
26+
class FormFieldTest extends \PHPUnit\Framework\TestCase
27+
{
28+
/**
29+
* Executed before each method of the class
30+
*/
31+
public function tearDown()
32+
{
33+
TestHelperDOCX::clear();
34+
}
35+
36+
/**
37+
* Test form fields
38+
*/
39+
public function testFormFieldElements()
40+
{
41+
$phpWord = new PhpWord();
42+
$section = $phpWord->addSection();
43+
44+
$section->addFormField('textinput')->setName('MyTextBox');
45+
$section->addFormField('checkbox')->setDefault(true)->setValue('Your name');
46+
$section->addFormField('checkbox')->setDefault(true);
47+
$section->addFormField('dropdown')->setEntries(array('Choice 1', 'Choice 2', 'Choice 3', ''));
48+
49+
$doc = TestHelperDOCX::getDocument($phpWord);
50+
51+
$path = '/w:document/w:body/w:p[1]/w:r/w:fldChar/w:ffData';
52+
$this->assertTrue($doc->elementExists("$path/w:textInput"));
53+
$this->assertEquals('MyTextBox', $doc->getElementAttribute("$path/w:name", 'w:val'));
54+
55+
$path = '/w:document/w:body/w:p[2]/w:r/w:fldChar/w:ffData';
56+
$this->assertTrue($doc->elementExists("$path/w:checkBox"));
57+
$path = '/w:document/w:body/w:p[2]/w:r[4]/w:t';
58+
$this->assertEquals('Your name', $doc->getElement($path)->textContent);
59+
60+
$path = '/w:document/w:body/w:p[3]/w:r/w:fldChar/w:ffData';
61+
$this->assertTrue($doc->elementExists("$path/w:checkBox"));
62+
63+
$path = '/w:document/w:body/w:p[4]/w:r/w:fldChar/w:ffData/w:ddList';
64+
$this->assertTrue($doc->elementExists($path));
65+
$this->assertEquals('Choice 1', $doc->getElementAttribute("$path/w:listEntry[1]", 'w:val'));
66+
$this->assertEquals('Choice 2', $doc->getElementAttribute("$path/w:listEntry[2]", 'w:val'));
67+
$this->assertEquals('Choice 3', $doc->getElementAttribute("$path/w:listEntry[3]", 'w:val'));
68+
$this->assertEquals('', trim($doc->getElementAttribute("$path/w:listEntry[4]", 'w:val'), ' '));
69+
}
70+
}

tests/PhpWord/Writer/Word2007/ElementTest.php

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -249,33 +249,7 @@ public function testShapeElements()
249249
}
250250
}
251251

252-
/**
253-
* Test shape elements
254-
*/
255-
public function testChartElements()
256-
{
257-
$phpWord = new PhpWord();
258-
$section = $phpWord->addSection();
259-
$style = array('width' => 1000000, 'height' => 1000000, 'showAxisLabels' => true, 'showGridX' => true, 'showGridY' => true);
260-
261-
$chartTypes = array('pie', 'doughnut', 'bar', 'line', 'area', 'scatter', 'radar');
262-
$categories = array('A', 'B', 'C', 'D', 'E');
263-
$series1 = array(1, 3, 2, 5, 4);
264-
foreach ($chartTypes as $chartType) {
265-
$section->addChart($chartType, $categories, $series1, $style);
266-
}
267-
$section->addChart('pie', $categories, $series1, array('3d' => true));
268-
269-
$doc = TestHelperDOCX::getDocument($phpWord);
270-
271-
$index = 0;
272-
foreach ($chartTypes as $chartType) {
273-
++$index;
274-
$file = "word/charts/chart{$index}.xml";
275-
$path = "/c:chartSpace/c:chart/c:plotArea/c:{$chartType}Chart";
276-
$this->assertTrue($doc->elementExists($path, $file));
277-
}
278-
}
252+
// testChartElements moved to Writer/Word2007/Element/ChartTest
279253

280254
public function testFieldElement()
281255
{
@@ -389,27 +363,7 @@ public function testMacroButtonField()
389363
$this->assertEquals(' MACROBUTTON Zoom100 double click to zoom ', $doc->getElement($element)->textContent);
390364
}
391365

392-
/**
393-
* Test form fields
394-
*/
395-
public function testFormFieldElements()
396-
{
397-
$phpWord = new PhpWord();
398-
$section = $phpWord->addSection();
399-
400-
$section->addFormField('textinput')->setName('MyTextBox');
401-
$section->addFormField('checkbox')->setDefault(true)->setValue('Your name');
402-
$section->addFormField('checkbox')->setDefault(true);
403-
$section->addFormField('dropdown')->setEntries(array('Choice 1', 'Choice 2', 'Choice 3'));
404-
405-
$doc = TestHelperDOCX::getDocument($phpWord);
406-
407-
$path = '/w:document/w:body/w:p[%d]/w:r/w:fldChar/w:ffData';
408-
$this->assertTrue($doc->elementExists(sprintf($path, 1) . '/w:textInput'));
409-
$this->assertTrue($doc->elementExists(sprintf($path, 2) . '/w:checkBox'));
410-
$this->assertTrue($doc->elementExists(sprintf($path, 3) . '/w:checkBox'));
411-
$this->assertTrue($doc->elementExists(sprintf($path, 4) . '/w:ddList'));
412-
}
366+
// testFormFieldElements moved to Writer/Word2007/Element/FormFieldTest
413367

414368
/**
415369
* Test SDT elements

0 commit comments

Comments
 (0)