Skip to content

Commit 6bdb27a

Browse files
committed
No Need for getMockForAbstractClass
Fix #2851. No source changes, only tests. Since unit tests run with most recent components, and since Phpstan has been very capricious lately (178 new errors!), some other tests accompany this PR.
1 parent ca6d2f0 commit 6bdb27a

24 files changed

+277
-338
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
'method_argument_space' => true,
8181
'method_chaining_indentation' => true,
8282
'modernize_types_casting' => true,
83-
'modifier_keywords' => ['elements' => ['property', 'method']], // not const
8483
'multiline_comment_opening_closing' => true,
8584
'multiline_whitespace_before_semicolons' => true,
8685
'native_constant_invocation' => false, // Micro optimization that look messy
@@ -219,6 +218,7 @@
219218
'trailing_comma_in_multiline' => true,
220219
'trim_array_spaces' => true,
221220
'unary_operator_spaces' => true,
221+
'visibility_required' => ['elements' => ['property', 'method']], // not const
222222
'void_return' => true,
223223
'whitespace_after_comma_in_array' => true,
224224
'yoda_style' => false,

phpstan-baseline.neon

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,11 +1290,6 @@ parameters:
12901290
count: 1
12911291
path: tests/PhpWordTests/Shared/XMLWriterTest.php
12921292

1293-
-
1294-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\Style\\\\AbstractStyleTest\\:\\:callProtectedMethod\\(\\) has no return type specified\\.$#"
1295-
count: 1
1296-
path: tests/PhpWordTests/Style/AbstractStyleTest.php
1297-
12981293
-
12991294
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<object\\>\\|object, class\\-string\\|false given\\.$#"
13001295
count: 1
@@ -1375,21 +1370,6 @@ parameters:
13751370
count: 1
13761371
path: tests/PhpWordTests/TestableTemplateProcesor.php
13771372

1378-
-
1379-
message: "#^Cannot access property \\$length on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
1380-
count: 9
1381-
path: tests/PhpWordTests/Writer/HTML/ElementTest.php
1382-
1383-
-
1384-
message: "#^Cannot access property \\$length on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
1385-
count: 2
1386-
path: tests/PhpWordTests/Writer/HTML/Element/RubyTest.php
1387-
1388-
-
1389-
message: "#^Cannot call method item\\(\\) on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
1390-
count: 11
1391-
path: tests/PhpWordTests/Writer/HTML/ElementTest.php
1392-
13931373
-
13941374
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
13951375
count: 3
@@ -1425,26 +1405,11 @@ parameters:
14251405
count: 1
14261406
path: tests/PhpWordTests/Writer/Word2007/Element/ChartTest.php
14271407

1428-
-
1429-
message: "#^Call to an undefined method object\\:\\:write\\(\\)\\.$#"
1430-
count: 1
1431-
path: tests/PhpWordTests/Writer/Word2007/ElementTest.php
1432-
1433-
-
1434-
message: "#^Call to an undefined method object\\:\\:write\\(\\)\\.$#"
1435-
count: 1
1436-
path: tests/PhpWordTests/Writer/Word2007/StyleTest.php
1437-
14381408
-
14391409
message: "#^Method PhpOffice\\\\PhpWordTests\\\\XmlDocument\\:\\:getElement\\(\\) should return DOMElement\\|null but returns DOMNode\\|null\\.$#"
14401410
count: 1
14411411
path: tests/PhpWordTests/XmlDocument.php
14421412

1443-
-
1444-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\XmlDocument\\:\\:getNodeList\\(\\) should return DOMNodeList\\<DOMNode\\> but returns DOMNodeList\\<DOMNode\\>\\|false\\.$#"
1445-
count: 1
1446-
path: tests/PhpWordTests/XmlDocument.php
1447-
14481413
-
14491414
message: "#^Property PhpOffice\\\\PhpWordTests\\\\XmlDocument\\:\\:\\$path \\(string\\) does not accept string\\|false\\.$#"
14501415
count: 1
@@ -1460,33 +1425,3 @@ parameters:
14601425
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\HTML\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:|]+$#"
14611426
count: 1
14621427
path: src/PhpWord/Writer/HTML.php
1463-
1464-
# https://github.com/phpstan/phpstan/issues/8770
1465-
-
1466-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Element\\\\AbstractElement is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1467-
count: 2
1468-
path: tests/PhpWordTests/Element/AbstractElementTest.php
1469-
1470-
# https://github.com/phpstan/phpstan/issues/8770
1471-
-
1472-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1473-
count: 4
1474-
path: tests/PhpWordTests/Style/AbstractStyleTest.php
1475-
1476-
# https://github.com/phpstan/phpstan/issues/8770
1477-
-
1478-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\EPub3\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1479-
count: 1
1480-
path: tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php
1481-
1482-
# https://github.com/phpstan/phpstan/issues/8770
1483-
-
1484-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\ODText\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1485-
count: 2
1486-
path: tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php
1487-
1488-
# https://github.com/phpstan/phpstan/issues/8770
1489-
-
1490-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1491-
count: 2
1492-
path: tests/PhpWordTests/Writer/Word2007/Part/AbstractPartTest.php

phpstan-baseline.php7.neon

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,6 @@ parameters:
12651265
count: 1
12661266
path: tests/PhpWordTests/Shared/XMLWriterTest.php
12671267

1268-
-
1269-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\Style\\\\AbstractStyleTest\\:\\:callProtectedMethod\\(\\) has no return type specified\\.$#"
1270-
count: 1
1271-
path: tests/PhpWordTests/Style/AbstractStyleTest.php
1272-
12731268
-
12741269
message: "#^Cannot call method setLineHeight\\(\\) on PhpOffice\\\\PhpWord\\\\Style\\\\Font\\|string\\.$#"
12751270
count: 1
@@ -1349,11 +1344,6 @@ parameters:
13491344
message: "#^Cannot access property \\$length on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
13501345
count: 9
13511346
path: tests/PhpWordTests/Writer/HTML/ElementTest.php
1352-
1353-
-
1354-
message: "#^Cannot access property \\$length on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
1355-
count: 2
1356-
path: tests/PhpWordTests/Writer/HTML/Element/RubyTest.php
13571347

13581348
-
13591349
message: "#^Cannot call method item\\(\\) on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
@@ -1424,15 +1414,3 @@ parameters:
14241414
message: "#^Property PhpOffice\\\\PhpWordTests\\\\XmlDocument\\:\\:\\$xpath \\(DOMXPath\\) does not accept null\\.$#"
14251415
count: 1
14261416
path: tests/PhpWordTests/XmlDocument.php
1427-
1428-
# https://github.com/phpstan/phpstan/issues/8770
1429-
-
1430-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\EPub3\\\\Style\\\\AbstractStyle is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1431-
count: 1
1432-
path: tests/PhpWordTests/Writer/EPub3/Style/AbstractStyleTest.php
1433-
1434-
# https://github.com/phpstan/phpstan/issues/8770
1435-
-
1436-
message: "#^PHPDoc tag @var with type PhpOffice\\\\PhpWord\\\\Writer\\\\ODText\\\\Part\\\\AbstractPart is not subtype of native type[\\sA-Za-z\\@\\\\\\/0-9\\.:]+$#"
1437-
count: 2
1438-
path: tests/PhpWordTests/Writer/ODText/Part/AbstractPartTest.php

phpstan-baseline.php73.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,6 @@ parameters:
12651265
count: 1
12661266
path: tests/PhpWordTests/Shared/XMLWriterTest.php
12671267

1268-
-
1269-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\Style\\\\AbstractStyleTest\\:\\:callProtectedMethod\\(\\) has no return type specified\\.$#"
1270-
count: 1
1271-
path: tests/PhpWordTests/Style/AbstractStyleTest.php
1272-
12731268
-
12741269
message: "#^Cannot call method setLineHeight\\(\\) on PhpOffice\\\\PhpWord\\\\Style\\\\Font\\|string\\.$#"
12751270
count: 1
@@ -1349,11 +1344,6 @@ parameters:
13491344
message: "#^Cannot access property \\$length on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
13501345
count: 9
13511346
path: tests/PhpWordTests/Writer/HTML/ElementTest.php
1352-
1353-
-
1354-
message: "#^Cannot access property \\$length on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
1355-
count: 2
1356-
path: tests/PhpWordTests/Writer/HTML/Element/RubyTest.php
13571347

13581348
-
13591349
message: "#^Cannot call method item\\(\\) on DOMNodeList\\<DOMNode\\>\\|false\\.$#"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests\Element;
20+
21+
use PhpOffice\PhpWord\Element\AbstractElement;
22+
23+
class AbstractElementClass extends AbstractElement
24+
{
25+
}

tests/PhpWordTests/Element/AbstractElementTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,17 @@
1818

1919
namespace PhpOffice\PhpWordTests\Element;
2020

21-
use PhpOffice\PhpWord\Element\AbstractElement;
22-
2321
/**
2422
* Test class for PhpOffice\PhpWord\Element\AbstractElement.
2523
*/
2624
class AbstractElementTest extends \PHPUnit\Framework\TestCase
2725
{
28-
/** @var string */
29-
protected static $method = 'getMockForAbstractClass';
30-
3126
/**
3227
* Test set/get element index.
3328
*/
3429
public function testElementIndex(): void
3530
{
36-
$method = self::$method;
37-
if (method_exists($this, $method)) {
38-
$stub = $this->$method(AbstractElement::class);
39-
} else {
40-
/** @var AbstractElement $stub */
41-
$stub = new class() extends AbstractElement {
42-
};
43-
}
31+
$stub = new AbstractElementClass();
4432
$ival = mt_rand(0, 100);
4533
$stub->setElementIndex($ival);
4634
self::assertEquals($ival, $stub->getElementIndex());
@@ -51,14 +39,7 @@ public function testElementIndex(): void
5139
*/
5240
public function testElementId(): void
5341
{
54-
$method = self::$method;
55-
if (method_exists($this, $method)) {
56-
$stub = $this->$method(AbstractElement::class);
57-
} else {
58-
/** @var AbstractElement $stub */
59-
$stub = new class() extends AbstractElement {
60-
};
61-
}
42+
$stub = new AbstractElementClass();
6243
$stub->setElementId();
6344
self::assertEquals(6, strlen($stub->getElementId()));
6445
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests\Style;
20+
21+
use PhpOffice\PhpWord\Style\AbstractStyle;
22+
23+
/**
24+
* Test class for PhpOffice\PhpWord\Style\AbstractStyle.
25+
*/
26+
class AbstractStyleClass extends AbstractStyle
27+
{
28+
}

tests/PhpWordTests/Style/AbstractStyleTest.php

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
use InvalidArgumentException;
2222
use PhpOffice\PhpWord\SimpleType\Jc;
23-
use PhpOffice\PhpWord\Style\AbstractStyle;
2423
use PhpOffice\PhpWord\Style\Paragraph;
2524
use ReflectionClass;
2625

@@ -29,28 +28,12 @@
2928
*/
3029
class AbstractStyleTest extends \PHPUnit\Framework\TestCase
3130
{
32-
/** @var string */
33-
protected static $mockAbstract = 'getMockForAbstractClass';
34-
35-
/** @param string $method */
36-
private function methodFound($method): bool
37-
{
38-
return method_exists($this, $method);
39-
}
40-
4131
/**
4232
* Test set style by array.
4333
*/
4434
public function testSetStyleByArray(): void
4535
{
46-
$method = self::$mockAbstract;
47-
if (self::methodFound($method)) {
48-
$stub = $this->$method(AbstractStyle::class);
49-
} else {
50-
/** @var AbstractStyle $stub */
51-
$stub = new class() extends AbstractStyle {
52-
};
53-
}
36+
$stub = new AbstractStyleClass();
5437
$stub->setStyleByArray(['index' => 1]);
5538

5639
self::assertEquals(1, $stub->getIndex());
@@ -77,15 +60,7 @@ public function testSetStyleByArrayWithAlignment(): void
7760
*/
7861
public function testSetValNormal(): void
7962
{
80-
$method = self::$mockAbstract;
81-
if (self::methodFound($method)) {
82-
$stub = $this->$method(AbstractStyle::class);
83-
} else {
84-
/** @var AbstractStyle $stub */
85-
$stub = new class() extends AbstractStyle {
86-
};
87-
}
88-
63+
$stub = new AbstractStyleClass();
8964
self::assertTrue(self::callProtectedMethod($stub, 'setBoolVal', [true, false]));
9065
self::assertEquals(12, self::callProtectedMethod($stub, 'setIntVal', [12, 200]));
9166
self::assertEquals(871.1, self::callProtectedMethod($stub, 'setFloatVal', [871.1, 2.1]));
@@ -98,15 +73,7 @@ public function testSetValNormal(): void
9873
*/
9974
public function testSetValDefault(): void
10075
{
101-
$method = self::$mockAbstract;
102-
if (self::methodFound($method)) {
103-
$stub = $this->$method(AbstractStyle::class);
104-
} else {
105-
/** @var AbstractStyle $stub */
106-
$stub = new class() extends AbstractStyle {
107-
};
108-
}
109-
76+
$stub = new AbstractStyleClass();
11077
self::assertNotTrue(self::callProtectedMethod($stub, 'setBoolVal', ['a', false]));
11178
self::assertEquals(200, self::callProtectedMethod($stub, 'setIntVal', ['foo', 200]));
11279
self::assertEquals(2.1, self::callProtectedMethod($stub, 'setFloatVal', ['foo', 2.1]));
@@ -119,15 +86,7 @@ public function testSetValDefault(): void
11986
public function testSetValEnumException(): void
12087
{
12188
$this->expectException(InvalidArgumentException::class);
122-
$method = self::$mockAbstract;
123-
if (self::methodFound($method)) {
124-
$stub = $this->$method(AbstractStyle::class);
125-
} else {
126-
/** @var AbstractStyle $stub */
127-
$stub = new class() extends AbstractStyle {
128-
};
129-
}
130-
89+
$stub = new AbstractStyleClass();
13190
self::assertEquals('b', self::callProtectedMethod($stub, 'setEnumVal', ['z', ['a', 'b'], 'b']));
13291
}
13392

@@ -139,6 +98,8 @@ public function testSetValEnumException(): void
13998
*
14099
* @param mixed $object
141100
* @param string $method
101+
*
102+
* @return mixed
142103
*/
143104
public static function callProtectedMethod($object, $method, array $args = [])
144105
{

tests/PhpWordTests/Style/FontTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public function testSetStyleValueWithNullOrEmpty(): void
8383
'fallbackFont' => '',
8484
];
8585
foreach ($attributes as $key => $default) {
86-
$get = is_bool($default) ? "is{$key}" : "get{$key}";
86+
$uckey = ucfirst($key);
87+
$get = is_bool($default) ? "is{$uckey}" : "get{$uckey}";
88+
self::assertTrue(method_exists($object, $get));
8789
self::assertEquals($default, $object->$get());
8890
$object->setStyleValue($key, null);
8991
self::assertEquals($default, $object->$get());
@@ -128,7 +130,9 @@ public function testSetStyleValueNormal(): void
128130
];
129131
$object->setStyleByArray($attributes);
130132
foreach ($attributes as $key => $value) {
131-
$get = is_bool($value) ? "is{$key}" : "get{$key}";
133+
$uckey = ucfirst($key);
134+
$get = is_bool($value) ? "is{$uckey}" : "get{$uckey}";
135+
self::assertTrue(method_exists($object, $get));
132136
self::assertEquals($value, $object->$get());
133137
}
134138
}

0 commit comments

Comments
 (0)