Skip to content

Commit db1ce96

Browse files
committed
Merge pull request #131 from ivanlanin/develop
More unit tests for Template and Writer
2 parents d9c3cfc + 92a7a6b commit db1ce96

File tree

8 files changed

+214
-35
lines changed

8 files changed

+214
-35
lines changed

Classes/PHPWord/Reader/Abstract.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
/**
2929
* PHPWord_Reader_Abstract
30+
*
31+
* @codeCoverageIgnore Abstract class
3032
*/
3133
abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
3234
{

Classes/PHPWord/Writer/ODText.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public function save($pFilename = null)
174174
$objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
175175
}
176176
}
177+
// @codeCoverageIgnoreEnd
177178

178179
// Close file
179180
if ($objZip->close() === false) {

Classes/PHPWord/Writer/ODText/Manifest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function writeManifest(PHPWord $pPHPWord = null)
101101
$objWriter->endElement();
102102
}
103103
}
104+
// @codeCoverageIgnoreEnd
104105

105106
$objWriter->endElement();
106107

Tests/PHPWord/TemplateTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
namespace PHPWord\Tests;
33

44
use PHPWord_Template;
5+
use PHPWord;
56

67
/**
7-
* @coversDefaultClass PHPWord_Template
8+
* @coversDefaultClass PHPWord_Template
89
*/
910
final class TemplateTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -143,4 +144,32 @@ final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromT
143144
*/
144145
@$template->applyXslStyleSheet($xslDOMDocument);
145146
}
147+
148+
/**
149+
* @covers ::setValue
150+
* @covers ::getVariables
151+
* @covers ::cloneRow
152+
* @covers ::saveAs
153+
*/
154+
public function testCloneRow()
155+
{
156+
$template = \join(
157+
\DIRECTORY_SEPARATOR,
158+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-row.docx')
159+
);
160+
$expectedVar = array('tableHeader', 'userId', 'userName', 'userLocation');
161+
$docName = 'clone-test-result.docx';
162+
163+
$PHPWord = new PHPWord();
164+
$document = $PHPWord->loadTemplate($template);
165+
$actualVar = $document->getVariables();
166+
$document->cloneRow('userId', 1);
167+
$document->setValue('userId#1', 'Test');
168+
$document->saveAs($docName);
169+
$docFound = file_exists($docName);
170+
unlink($docName);
171+
172+
$this->assertEquals($expectedVar, $actualVar);
173+
$this->assertTrue($docFound);
174+
}
146175
}

Tests/PHPWord/Writer/ODTextTest.php

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
/**
88
* Class ODTextTest
99
*
10-
* @package PHPWord\Tests
10+
* @package PHPWord\Tests
11+
* @coversDefaultClass PHPWord_Writer_ODText
1112
* @runTestsInSeparateProcesses
1213
*/
1314
class ODTextTest extends \PHPUnit_Framework_TestCase
@@ -37,10 +38,9 @@ public function testConstruct()
3738
}
3839

3940
/**
40-
* Test construct with null value/without PHPWord
41-
*
42-
* @expectedException Exception
43-
* @expectedExceptionMessage No PHPWord assigned.
41+
* @covers ::getPHPWord
42+
* @expectedException Exception
43+
* @expectedExceptionMessage No PHPWord assigned.
4444
*/
4545
public function testConstructWithNull()
4646
{
@@ -49,39 +49,106 @@ public function testConstructWithNull()
4949
}
5050

5151
/**
52-
* Test save()
52+
* @covers ::save
5353
*/
5454
public function testSave()
5555
{
56+
$imageSrc = \join(
57+
\DIRECTORY_SEPARATOR,
58+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
59+
);
60+
$objectSrc = \join(
61+
\DIRECTORY_SEPARATOR,
62+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
63+
);
64+
$file = \join(
65+
\DIRECTORY_SEPARATOR,
66+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt')
67+
);
68+
5669
$phpWord = new PHPWord();
5770
$phpWord->addFontStyle('Font', array('size' => 11));
5871
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
5972
$section = $phpWord->createSection();
60-
$section->addText('Test 1', 'Font', 'Paragraph');
73+
$section->addText('Test 1', 'Font');
6174
$section->addTextBreak();
62-
$section->addText('Test 2');
75+
$section->addText('Test 2', null, 'Paragraph');
76+
$section->addLink('http://test.com');
77+
$section->addTitle('Test', 1);
78+
$section->addPageBreak();
79+
$section->addTable();
80+
$section->addListItem('Test');
81+
$section->addImage($imageSrc);
82+
$section->addObject($objectSrc);
83+
$section->addTOC();
6384
$section = $phpWord->createSection();
6485
$textrun = $section->createTextRun();
6586
$textrun->addText('Test 3');
66-
6787
$writer = new PHPWord_Writer_ODText($phpWord);
68-
$file = join(
69-
DIRECTORY_SEPARATOR,
70-
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt')
71-
);
7288
$writer->save($file);
89+
7390
$this->assertTrue(file_exists($file));
91+
7492
unlink($file);
7593
}
7694

7795
/**
78-
* Test disk caching parameters
96+
* @covers ::save
97+
* @todo Haven't got any method to test this
98+
*/
99+
public function testSavePhpOutput()
100+
{
101+
$phpWord = new PHPWord();
102+
$section = $phpWord->createSection();
103+
$section->addText('Test');
104+
$writer = new PHPWord_Writer_ODText($phpWord);
105+
$writer->save('php://output');
106+
}
107+
108+
/**
109+
* @covers ::save
110+
* @expectedException Exception
111+
* @expectedExceptionMessage PHPWord object unassigned.
112+
*/
113+
public function testSaveException()
114+
{
115+
$writer = new PHPWord_Writer_ODText();
116+
$writer->save();
117+
}
118+
119+
/**
120+
* @covers ::getWriterPart
79121
*/
80-
public function testSetDiskCaching()
122+
public function testGetWriterPartNull()
123+
{
124+
$object = new PHPWord_Writer_ODText();
125+
$this->assertNull($object->getWriterPart('foo'));
126+
}
127+
128+
/**
129+
* @covers ::setUseDiskCaching
130+
* @covers ::getUseDiskCaching
131+
*/
132+
public function testSetGetUseDiskCaching()
81133
{
82134
$object = new PHPWord_Writer_ODText();
83135
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
84136
$this->assertTrue($object->getUseDiskCaching());
85137
$this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory());
86138
}
139+
140+
/**
141+
* @covers ::setUseDiskCaching
142+
* @expectedException Exception
143+
*/
144+
public function testSetUseDiskCachingException()
145+
{
146+
$dir = \join(
147+
\DIRECTORY_SEPARATOR,
148+
array(\PHPWORD_TESTS_DIR_ROOT, 'foo')
149+
);
150+
151+
$object = new PHPWord_Writer_ODText($phpWord);
152+
$object->setUseDiskCaching(true, $dir);
153+
}
87154
}

Tests/PHPWord/Writer/RTFTest.php

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
/**
88
* Class RTFTest
9-
* @package PHPWord\Tests
9+
*
10+
* @package PHPWord\Tests
11+
* @coversDefaultClass PHPWord_Writer_RTF
1012
* @runTestsInSeparateProcesses
1113
*/
1214
class RTFTest extends \PHPUnit_Framework_TestCase
1315
{
1416
/**
15-
* Test construct
17+
* covers ::construct
1618
*/
1719
public function testConstruct()
1820
{
@@ -23,10 +25,9 @@ public function testConstruct()
2325
}
2426

2527
/**
26-
* Test construct with null value/without PHPWord
27-
*
28-
* @expectedException Exception
29-
* @expectedExceptionMessage No PHPWord assigned.
28+
* covers ::__construct
29+
* @expectedException Exception
30+
* @expectedExceptionMessage No PHPWord assigned.
3031
*/
3132
public function testConstructWithNull()
3233
{
@@ -35,28 +36,72 @@ public function testConstructWithNull()
3536
}
3637

3738
/**
38-
* Test save()
39+
* @covers ::save
40+
* @todo Haven't got any method to test this
41+
*/
42+
public function testSavePhpOutput()
43+
{
44+
$phpWord = new PHPWord();
45+
$section = $phpWord->createSection();
46+
$section->addText('Test');
47+
$writer = new PHPWord_Writer_RTF($phpWord);
48+
$writer->save('php://output');
49+
}
50+
51+
/**
52+
* @covers ::save
53+
* @expectedException Exception
54+
* @expectedExceptionMessage PHPWord object unassigned.
55+
*/
56+
public function testSaveException()
57+
{
58+
$writer = new PHPWord_Writer_RTF();
59+
$writer->save();
60+
}
61+
62+
/**
63+
* @covers ::save
64+
* @covers ::<private>
3965
*/
4066
public function testSave()
4167
{
68+
$imageSrc = \join(
69+
\DIRECTORY_SEPARATOR,
70+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
71+
);
72+
$objectSrc = \join(
73+
\DIRECTORY_SEPARATOR,
74+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'sheet.xls')
75+
);
76+
$file = \join(
77+
\DIRECTORY_SEPARATOR,
78+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
79+
);
80+
4281
$phpWord = new PHPWord();
4382
$phpWord->addFontStyle('Font', array('size' => 11));
4483
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
4584
$section = $phpWord->createSection();
46-
$section->addText('Test 1', 'Font', 'Paragraph');
85+
$section->addText('Test 1', 'Font');
4786
$section->addTextBreak();
48-
$section->addText('Test 2');
87+
$section->addText('Test 2', null, 'Paragraph');
88+
$section->addLink('http://test.com');
89+
$section->addTitle('Test', 1);
90+
$section->addPageBreak();
91+
$section->addTable();
92+
$section->addListItem('Test');
93+
$section->addImage($imageSrc);
94+
$section->addObject($objectSrc);
95+
$section->addTOC();
4996
$section = $phpWord->createSection();
5097
$textrun = $section->createTextRun();
5198
$textrun->addText('Test 3');
52-
99+
$textrun->addTextBreak();
53100
$writer = new PHPWord_Writer_RTF($phpWord);
54-
$file = join(
55-
DIRECTORY_SEPARATOR,
56-
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
57-
);
58101
$writer->save($file);
102+
59103
$this->assertTrue(file_exists($file));
104+
60105
unlink($file);
61106
}
62107
}

0 commit comments

Comments
 (0)