Skip to content

Commit 5bcd114

Browse files
committed
Merge pull request #138 from ivanlanin/develop
Additional unit tests
2 parents 28fef5f + 18c1e5f commit 5bcd114

File tree

13 files changed

+252
-38
lines changed

13 files changed

+252
-38
lines changed

Classes/PHPWord/Writer/ODText/Content.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart
3333
/**
3434
* Write content file to XML format
3535
*
36-
* @param PHPWord $pPHPWord
37-
* @return string XML Output
38-
* @throws Exception
36+
* @param PHPWord $pPHPWord
37+
* @return string XML Output
38+
* @throws Exception
3939
*/
4040
public function writeContent(PHPWord $pPHPWord = null)
4141
{

Classes/PHPWord/Writer/ODText/Manifest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ public function writeManifest(PHPWord $pPHPWord = null)
113113
/**
114114
* Get image mime type
115115
*
116-
* @param string $pFile Filename
117-
* @return string Mime Type
118-
* @throws Exception
116+
* @param string $pFile Filename
117+
* @return string Mime Type
118+
* @throws Exception
119+
* @codeCoverageIgnore Image is not yet handled by ODText
119120
*/
120121
private function _getImageMimeType($pFile = '')
121122
{

Classes/PHPWord/Writer/Word2007.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,14 @@ private function _addFileToPackage($objZip, $element)
289289
$this->checkContentTypes($element['source']);
290290
}
291291
}
292+
293+
/**
294+
* Get disk caching directory
295+
*
296+
* @return string
297+
*/
298+
public function getDiskCachingDirectory()
299+
{
300+
return $this->_diskCachingDirectory;
301+
}
292302
}

Tests/PHPWord/TemplateTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,43 @@ final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromT
145145
@$template->applyXslStyleSheet($xslDOMDocument);
146146
}
147147

148+
/**
149+
* @covers PHPWord_Template
150+
*/
151+
public function testConstruct()
152+
{
153+
$template = \join(
154+
\DIRECTORY_SEPARATOR,
155+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-row.docx')
156+
);
157+
$expectedVar = array('tableHeader', 'userId', 'userName');
158+
$document = new PHPWord_Template($template);
159+
$actualVar = $document->getVariables();
160+
$document->cloneRow('userId', 9);
161+
$document->setValue('userId#1', utf8_decode('ééé'));
162+
$document->setValue('userId#2', 'a');
163+
$document->setValue('userId#3', 'a');
164+
$document->setValue('userId#3', 'a');
165+
$document->setValue('userId#4', 'a');
166+
$document->setValue('userId#5', 'a');
167+
$document->setValue('userId#6', 'a');
168+
$document->setValue('userId#7', 'a');
169+
$document->setValue('userId#8', 'a');
170+
$document->cloneRow('userId#9', 'a');
171+
$this->assertEquals($expectedVar, $actualVar);
172+
}
173+
148174
/**
149175
* @covers ::setValue
150176
* @covers ::getVariables
151177
* @covers ::cloneRow
152178
* @covers ::saveAs
153179
*/
154-
public function testCloneRow()
180+
public function testCloneMergedRow()
155181
{
156182
$template = \join(
157183
\DIRECTORY_SEPARATOR,
158-
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-row.docx')
184+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'clone-merge.docx')
159185
);
160186
$expectedVar = array('tableHeader', 'userId', 'userName', 'userLocation');
161187
$docName = 'clone-test-result.docx';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer\ODText;
3+
4+
use PHPWord_Writer_ODText_WriterPart;
5+
use PHPWord_Writer_ODText;
6+
use PHPWord\Tests\TestHelperDOCX;
7+
8+
/**
9+
* Class WriterPartTest
10+
*
11+
* @package PHPWord\Tests
12+
* @coversDefaultClass PHPWord_Writer_ODText_WriterPart
13+
* @runTestsInSeparateProcesses
14+
*/
15+
class WriterPartTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* covers ::setParentWriter
19+
* covers ::getParentWriter
20+
*/
21+
public function testSetGetParentWriter()
22+
{
23+
$object = $this->getMockForAbstractClass(
24+
'PHPWord_Writer_Word2007_WriterPart'
25+
);
26+
$object->setParentWriter(new PHPWord_Writer_ODText());
27+
$this->assertEquals(
28+
new PHPWord_Writer_ODText(),
29+
$object->getParentWriter()
30+
);
31+
}
32+
33+
/**
34+
* covers ::getParentWriter
35+
* @expectedException Exception
36+
* @expectedExceptionMessage No parent PHPWord_Writer_IWriter assigned.
37+
*/
38+
public function testSetGetParentWriterNull()
39+
{
40+
$object = $this->getMockForAbstractClass(
41+
'PHPWord_Writer_Word2007_WriterPart'
42+
);
43+
$object->getParentWriter();
44+
}
45+
}

Tests/PHPWord/Writer/ODTextTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,6 @@ public function testSave()
9292
unlink($file);
9393
}
9494

95-
/**
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-
10895
/**
10996
* @covers ::save
11097
* @expectedException Exception

Tests/PHPWord/Writer/RTFTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,6 @@ public function testConstructWithNull()
3535
$object->getPHPWord();
3636
}
3737

38-
/**
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-
5138
/**
5239
* @covers ::save
5340
* @expectedException Exception
@@ -78,13 +65,15 @@ public function testSave()
7865
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
7966
);
8067

68+
$fontStyle = array('name' => 'Verdana', 'size' => 11, 'bold' => true, 'italic' => true, 'color' => 'F29101', 'fgColor' => '123456');
69+
$paragraphStyle = array('align' => 'center', 'spaceAfter' => 120);
8170
$phpWord = new PHPWord();
82-
$phpWord->addFontStyle('Font', array('size' => 11));
83-
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
71+
$phpWord->addFontStyle('Font', $fontStyle);
72+
$phpWord->addParagraphStyle('Paragraph', $paragraphStyle);
8473
$section = $phpWord->createSection();
8574
$section->addText('Test 1', 'Font');
75+
$section->addText('Test 2', array('name' => 'Tahoma'), 'Paragraph');
8676
$section->addTextBreak();
87-
$section->addText('Test 2', null, 'Paragraph');
8877
$section->addLink('http://test.com');
8978
$section->addTitle('Test', 1);
9079
$section->addPageBreak();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer\Word2007;
3+
4+
use PHPWord_Writer_Word2007_Footer;
5+
use PHPWord_Writer_Word2007;
6+
use PHPWord_Section_Footer;
7+
use PHPWord\Tests\TestHelperDOCX;
8+
9+
/**
10+
* Class FooterTest
11+
*
12+
* @package PHPWord\Tests
13+
* @coversDefaultClass PHPWord_Writer_Word2007_Footer
14+
* @runTestsInSeparateProcesses
15+
*/
16+
class FooterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @covers ::writeFooter
20+
*/
21+
public function testWriteFooter()
22+
{
23+
$imageSrc = \join(
24+
\DIRECTORY_SEPARATOR,
25+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
26+
);
27+
$container = new PHPWord_Section_Footer(1);
28+
$container->addText('');
29+
$container->addPreserveText('');
30+
$container->addTextBreak();
31+
$container->createTextRun();
32+
$container->addTable()->addRow()->addCell()->addText('');
33+
$container->addImage($imageSrc);
34+
35+
$writer = new PHPWord_Writer_Word2007();
36+
$object = new PHPWord_Writer_Word2007_Footer();
37+
$object->setParentWriter($writer);
38+
$object->writeFooter($container);
39+
$writer->setUseDiskCaching(true);
40+
$xml = simplexml_load_string($object->writeFooter($container));
41+
42+
$this->assertInstanceOf('SimpleXMLElement', $xml);
43+
}
44+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer\Word2007;
3+
4+
use PHPWord_Writer_Word2007_Header;
5+
use PHPWord_Writer_Word2007;
6+
use PHPWord_Section_Header;
7+
use PHPWord\Tests\TestHelperDOCX;
8+
9+
/**
10+
* Class HeaderTest
11+
*
12+
* @package PHPWord\Tests
13+
* @coversDefaultClass PHPWord_Writer_Word2007_Header
14+
* @runTestsInSeparateProcesses
15+
*/
16+
class HeaderTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @covers ::writeHeader
20+
*/
21+
public function testWriteHeader()
22+
{
23+
$imageSrc = \join(
24+
\DIRECTORY_SEPARATOR,
25+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'PHPWord.png')
26+
);
27+
28+
$container = new PHPWord_Section_Header(1);
29+
$container->addText('Test');
30+
$container->addPreserveText('');
31+
$container->addTextBreak();
32+
$container->createTextRun();
33+
$container->addTable()->addRow()->addCell()->addText('');
34+
$container->addImage($imageSrc);
35+
$container->addWatermark($imageSrc);
36+
37+
$writer = new PHPWord_Writer_Word2007();
38+
$object = new PHPWord_Writer_Word2007_Header();
39+
$object->setParentWriter($writer);
40+
$object->writeHeader($container);
41+
$writer->setUseDiskCaching(true);
42+
$xml = simplexml_load_string($object->writeHeader($container));
43+
44+
$this->assertInstanceOf('SimpleXMLElement', $xml);
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer\Word2007;
3+
4+
use PHPWord_Writer_Word2007_WriterPart;
5+
use PHPWord_Writer_Word2007;
6+
use PHPWord\Tests\TestHelperDOCX;
7+
8+
/**
9+
* Class WriterPartTest
10+
*
11+
* @package PHPWord\Tests
12+
* @coversDefaultClass PHPWord_Writer_Word2007_WriterPart
13+
* @runTestsInSeparateProcesses
14+
*/
15+
class WriterPartTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* covers ::setParentWriter
19+
* covers ::getParentWriter
20+
*/
21+
public function testSetGetParentWriter()
22+
{
23+
$object = $this->getMockForAbstractClass(
24+
'PHPWord_Writer_Word2007_WriterPart'
25+
);
26+
$object->setParentWriter(new PHPWord_Writer_Word2007());
27+
$this->assertEquals(
28+
new PHPWord_Writer_Word2007(),
29+
$object->getParentWriter()
30+
);
31+
}
32+
33+
/**
34+
* covers ::getParentWriter
35+
* @expectedException Exception
36+
* @expectedExceptionMessage No parent PHPWord_Writer_IWriter assigned.
37+
*/
38+
public function testSetGetParentWriterNull()
39+
{
40+
$object = $this->getMockForAbstractClass(
41+
'PHPWord_Writer_Word2007_WriterPart'
42+
);
43+
$object->getParentWriter();
44+
}
45+
}

Tests/PHPWord/Writer/Word2007Test.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public function testSave()
6767
unlink($file);
6868
}
6969

70+
/**
71+
* @covers ::save
72+
* @expectedException Exception
73+
* @expectedExceptionMessage PHPWord object unassigned.
74+
*/
75+
public function testSaveException()
76+
{
77+
$writer = new PHPWord_Writer_Word2007();
78+
$writer->save();
79+
}
80+
81+
/**
82+
* @covers ::getWriterPart
83+
*/
84+
public function testGetWriterPartNull()
85+
{
86+
$object = new PHPWord_Writer_Word2007();
87+
$this->assertNull($object->getWriterPart('foo'));
88+
}
89+
7090
/**
7191
* @covers ::checkContentTypes
7292
*/
@@ -100,13 +120,14 @@ public function testCheckContentTypes()
100120
/**
101121
* @covers ::setUseDiskCaching
102122
* @covers ::getUseDiskCaching
123+
* @covers ::getDiskCachingDirectory
103124
*/
104125
public function testSetGetUseDiskCaching()
105126
{
106127
$object = new PHPWord_Writer_Word2007();
107128
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
108-
109129
$this->assertTrue($object->getUseDiskCaching());
130+
$this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory());
110131
}
111132

112133
/**
14.3 KB
Binary file not shown.

Tests/_files/templates/clone-row.docx

-12 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)