Skip to content

Commit bd41ffc

Browse files
Closes #5965
1 parent 0118e54 commit bd41ffc

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

ChangeLog-10.5.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi
99
* [#5957](https://github.com/sebastianbergmann/phpunit/pull/5957): Skip data provider build when requirements are not satisfied
1010
* [#5969](https://github.com/sebastianbergmann/phpunit/pull/5969): Check for requirements before creating a separate process
1111

12+
### Fixed
13+
14+
* [#5965](https://github.com/sebastianbergmann/phpunit/issues/5965): `PHPUnit\Framework\Exception` does not handle string error codes (`PDOException` with error code `'HY000'`, for example)
15+
1216
## [10.5.35] - 2024-09-19
1317

1418
### Changed

src/Framework/Exception/Exception.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use function array_keys;
1313
use function get_object_vars;
14+
use function is_int;
15+
use function sprintf;
1416
use RuntimeException;
1517
use Throwable;
1618

@@ -42,8 +44,20 @@ class Exception extends RuntimeException implements \PHPUnit\Exception
4244
{
4345
protected array $serializableTrace;
4446

45-
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
47+
public function __construct(string $message = '', int|string $code = 0, ?Throwable $previous = null)
4648
{
49+
/**
50+
* @see https://github.com/sebastianbergmann/phpunit/issues/5965
51+
*/
52+
if (!is_int($code)) {
53+
$message .= sprintf(
54+
' (exception code: %s)',
55+
$code,
56+
);
57+
58+
$code = 0;
59+
}
60+
4761
parent::__construct($message, $code, $previous);
4862

4963
$this->serializableTrace = $this->getTrace();

tests/end-to-end/regression/5965.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
Test Suite Loaded (1 test)
17+
Event Facade Sealed
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+
Assertion Succeeded (Constraint: is of type string, Value: 'HY000')
25+
Assertion Failed (Constraint: count matches 0, Value: {enable export of objects to see this value})
26+
Test Errored (PHPUnit\TestFixture\Issue5891\Issue5965Test::testOne)
27+
(exception code: HY000)
28+
Test Finished (PHPUnit\TestFixture\Issue5891\Issue5965Test::testOne)
29+
Test Suite Finished (PHPUnit\TestFixture\Issue5891\Issue5965Test, 1 test)
30+
Test Runner Execution Finished
31+
Test Runner Finished
32+
PHPUnit Finished (Shell Exit Code: 2)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

0 commit comments

Comments
 (0)