Skip to content

Commit 8f54fa4

Browse files
committed
Simple unit tests for ODText, RTF, and Word2007 Writer
1 parent 92cfa12 commit 8f54fa4

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

Tests/PHPWord/Writer/ODTextTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Writer_ODText;
6+
use PHPWord;
7+
8+
/**
9+
* Class ODTextTest
10+
*
11+
* @package PHPWord\Tests
12+
* @runTestsInSeparateProcesses
13+
*/
14+
class ODTextTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* Test construct
18+
*/
19+
public function testConstruct()
20+
{
21+
$object = new PHPWord_Writer_ODText(new PHPWord());
22+
23+
$this->assertInstanceOf('PHPWord', $object->getPHPWord());
24+
$this->assertInstanceOf("PHPWord_HashTable", $object->getDrawingHashTable());
25+
26+
$this->assertEquals('./', $object->getDiskCachingDirectory());
27+
$writerParts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles');
28+
foreach ($writerParts as $part) {
29+
$this->assertInstanceOf(
30+
"PHPWord_Writer_ODText_{$part}",
31+
$object->getWriterPart($part)
32+
);
33+
$this->assertInstanceOf(
34+
"PHPWord_Writer_ODText",
35+
$object->getWriterPart($part)->getParentWriter()
36+
);
37+
}
38+
}
39+
40+
/**
41+
* Test construct with null value/without PHPWord
42+
*
43+
* @expectedException Exception
44+
* @expectedExceptionMessage No PHPWord assigned.
45+
*/
46+
public function testConstructWithNull()
47+
{
48+
$object = new PHPWord_Writer_ODText();
49+
$object->getPHPWord();
50+
}
51+
52+
/**
53+
* Test save()
54+
*/
55+
public function testSave()
56+
{
57+
$phpWord = new PHPWord();
58+
$phpWord->addFontStyle('Font', array('size' => 11));
59+
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
60+
$section = $phpWord->createSection();
61+
$section->addText('Test 1', 'Font', 'Paragraph');
62+
$section->addTextBreak();
63+
$section->addText('Test 2');
64+
$section = $phpWord->createSection();
65+
$textrun = $section->createTextRun();
66+
$textrun->addText('Test 3');
67+
68+
$writer = new PHPWord_Writer_ODText($phpWord);
69+
$file = join(
70+
DIRECTORY_SEPARATOR,
71+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt')
72+
);
73+
$writer->save($file);
74+
$this->assertTrue(file_exists($file));
75+
unlink($file);
76+
}
77+
78+
/**
79+
* Test disk caching parameters
80+
*/
81+
public function testSetDiskCaching()
82+
{
83+
$object = new PHPWord_Writer_ODText();
84+
$object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT);
85+
$this->assertTrue($object->getUseDiskCaching());
86+
$this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory());
87+
}
88+
}

Tests/PHPWord/Writer/RTFTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Writer_RTF;
6+
use PHPWord;
7+
8+
/**
9+
* Class RTFTest
10+
* @package PHPWord\Tests
11+
* @runTestsInSeparateProcesses
12+
*/
13+
class RTFTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* Test construct
17+
*/
18+
public function testConstruct()
19+
{
20+
$object = new PHPWord_Writer_RTF(new PHPWord);
21+
22+
$this->assertInstanceOf('PHPWord', $object->getPHPWord());
23+
$this->assertInstanceOf("PHPWord_HashTable", $object->getDrawingHashTable());
24+
}
25+
26+
/**
27+
* Test construct with null value/without PHPWord
28+
*
29+
* @expectedException Exception
30+
* @expectedExceptionMessage No PHPWord assigned.
31+
*/
32+
public function testConstructWithNull()
33+
{
34+
$object = new PHPWord_Writer_RTF();
35+
$object->getPHPWord();
36+
}
37+
38+
/**
39+
* Test save()
40+
*/
41+
public function testSave()
42+
{
43+
$phpWord = new PHPWord();
44+
$phpWord->addFontStyle('Font', array('size' => 11));
45+
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
46+
$section = $phpWord->createSection();
47+
$section->addText('Test 1', 'Font', 'Paragraph');
48+
$section->addTextBreak();
49+
$section->addText('Test 2');
50+
$section = $phpWord->createSection();
51+
$textrun = $section->createTextRun();
52+
$textrun->addText('Test 3');
53+
54+
$writer = new PHPWord_Writer_RTF($phpWord);
55+
$file = join(
56+
DIRECTORY_SEPARATOR,
57+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf')
58+
);
59+
$writer->save($file);
60+
$this->assertTrue(file_exists($file));
61+
unlink($file);
62+
}
63+
}

Tests/PHPWord/Writer/Word2007Test.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace PHPWord\Tests\Writer;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord_Writer_Word2007;
6+
use PHPWord;
7+
8+
/**
9+
* Class Word2007Test
10+
*
11+
* @package PHPWord\Tests
12+
* @runTestsInSeparateProcesses
13+
*/
14+
class Word2007Test extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* Test construct
18+
*/
19+
public function testConstruct()
20+
{
21+
$object = new PHPWord_Writer_Word2007(new PHPWord());
22+
23+
$writerParts = array('ContentTypes', 'Rels', 'DocProps',
24+
'DocumentRels', 'Document', 'Styles', 'Header', 'Footer',
25+
'Footnotes', 'FootnotesRels');
26+
foreach ($writerParts as $part) {
27+
$this->assertInstanceOf(
28+
"PHPWord_Writer_Word2007_{$part}",
29+
$object->getWriterPart($part)
30+
);
31+
$this->assertInstanceOf(
32+
"PHPWord_Writer_Word2007",
33+
$object->getWriterPart($part)->getParentWriter()
34+
);
35+
}
36+
}
37+
38+
/**
39+
* Test save()
40+
*/
41+
public function testSave()
42+
{
43+
$phpWord = new PHPWord();
44+
$phpWord->addFontStyle('Font', array('size' => 11));
45+
$phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
46+
$section = $phpWord->createSection();
47+
$section->addText('Test 1', 'Font', 'Paragraph');
48+
$section->addTextBreak();
49+
$section->addText('Test 2');
50+
$section = $phpWord->createSection();
51+
$textrun = $section->createTextRun();
52+
$textrun->addText('Test 3');
53+
54+
$writer = new PHPWord_Writer_Word2007($phpWord);
55+
$file = join(
56+
DIRECTORY_SEPARATOR,
57+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx')
58+
);
59+
$writer->save($file);
60+
$this->assertTrue(file_exists($file));
61+
unlink($file);
62+
}
63+
}

0 commit comments

Comments
 (0)