Skip to content

Commit 8b019ea

Browse files
Exclude __sleep() and __wakeup() from test double code generation on PHP >= 8.5
1 parent 5b10dc2 commit 8b019ea

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

ChangeLog-8.5.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes of the PHPUnit 8.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [8.5.46] - 2025-MM-DD
6+
7+
### Changed
8+
9+
* [#6366](https://github.com/sebastianbergmann/phpunit/issues/6366): Exclude `__sleep()` and `__wakeup()` from test double code generation on PHP >= 8.5
10+
511
## [8.5.45] - 2025-09-11
612

713
### Changed
@@ -346,6 +352,7 @@ All notable changes of the PHPUnit 8.5 release series are documented in this fil
346352
* [#3967](https://github.com/sebastianbergmann/phpunit/issues/3967): Cannot double interface that extends interface that extends `\Throwable`
347353
* [#3968](https://github.com/sebastianbergmann/phpunit/pull/3968): Test class run in a separate PHP process are passing when `exit` called inside
348354

355+
[8.5.46]: https://github.com/sebastianbergmann/phpunit/compare/8.5.45...8.5
349356
[8.5.45]: https://github.com/sebastianbergmann/phpunit/compare/8.5.44...8.5.45
350357
[8.5.44]: https://github.com/sebastianbergmann/phpunit/compare/8.5.43...8.5.44
351358
[8.5.43]: https://github.com/sebastianbergmann/phpunit/compare/8.5.42...8.5.43

src/Framework/MockObject/Generator.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use const DIRECTORY_SEPARATOR;
1313
use const PHP_EOL;
1414
use const PHP_MAJOR_VERSION;
15+
use const PHP_VERSION;
1516
use const PREG_OFFSET_CAPTURE;
1617
use const WSDL_CACHE_NONE;
1718
use function array_diff_assoc;
@@ -43,6 +44,7 @@
4344
use function strtolower;
4445
use function substr;
4546
use function trait_exists;
47+
use function version_compare;
4648
use Doctrine\Instantiator\Exception\ExceptionInterface as InstantiatorException;
4749
use Doctrine\Instantiator\Instantiator;
4850
use Exception;
@@ -113,20 +115,9 @@ public function __clone()
113115
EOT;
114116

115117
/**
116-
* @var array
118+
* @var array<non-empty-string, true>
117119
*/
118-
private const BLACKLISTED_METHOD_NAMES = [
119-
'__CLASS__' => true,
120-
'__DIR__' => true,
121-
'__FILE__' => true,
122-
'__FUNCTION__' => true,
123-
'__LINE__' => true,
124-
'__METHOD__' => true,
125-
'__NAMESPACE__' => true,
126-
'__TRAIT__' => true,
127-
'__clone' => true,
128-
'__halt_compiler' => true,
129-
];
120+
private static $excludedMethodNames = [];
130121

131122
/**
132123
* @var array
@@ -1112,12 +1103,32 @@ private function generateMockClassDeclaration(array $mockClassName, bool $isInte
11121103

11131104
private function canMockMethod(ReflectionMethod $method): bool
11141105
{
1115-
return !($this->isConstructor($method) || $method->isFinal() || $method->isPrivate() || $this->isMethodNameBlacklisted($method->getName()));
1106+
return !($this->isConstructor($method) || $method->isFinal() || $method->isPrivate() || $this->isMethodNameExcluded($method->getName()));
11161107
}
11171108

1118-
private function isMethodNameBlacklisted(string $name): bool
1109+
private function isMethodNameExcluded(string $name): bool
11191110
{
1120-
return isset(self::BLACKLISTED_METHOD_NAMES[$name]);
1111+
if (self::$excludedMethodNames === []) {
1112+
self::$excludedMethodNames = [
1113+
'__CLASS__' => true,
1114+
'__DIR__' => true,
1115+
'__FILE__' => true,
1116+
'__FUNCTION__' => true,
1117+
'__LINE__' => true,
1118+
'__METHOD__' => true,
1119+
'__NAMESPACE__' => true,
1120+
'__TRAIT__' => true,
1121+
'__clone' => true,
1122+
'__halt_compiler' => true,
1123+
];
1124+
1125+
if (version_compare(PHP_VERSION, '8.5', '>=')) {
1126+
self::$excludedMethodNames['__sleep'] = true;
1127+
self::$excludedMethodNames['__wakeup'] = true;
1128+
}
1129+
}
1130+
1131+
return isset(self::$excludedMethodNames[$name]);
11211132
}
11221133

11231134
private function getTemplate(string $template): Text_Template
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/6366
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/6366/Issue6366Test.php';
8+
9+
require_once __DIR__ . '/../../bootstrap.php';
10+
11+
PHPUnit\TextUI\Command::main();
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
. 1 / 1 (100%)
16+
17+
Time: %s, Memory: %s
18+
19+
OK (1 test, 0 assertions)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\Issue6366;
11+
12+
use Exception;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class Issue6366Test extends TestCase
16+
{
17+
/**
18+
* @doesNotPerformAssertions
19+
*/
20+
public function testOne(): void
21+
{
22+
$this->createStub(Exception::class);
23+
}
24+
}

0 commit comments

Comments
 (0)