Skip to content

Commit 9c0e70a

Browse files
committed
Merge pull request #112 from ivanlanin/develop
Unit test for PHPWord main class
2 parents b58d1c8 + b1ee8a3 commit 9c0e70a

File tree

2 files changed

+209
-24
lines changed

2 files changed

+209
-24
lines changed

Classes/PHPWord.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
*/
2727

2828
/** PHPWORD_BASE_PATH */
29+
// @codeCoverageIgnoreStart
2930
if (!defined('PHPWORD_BASE_PATH')) {
3031
define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/');
3132
require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
3233
PHPWord_Autoloader::Register();
3334
}
34-
35+
// @codeCoverageIgnoreEnd
3536

3637
/**
3738
* PHPWord
@@ -151,7 +152,7 @@ public function setDefaultFontName($pValue)
151152
}
152153

153154
/**
154-
* Get default Font size
155+
* Get default Font size (in points)
155156
* @return string
156157
*/
157158
public function getDefaultFontSize()
@@ -160,15 +161,24 @@ public function getDefaultFontSize()
160161
}
161162

162163
/**
163-
* Set default Font size
164+
* Set default Font size (in points)
164165
* @param int $pValue
165166
*/
166167
public function setDefaultFontSize($pValue)
167168
{
168-
$pValue = $pValue * 2;
169169
$this->_defaultFontSize = $pValue;
170170
}
171171

172+
/**
173+
* Set default paragraph style definition to styles.xml
174+
*
175+
* @param array $styles Paragraph style definition
176+
*/
177+
public function setDefaultParagraphStyle($styles)
178+
{
179+
PHPWord_Style::setDefaultParagraphStyle($styles);
180+
}
181+
172182
/**
173183
* Adds a paragraph style definition to styles.xml
174184
*
@@ -213,16 +223,6 @@ public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null)
213223
PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
214224
}
215225

216-
/**
217-
* Set default paragraph style definition to styles.xml
218-
*
219-
* @param array $styles Paragraph style definition
220-
*/
221-
public function setDefaultParagraphStyle($styles)
222-
{
223-
PHPWord_Style::setDefaultParagraphStyle($styles);
224-
}
225-
226226
/**
227227
* Adds a hyperlink style to styles.xml
228228
*
@@ -243,15 +243,6 @@ public function getSections()
243243
return $this->_sectionCollection;
244244
}
245245

246-
/**
247-
* Get section count
248-
* @return int
249-
*/
250-
private function _countSections()
251-
{
252-
return count($this->_sectionCollection);
253-
}
254-
255246
/**
256247
* Load a Template File
257248
*
@@ -264,7 +255,18 @@ public function loadTemplate($strFilename)
264255
$template = new PHPWord_Template($strFilename);
265256
return $template;
266257
} else {
267-
trigger_error('Template file ' . $strFilename . ' not found.', E_USER_ERROR);
258+
throw new PHPWord_Exception(
259+
"Template file {$strFilename} not found."
260+
);
268261
}
269262
}
263+
264+
/**
265+
* Get section count
266+
* @return int
267+
*/
268+
private function _countSections()
269+
{
270+
return count($this->_sectionCollection);
271+
}
270272
}

Tests/PHPWordTest.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
namespace PHPWord\Tests;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
use PHPWord_DocumentProperties;
7+
use PHPWord_Section;
8+
use PHPWord_Style;
9+
10+
/**
11+
* @covers PHPWord
12+
*/
13+
class PHPWordTest extends PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var PHPWord
17+
*/
18+
protected $object;
19+
20+
/**
21+
* @covers PHPWord::__construct
22+
* @covers PHPWord::getProperties
23+
* @covers PHPWord::getDefaultFontName
24+
* @covers PHPWord::getDefaultFontSize
25+
*/
26+
public function testConstruct()
27+
{
28+
$object = new PHPWord();
29+
$this->assertEquals(
30+
new PHPWord_DocumentProperties(),
31+
$object->getProperties()
32+
);
33+
$this->assertEquals(
34+
PHPWord::DEFAULT_FONT_NAME,
35+
$object->getDefaultFontName()
36+
);
37+
$this->assertEquals(
38+
PHPWord::DEFAULT_FONT_SIZE,
39+
$object->getDefaultFontSize()
40+
);
41+
}
42+
43+
/**
44+
* @covers PHPWord::setProperties
45+
* @covers PHPWord::getProperties
46+
*/
47+
public function testSetGetProperties()
48+
{
49+
$object = new PHPWord();
50+
$creator = 'PHPWord';
51+
$properties = $object->getProperties();
52+
$properties->setCreator($creator);
53+
$object->setProperties($properties);
54+
$this->assertEquals($creator, $object->getProperties()->getCreator());
55+
}
56+
57+
/**
58+
* @covers PHPWord::createSection
59+
* @covers PHPWord::getSections
60+
*/
61+
public function testCreateGetSections()
62+
{
63+
$object = new PHPWord();
64+
$this->assertEquals(new PHPWord_Section(1), $object->createSection());
65+
$object->createSection();
66+
$this->assertEquals(2, count($object->getSections()));
67+
}
68+
69+
/**
70+
* @covers PHPWord::setDefaultFontName
71+
* @covers PHPWord::getDefaultFontName
72+
*/
73+
public function testSetGetDefaultFontName()
74+
{
75+
$object = new PHPWord();
76+
$fontName = 'Times New Roman';
77+
$this->assertEquals(
78+
PHPWord::DEFAULT_FONT_NAME,
79+
$object->getDefaultFontName()
80+
);
81+
$object->setDefaultFontName($fontName);
82+
$this->assertEquals($fontName, $object->getDefaultFontName());
83+
}
84+
85+
/**
86+
* @covers PHPWord::setDefaultFontSize
87+
* @covers PHPWord::getDefaultFontSize
88+
*/
89+
public function testSetGetDefaultFontSize()
90+
{
91+
$object = new PHPWord();
92+
$fontSize = 16;
93+
$this->assertEquals(
94+
PHPWord::DEFAULT_FONT_SIZE,
95+
$object->getDefaultFontSize()
96+
);
97+
$object->setDefaultFontSize($fontSize);
98+
$this->assertEquals($fontSize, $object->getDefaultFontSize());
99+
}
100+
101+
/**
102+
* @covers PHPWord::setDefaultParagraphStyle
103+
* @covers PHPWord::loadTemplate
104+
*/
105+
public function testSetDefaultParagraphStyle()
106+
{
107+
$object = new PHPWord();
108+
$object->setDefaultParagraphStyle(array());
109+
$this->assertInstanceOf(
110+
'PHPWord_Style_Paragraph',
111+
PHPWord_Style::getStyle('Normal')
112+
);
113+
}
114+
115+
/**
116+
* @covers PHPWord::addParagraphStyle
117+
* @covers PHPWord::addFontStyle
118+
* @covers PHPWord::addTableStyle
119+
* @covers PHPWord::addLinkStyle
120+
*/
121+
public function testAddStyles()
122+
{
123+
$object = new PHPWord();
124+
$styles = array('Paragraph' => 'Paragraph', 'Font' => 'Font',
125+
'Table' => 'TableFull', 'Link' => 'Font');
126+
foreach ($styles as $key => $value) {
127+
$method = "add{$key}Style";
128+
$styleId = "{$key} Style";
129+
$styleType = "PHPWord_Style_{$value}";
130+
$object->$method($styleId, array());
131+
$this->assertInstanceOf(
132+
$styleType,
133+
PHPWord_Style::getStyle($styleId)
134+
);
135+
}
136+
137+
}
138+
139+
/**
140+
* @covers PHPWord::addTitleStyle
141+
*/
142+
public function testAddTitleStyle()
143+
{
144+
$object = new PHPWord();
145+
$titleLevel = 1;
146+
$titleName = "Heading_{$titleLevel}";
147+
$object->addTitleStyle($titleLevel, array());
148+
$this->assertInstanceOf(
149+
'PHPWord_Style_Font',
150+
PHPWord_Style::getStyle($titleName)
151+
);
152+
}
153+
154+
/**
155+
* @covers PHPWord::loadTemplate
156+
*/
157+
public function testLoadTemplate()
158+
{
159+
$file = join(
160+
DIRECTORY_SEPARATOR,
161+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')
162+
);
163+
$object = new PHPWord();
164+
$this->assertInstanceOf(
165+
'PHPWord_Template',
166+
$object->loadTemplate($file)
167+
);
168+
}
169+
170+
/**
171+
* @covers PHPWord::loadTemplate
172+
* @expectedException PHPWord_Exception
173+
*/
174+
public function testLoadTemplateException()
175+
{
176+
$file = join(
177+
DIRECTORY_SEPARATOR,
178+
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blanks.docx')
179+
);
180+
$object = new PHPWord();
181+
$object->loadTemplate($file);
182+
}
183+
}

0 commit comments

Comments
 (0)