File tree Expand file tree Collapse file tree 3 files changed +94
-1
lines changed
tests/end-to-end/regression Expand file tree Collapse file tree 3 files changed +94
-1
lines changed Original file line number Diff line number Diff line change 11
11
12
12
use function array_keys ;
13
13
use function get_object_vars ;
14
+ use function is_int ;
15
+ use function sprintf ;
14
16
use RuntimeException ;
15
17
use Throwable ;
16
18
@@ -45,8 +47,20 @@ class Exception extends RuntimeException implements \PHPUnit\Exception
45
47
*/
46
48
protected array $ serializableTrace ;
47
49
48
- public function __construct (string $ message = '' , int $ code = 0 , ?Throwable $ previous = null )
50
+ public function __construct (string $ message = '' , int | string $ code = 0 , ?Throwable $ previous = null )
49
51
{
52
+ /**
53
+ * @see https://github.com/sebastianbergmann/phpunit/issues/5965
54
+ */
55
+ if (!is_int ($ code )) {
56
+ $ message .= sprintf (
57
+ ' (exception code: %s) ' ,
58
+ $ code ,
59
+ );
60
+
61
+ $ code = 0 ;
62
+ }
63
+
50
64
parent ::__construct ($ message , $ code , $ previous );
51
65
52
66
$ this ->serializableTrace = $ this ->getTrace ();
Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ https://github.com/sebastianbergmann/phpunit/issues/5965
3
+ --FILE--
4
+ <?php declare (strict_types=1 );
5
+ $ _SERVER ['argv ' ][] = '--do-not-cache-result ' ;
6
+ $ _SERVER ['argv ' ][] = '--no-configuration ' ;
7
+ $ _SERVER ['argv ' ][] = '--debug ' ;
8
+ $ _SERVER ['argv ' ][] = __DIR__ . '/5965/Issue5965Test.php ' ;
9
+
10
+ require_once __DIR__ . '/../../bootstrap.php ' ;
11
+
12
+ (new PHPUnit \TextUI \Application )->run ($ _SERVER ['argv ' ]);
13
+ --EXPECTF --
14
+ PHPUnit Started (PHPUnit %s using %s)
15
+ Test Runner Configured
16
+ Event Facade Sealed
17
+ Test Suite Loaded (1 test)
18
+ Test Runner Started
19
+ Test Suite Sorted
20
+ Test Runner Execution Started (1 test)
21
+ Test Suite Started (PHPUnit \TestFixture \Issue5891 \Issue5965Test, 1 test)
22
+ Test Preparation Started (PHPUnit \TestFixture \Issue5891 \Issue5965Test::testOne)
23
+ Test Prepared (PHPUnit \TestFixture \Issue5891 \Issue5965Test::testOne)
24
+ Test Errored (PHPUnit \TestFixture \Issue5891 \Issue5965Test::testOne)
25
+ (exception code: HY000 )
26
+ Test Finished (PHPUnit \TestFixture \Issue5891 \Issue5965Test::testOne)
27
+ Test Suite Finished (PHPUnit \TestFixture \Issue5891 \Issue5965Test, 1 test)
28
+ Test Runner Execution Finished
29
+ Test Runner Finished
30
+ PHPUnit Finished (Shell Exit Code: 2 )
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types=1 );
2
+ /*
3
+ * This file is part of PHPUnit.
4
+ *
5
+ * (c) Sebastian Bergmann <[email protected] >
6
+ *
7
+ * For the full copyright and license information, please view the LICENSE
8
+ * file that was distributed with this source code.
9
+ */
10
+ namespace PHPUnit \TestFixture \Issue5891 ;
11
+
12
+ use IteratorAggregate ;
13
+ use PDOException ;
14
+ use PHPUnit \Framework \Attributes \RequiresPhpExtension ;
15
+ use PHPUnit \Framework \TestCase ;
16
+ use ReflectionClass ;
17
+ use Traversable ;
18
+
19
+ #[RequiresPhpExtension('pdo ' )]
20
+ final class Issue5965Test extends TestCase
21
+ {
22
+ public function testOne (): void
23
+ {
24
+ $ exception = new PDOException ;
25
+ $ reflector = new ReflectionClass ($ exception );
26
+
27
+ $ property = $ reflector ->getProperty ('code ' );
28
+ $ property ->setValue ($ exception , 'HY000 ' );
29
+
30
+ $ this ->assertIsString ($ exception ->getCode ());
31
+
32
+ $ iterator = new class ($ exception ) implements IteratorAggregate
33
+ {
34
+ public PDOException $ exception ;
35
+
36
+ public function __construct ($ exception )
37
+ {
38
+ $ this ->exception = $ exception ;
39
+ }
40
+
41
+ public function getIterator (): Traversable
42
+ {
43
+ throw $ this ->exception ;
44
+ }
45
+ };
46
+
47
+ $ this ->assertCount (0 , $ iterator );
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments