Skip to content

Commit 750ce17

Browse files
Merge pull request #1184 from ghostwriter/bugfix/support-never-return-type
[8.1] Support `never` return type
2 parents 33cb226 + ec2443a commit 750ce17

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

library/Mockery/Generator/StringManipulation/Pass/MethodDefinitionPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function renderMethodBody($method, $config)
157157

158158
$body .= "\$ret = {$invoke}(__FUNCTION__, \$argv);\n";
159159

160-
if ($method->getReturnType() !== "void") {
160+
if (! in_array($method->getReturnType(), ['never','void'], true)) {
161161
$body .= "return \$ret;\n";
162162
}
163163

tests/PHP81/Php81LanguageFeaturesTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
use Mockery;
77
use Mockery\Adapter\Phpunit\MockeryTestCase;
88
use ReturnTypeWillChange;
9+
use RuntimeException;
910
use Serializable;
11+
use function pcntl_fork;
12+
use function pcntl_waitpid;
13+
use function pcntl_wexitstatus;
1014

1115
/**
1216
* @requires PHP 8.1.0-dev
@@ -82,6 +86,39 @@ public function it_can_mock_a_class_with_an_intersection_argument_type_hint()
8286

8387
$mock->foo($object);
8488
}
89+
90+
/** @test */
91+
public function it_can_mock_a_class_with_a_never_returning_type_hint()
92+
{
93+
$mock = Mockery::mock(NeverReturningTypehintClass::class)->makePartial();
94+
95+
$this->expectException(RuntimeException::class);
96+
$mock->throws();
97+
}
98+
99+
/**
100+
* @requires extension pcntl
101+
* @test
102+
*/
103+
public function it_can_mock_a_class_with_a_never_returning_type_hint_with_exit()
104+
{
105+
$mock = Mockery::mock(NeverReturningTypehintClass::class)->makePartial();
106+
107+
$pid = pcntl_fork();
108+
109+
if (-1 === $pid) {
110+
$this->markTestSkipped("Couldn't fork for exit test");
111+
112+
return;
113+
} elseif ($pid) {
114+
pcntl_waitpid($pid, $status);
115+
$this->assertEquals(123, pcntl_wexitstatus($status));
116+
117+
return;
118+
}
119+
120+
$mock->exits();
121+
}
85122
}
86123

87124
interface LoggerInterface
@@ -135,6 +172,18 @@ public function getTimestamp(): float
135172
}
136173
}
137174

175+
class NeverReturningTypehintClass
176+
{
177+
public function throws(): never
178+
{
179+
throw new RuntimeException('Never!');
180+
}
181+
182+
public function exits(): never
183+
{
184+
exit(123);
185+
}
186+
}
138187
class IntersectionTypeHelperClass
139188
{
140189
}

0 commit comments

Comments
 (0)