Skip to content

Commit 0b074ed

Browse files
committed
Merge branch 'master' into merge-3.2
2 parents 7b95fd2 + 7d813bc commit 0b074ed

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/Response.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@
4747

4848
class Response implements PsrResponseInterface, ResponseInterface
4949
{
50-
use Macroable;
50+
use Macroable {
51+
Macroable::__call as macroCall;
52+
Macroable::__callStatic as macroCallStatic;
53+
}
5154

5255
public function __construct(protected ?ResponsePlusInterface $response = null)
5356
{
5457
}
5558

5659
public function __call(string $name, array $arguments): mixed
5760
{
61+
if (static::hasMacro($method)) {
62+
return $this->macroCall($method, $parameters);
63+
}
64+
5865
$response = $this->getResponse();
5966
if (! method_exists($response, $name)) {
6067
throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $name));
@@ -64,6 +71,10 @@ public function __call(string $name, array $arguments): mixed
6471

6572
public static function __callStatic($method, $parameters)
6673
{
74+
if (static::hasMacro($method)) {
75+
return static::macroCallStatic($method, $parameters);
76+
}
77+
6778
$response = Context::get(PsrResponseInterface::class);
6879
if (! method_exists($response, $method)) {
6980
throw new BadMethodCallException(sprintf('Call to undefined static method %s::%s()', self::class, $method));

tests/ResponseTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
* @coversNothing
3939
*/
4040
#[CoversNothing]
41-
/**
42-
* @internal
43-
* @coversNothing
44-
*/
4541
class ResponseTest extends TestCase
4642
{
4743
protected function tearDown(): void
@@ -231,6 +227,7 @@ public function testPsrResponse()
231227

232228
$this->assertInstanceOf(PsrResponseInterface::class, $response);
233229
$this->assertInstanceOf(ResponseInterface::class, $response);
230+
$this->assertSame('xxx', (string) $response->getBody());
234231
}
235232

236233
public function testCookiesAndHeaders()
@@ -278,4 +275,23 @@ public function testCookiesAndHeaders()
278275

279276
$this->assertSame($psrResponse, Context::get(PsrResponseInterface::class));
280277
}
278+
279+
public function testCallMacro()
280+
{
281+
Response::macro('testMacro', function () {
282+
return 'testMacro';
283+
});
284+
285+
$response = new Response();
286+
$this->assertSame('testMacro', $response->testMacro());
287+
}
288+
289+
public function testCallStaticMacro()
290+
{
291+
Response::macro('testStaticMacro', function () {
292+
return 'testStaticMacro';
293+
});
294+
295+
$this->assertSame('testStaticMacro', Response::testStaticMacro());
296+
}
281297
}

0 commit comments

Comments
 (0)