Skip to content

Commit 21673a4

Browse files
Closes #6797
1 parent 54abce4 commit 21673a4

6 files changed

Lines changed: 74 additions & 7 deletions

File tree

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.53] - 2026-MM-DD
6+
7+
### Changed
8+
9+
* [#6797](https://github.com/sebastianbergmann/phpunit/issues/6797): Adapt code generated for test double of interface with constructor for PHP 8.6
10+
511
## [8.5.52] - 2026-01-27
612

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

389+
[8.5.53]: https://github.com/sebastianbergmann/phpunit/compare/8.5.52...8.5
383390
[8.5.52]: https://github.com/sebastianbergmann/phpunit/compare/8.5.51...8.5.52
384391
[8.5.51]: https://github.com/sebastianbergmann/phpunit/compare/8.5.50...8.5.51
385392
[8.5.50]: https://github.com/sebastianbergmann/phpunit/compare/8.5.49...8.5.50

src/Framework/MockObject/MockMethod.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function preg_replace;
1818
use function sprintf;
1919
use function str_replace;
20+
use function strtolower;
2021
use function substr_count;
2122
use function trim;
2223
use function var_export;
@@ -186,7 +187,7 @@ public function generateCode(): string
186187
{
187188
if ($this->static) {
188189
$templateFile = 'mocked_static_method.tpl';
189-
} elseif ($this->returnType instanceof VoidType) {
190+
} elseif ($this->returnType instanceof VoidType || $this->mustNotReturnValue()) {
190191
$templateFile = sprintf(
191192
'%s_method_void.tpl',
192193
$this->callOriginalMethod ? 'proxied' : 'mocked'
@@ -257,6 +258,16 @@ private function getTemplate(string $template): Text_Template
257258
return self::$templates[$filename];
258259
}
259260

261+
/**
262+
* @see https://wiki.php.net/rfc/deprecate-return-value-from-construct
263+
*/
264+
private function mustNotReturnValue(): bool
265+
{
266+
$methodName = strtolower($this->methodName);
267+
268+
return $methodName === '__construct' || $methodName === '__destruct';
269+
}
270+
260271
/**
261272
* Returns the parameters of a function or method.
262273
*

tests/end-to-end/mock-objects/generator/4139-php7.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ class %s implements PHPUnit\Framework\MockObject\MockObject, InterfaceWithConstr
4040
}
4141
}
4242

43-
$__phpunit_result = $this->__phpunit_getInvocationHandler()->invoke(
43+
$this->__phpunit_getInvocationHandler()->invoke(
4444
new \PHPUnit\Framework\MockObject\Invocation(
4545
'InterfaceWithConstructor', '__construct', $__phpunit_arguments, '', $this, true
4646
)
4747
);
48-
49-
return $__phpunit_result;
5048
}
5149
}

tests/end-to-end/mock-objects/generator/4139-php8.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ class %s implements PHPUnit\Framework\MockObject\MockObject, InterfaceWithConstr
4040
}
4141
}
4242

43-
$__phpunit_result = $this->__phpunit_getInvocationHandler()->invoke(
43+
$this->__phpunit_getInvocationHandler()->invoke(
4444
new \PHPUnit\Framework\MockObject\Invocation(
4545
'InterfaceWithConstructor', '__construct', $__phpunit_arguments, '', $this, true
4646
)
4747
);
48-
49-
return $__phpunit_result;
5048
}
5149
}
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/6797
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/6797/Issue6797Test.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+
.. 2 / 2 (100%)
16+
17+
Time: %s, Memory: %s
18+
19+
OK (2 tests, 2 assertions)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Issue6797;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
interface InterfaceWithConstructor
15+
{
16+
public function __construct();
17+
}
18+
19+
final class Issue6797Test extends TestCase
20+
{
21+
public function testStubCanBeCreatedForInterfaceWithConstructor(): void
22+
{
23+
$stub = $this->createStub(InterfaceWithConstructor::class);
24+
25+
$this->assertInstanceOf(InterfaceWithConstructor::class, $stub);
26+
}
27+
28+
public function testMockCanBeCreatedForInterfaceWithConstructor(): void
29+
{
30+
$mock = $this->createMock(InterfaceWithConstructor::class);
31+
32+
$this->assertInstanceOf(InterfaceWithConstructor::class, $mock);
33+
}
34+
}

0 commit comments

Comments
 (0)