File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 4747
4848class 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 ));
Original file line number Diff line number Diff line change 3838 * @coversNothing
3939 */
4040#[CoversNothing]
41- /**
42- * @internal
43- * @coversNothing
44- */
4541class 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}
You can’t perform that action at this time.
0 commit comments