Skip to content

Commit 2e61f10

Browse files
committed
Confirm Warnings etc. Become Exceptions
1 parent f4aea96 commit 2e61f10

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/PhpWord/Helper/Handler.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpWord\Helper;
4+
5+
class Handler
6+
{
7+
/** @var string */
8+
private static $invalidHex = 'Y';
9+
10+
// A bunch of methods to show that we continue
11+
// to capture messages even using PhpUnit 10.
12+
public static function suppressed(): bool
13+
{
14+
return @trigger_error('hello');
15+
}
16+
17+
public static function deprecated(): string
18+
{
19+
return (string) hexdec(self::$invalidHex);
20+
}
21+
22+
/** @param string $value */
23+
public static function notice($value): void
24+
{
25+
date_default_timezone_set($value);
26+
}
27+
28+
public static function warning(): bool
29+
{
30+
return file_get_contents(__FILE__ . 'noexist') !== false;
31+
}
32+
33+
public static function userDeprecated(): bool
34+
{
35+
return trigger_error('hello', E_USER_DEPRECATED);
36+
}
37+
38+
public static function userNotice(): bool
39+
{
40+
return trigger_error('userNotice', E_USER_NOTICE);
41+
}
42+
43+
public static function userWarning(): bool
44+
{
45+
return trigger_error('userWarning', E_USER_WARNING);
46+
}
47+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpWordTests\Helper;
6+
7+
use PhpOffice\PhpWord\Helper\Handler;
8+
use PHPUnit\Framework\TestCase;
9+
use Throwable;
10+
11+
class HandlerTest extends TestCase
12+
{
13+
/** @var int */
14+
protected static $versionLimit = 70400;
15+
16+
public function testSuppressed(): void
17+
{
18+
self::assertTrue(Handler::suppressed());
19+
}
20+
21+
public function testDeprecated(): void
22+
{
23+
try {
24+
Handler::deprecated();
25+
self::assertLessThan(self::$versionLimit, PHP_VERSION_ID, 'Expected error/exception did not happen');
26+
} catch (Throwable $e) {
27+
self::assertStringContainsString('Invalid characters', $e->getMessage());
28+
}
29+
}
30+
31+
public function testNotice(): void
32+
{
33+
try {
34+
Handler::notice('invalidtz');
35+
self::fail('Expected error/exception did not happen');
36+
} catch (Throwable $e) {
37+
self::assertStringContainsString('Timezone', $e->getMessage());
38+
}
39+
}
40+
41+
public function testWarning(): void
42+
{
43+
try {
44+
Handler::warning();
45+
self::fail('Expected error/exception did not happen');
46+
} catch (Throwable $e) {
47+
self::assertStringContainsString('ailed to open stream', $e->getMessage());
48+
}
49+
}
50+
51+
public function testUserDeprecated(): void
52+
{
53+
try {
54+
Handler::userDeprecated();
55+
self::fail('Expected error/exception did not happen');
56+
} catch (Throwable $e) {
57+
self::assertStringContainsString('hello', $e->getMessage());
58+
}
59+
}
60+
61+
public function testUserNotice(): void
62+
{
63+
try {
64+
Handler::userNotice();
65+
self::fail('Expected error/exception did not happen');
66+
} catch (Throwable $e) {
67+
self::assertStringContainsString('userNotice', $e->getMessage());
68+
}
69+
}
70+
71+
public function testUserWarning(): void
72+
{
73+
try {
74+
Handler::userWarning();
75+
self::fail('Expected error/exception did not happen');
76+
} catch (Throwable $e) {
77+
self::assertStringContainsString('userWarning', $e->getMessage());
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)