Skip to content

Commit 9bb5655

Browse files
committed
Unit test for AbstractStyle
1 parent 47956b0 commit 9bb5655

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

src/PhpWord/Style/AbstractStyle.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,12 @@ protected function setBoolVal($value, $default = null)
105105
/**
106106
* Set integer value
107107
*
108-
* @param integer|null $value
108+
* @param mixed $value
109109
* @param integer|null $default
110110
* @return integer|null
111111
*/
112112
protected function setIntVal($value, $default = null)
113113
{
114-
$value = intval($value);
115114
if (!is_int($value)) {
116115
$value = $default;
117116
}
@@ -128,7 +127,6 @@ protected function setIntVal($value, $default = null)
128127
*/
129128
protected function setFloatVal($value, $default = null)
130129
{
131-
$value = floatval($value);
132130
if (!is_float($value)) {
133131
$value = $default;
134132
}

tests/PhpWord/Tests/Element/AbstractElementTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace PhpOffice\PhpWord\Tests\Element;
1111

1212
/**
13-
* Test class for PhpOffice\PhpWord\Element\Cell
13+
* Test class for PhpOffice\PhpWord\Element\AbstractElement
1414
*
1515
* @runTestsInSeparateProcesses
1616
*/
@@ -26,6 +26,7 @@ public function testElementIndex()
2626
$stub->setElementIndex($ival);
2727
$this->assertEquals($stub->getElementIndex(), $ival);
2828
}
29+
2930
/**
3031
* Test set/get element unique Id
3132
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Tests\Style;
11+
12+
/**
13+
* Test class for PhpOffice\PhpWord\Style\AbstractStyle
14+
*
15+
* @runTestsInSeparateProcesses
16+
*/
17+
class AbstractStyleTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* Test set style by array
21+
*/
22+
public function testSetStyleByArray()
23+
{
24+
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
25+
$stub->setStyleByArray(array('index' => 1));
26+
27+
$this->assertEquals(1, $stub->getIndex());
28+
}
29+
30+
/**
31+
* Test setBoolVal, setIntVal, setFloatVal, setEnumVal with normal value
32+
*/
33+
public function testSetValNormal()
34+
{
35+
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
36+
37+
$this->assertEquals(true, self::callProtectedMethod($stub, 'setBoolVal', array(true, false)));
38+
$this->assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', array(12, 200)));
39+
$this->assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', array(871.1, 2.1)));
40+
$this->assertEquals('a', self::callProtectedMethod($stub, 'setEnumVal', array('a', array('a', 'b'), 'b')));
41+
}
42+
43+
/**
44+
* Test setBoolVal, setIntVal, setFloatVal, setEnumVal with default value
45+
*/
46+
public function testSetValDefault()
47+
{
48+
$stub = $this->getMockForAbstractClass('\PhpOffice\PhpWord\Style\AbstractStyle');
49+
50+
$this->assertEquals(false, self::callProtectedMethod($stub, 'setBoolVal', array('a', false)));
51+
$this->assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', array('foo', 200)));
52+
$this->assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', array('foo', 2.1)));
53+
$this->assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', array('z', array('a', 'b'), 'b')));
54+
}
55+
56+
/**
57+
* Helper function to call protected method
58+
*/
59+
public static function callProtectedMethod($object, $method, array $args = array())
60+
{
61+
$class = new \ReflectionClass(get_class($object));
62+
$method = $class->getMethod($method);
63+
$method->setAccessible(true);
64+
return $method->invokeArgs($object, $args);
65+
}
66+
}

tests/PhpWord/Tests/Style/ListItemTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,16 @@ public function testListType()
5353
$object->setListType($value);
5454
$this->assertEquals($value, $object->getListType());
5555
}
56+
57+
/**
58+
* Test set/get numbering style name
59+
*/
60+
public function testSetGetNumStyle()
61+
{
62+
$expected = 'List Name';
63+
64+
$object = new ListItem();
65+
$object->setNumStyle($expected);
66+
$this->assertEquals($expected, $object->getNumStyle());
67+
}
5668
}

0 commit comments

Comments
 (0)