Skip to content

Commit 91237cd

Browse files
Merge branch '10.5' into 11.3
2 parents cab8ad5 + bd41ffc commit 91237cd

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

ChangeLog-11.3.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 11.3 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
## [11.3.6] - 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

@@ -45,8 +47,20 @@ class Exception extends RuntimeException implements \PHPUnit\Exception
4547
*/
4648
protected array $serializableTrace;
4749

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)
4951
{
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+
5064
parent::__construct($message, $code, $previous);
5165

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
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)
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)