-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathAbstractStyleTest.php
137 lines (120 loc) · 4.56 KB
/
AbstractStyleTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWordTests\Style;
use InvalidArgumentException;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Style\AbstractStyle;
use PhpOffice\PhpWord\Style\Paragraph;
use ReflectionClass;
/**
* Test class for PhpOffice\PhpWord\Style\AbstractStyle.
*
* @runTestsInSeparateProcesses
*/
class AbstractStyleTest extends \PHPUnit\Framework\TestCase
{
/**
* Test set style by array.
*/
public function testSetStyleByArray(): void
{
if (method_exists($this, 'getMockForAbstractClass')) {
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
} else {
/** @var AbstractStyle $stub */
$stub = new class() extends AbstractStyle {
};
}
$stub->setStyleByArray(['index' => 1]);
self::assertEquals(1, $stub->getIndex());
}
public function testSetStyleByArrayWithAlign(): void
{
$stub = new Paragraph();
$stub->setStyleByArray(['align' => Jc::CENTER]);
self::assertEquals(Jc::CENTER, $stub->getAlignment());
}
public function testSetStyleByArrayWithAlignment(): void
{
$stub = new Paragraph();
$stub->setStyleByArray(['alignment' => Jc::CENTER]);
self::assertEquals(Jc::CENTER, $stub->getAlignment());
}
/**
* Test setBoolVal, setIntVal, setFloatVal, setEnumVal with normal value.
*/
public function testSetValNormal(): void
{
if (method_exists($this, 'getMockForAbstractClass')) {
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
} else {
/** @var AbstractStyle $stub */
$stub = new class() extends AbstractStyle {
};
}
self::assertTrue(self::callProtectedMethod($stub, 'setBoolVal', [true, false]));
self::assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', [12, 200]));
self::assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', [871.1, 2.1]));
self::assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', ['871.1', 2.1]));
self::assertEquals('a', self::callProtectedMethod($stub, 'setEnumVal', ['a', ['a', 'b'], 'b']));
}
/**
* Test setBoolVal, setIntVal, setFloatVal, setEnumVal with default value.
*/
public function testSetValDefault(): void
{
if (method_exists($this, 'getMockForAbstractClass')) {
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
} else {
/** @var AbstractStyle $stub */
$stub = new class() extends AbstractStyle {
};
}
self::assertNotTrue(self::callProtectedMethod($stub, 'setBoolVal', ['a', false]));
self::assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', ['foo', 200]));
self::assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', ['foo', 2.1]));
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', [null, ['a', 'b'], 'b']));
}
/**
* Test setEnumVal exception.
*/
public function testSetValEnumException(): void
{
$this->expectException(InvalidArgumentException::class);
if (method_exists($this, 'getMockForAbstractClass')) {
$stub = $this->getMockForAbstractClass(AbstractStyle::class);
} else {
/** @var AbstractStyle $stub */
$stub = new class() extends AbstractStyle {
};
}
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b']));
}
/**
* Helper function to call protected method.
*
* @param mixed $object
* @param string $method
*/
public static function callProtectedMethod($object, $method, array $args = [])
{
$class = new ReflectionClass(get_class($object));
$method = $class->getMethod($method);
$method->setAccessible(true);
return $method->invokeArgs($object, $args);
}
}