Skip to content

Commit 4a11788

Browse files
Closes #4933
1 parent 7699c48 commit 4a11788

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

Diff for: ChangeLog-9.5.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes of the PHPUnit 9.5 release series are documented in this fil
77
### Fixed
88

99
* [#4929](https://github.com/sebastianbergmann/phpunit/issues/4929): Test Double code generator does not handle new expressions inside parameter default values
10+
* [#4933](https://github.com/sebastianbergmann/phpunit/issues/4933): Backport support for `never` type from PHPUnit 10 to PHPUnit 9.5
1011

1112
## [9.5.18] - 2022-03-08
1213

Diff for: src/Framework/MockObject/MockMethod.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ public function generateCode(): string
191191
{
192192
if ($this->static) {
193193
$templateFile = 'mocked_static_method.tpl';
194-
} elseif ($this->returnType instanceof VoidType) {
194+
} elseif ($this->returnType->isNever() || $this->returnType->isVoid()) {
195195
$templateFile = sprintf(
196-
'%s_method_void.tpl',
196+
'%s_method_never_or_void.tpl',
197197
$this->callOriginalMethod ? 'proxied' : 'mocked'
198198
);
199199
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
\PHPUnit\Framework\MockObject\Generator::generate('Foo', [], 'MockFoo', true, true)
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
if (version_compare('8.1.0-dev', PHP_VERSION, '>')) {
6+
print 'skip: PHP 8.1 is required.';
7+
}
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
interface Foo
11+
{
12+
public function bar(string $baz): never;
13+
}
14+
15+
require_once __DIR__ . '/../../../../vendor/autoload.php';
16+
17+
$generator = new \PHPUnit\Framework\MockObject\Generator;
18+
19+
$mock = $generator->generate(
20+
'Foo',
21+
[],
22+
'MockFoo',
23+
true,
24+
true
25+
);
26+
27+
print $mock->getClassCode();
28+
--EXPECTF--
29+
declare(strict_types=1);
30+
31+
class MockFoo implements PHPUnit\Framework\MockObject\MockObject, Foo
32+
{
33+
use \PHPUnit\Framework\MockObject\Api;
34+
use \PHPUnit\Framework\MockObject\Method;
35+
use \PHPUnit\Framework\MockObject\MockedCloneMethod;
36+
37+
public function bar(string $baz): never
38+
{
39+
$__phpunit_arguments = [$baz];
40+
$__phpunit_count = func_num_args();
41+
42+
if ($__phpunit_count > 1) {
43+
$__phpunit_arguments_tmp = func_get_args();
44+
45+
for ($__phpunit_i = 1; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
46+
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
47+
}
48+
}
49+
50+
$this->__phpunit_getInvocationHandler()->invoke(
51+
new \PHPUnit\Framework\MockObject\Invocation(
52+
'Foo', 'bar', $__phpunit_arguments, 'never', $this, true
53+
)
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)