Skip to content

Commit a7989a5

Browse files
committed
Make ResponseByMethod respect sub MultiResponseInterfaces
Adds `next()` to ResponseByMethod which itself invokes next on the most recently called method.
1 parent 1be8385 commit a7989a5

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

src/ResponseByMethod.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* ResponseByMethod is used to vary the response to a request by the called HTTP Method.
77
*/
8-
class ResponseByMethod implements ResponseInterface {
8+
class ResponseByMethod implements MultiResponseInterface {
99

1010
public const METHOD_GET = 'GET';
1111
public const METHOD_POST = 'POST';
@@ -22,13 +22,16 @@ class ResponseByMethod implements ResponseInterface {
2222
/** @var ResponseInterface */
2323
private $defaultResponse;
2424

25+
/** @var string|null */
26+
private $latestMethod;
27+
2528
/**
2629
* MethodResponse constructor.
2730
*
2831
* @param array<string, ResponseInterface> $responses A map of responses keyed by their method.
29-
* @param ResponseInterface|null $defaultResponse The fallthrough response to return if a response for a given
30-
* method is not found. If this is not defined the server will
31-
* return an HTTP 501 error.
32+
* @param ResponseInterface|null $defaultResponse The fallthrough response to return if a response for a
33+
* given method is not found. If this is not defined the
34+
* server will return an HTTP 501 error.
3235
*/
3336
public function __construct( array $responses = [], ?ResponseInterface $defaultResponse = null ) {
3437
foreach( $responses as $method => $response ) {
@@ -64,7 +67,8 @@ public function getStatus( RequestInfo $request ) : int {
6467
}
6568

6669
private function getMethodResponse( RequestInfo $request ) : ResponseInterface {
67-
$method = $request->getRequestMethod();
70+
$method = $request->getRequestMethod();
71+
$this->latestMethod = $method;
6872

6973
return $this->responses[$method] ?? $this->defaultResponse;
7074
}
@@ -76,4 +80,21 @@ public function setMethodResponse( string $method, ResponseInterface $response )
7680
$this->responses[$method] = $response;
7781
}
7882

83+
public function next() : bool {
84+
$method = $this->latestMethod;
85+
if( !$method ) {
86+
return false;
87+
}
88+
89+
if( !isset($this->responses[$method]) ) {
90+
return false;
91+
}
92+
93+
if( !$this->responses[$method] instanceof MultiResponseInterface ) {
94+
return false;
95+
}
96+
97+
return $this->responses[$method]->next();
98+
}
99+
79100
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Test\Regression;
4+
5+
use donatj\MockWebServer\MockWebServer;
6+
use donatj\MockWebServer\Response;
7+
use donatj\MockWebServer\ResponseByMethod;
8+
use donatj\MockWebServer\ResponseStack;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class ResponseByMethod_RegressionTest extends TestCase {
12+
13+
public function test_forwardNextAsExpected() : void {
14+
$server = new MockWebServer;
15+
$path = $server->setResponseOfPath(
16+
'/interest-categories/cat-xyz/interests',
17+
new ResponseByMethod([
18+
ResponseByMethod::METHOD_GET => new ResponseStack(
19+
new Response('get-a'),
20+
new Response('get-b'),
21+
new Response('get-c')
22+
),
23+
ResponseByMethod::METHOD_DELETE => new ResponseStack(
24+
new Response('delete-a'),
25+
new Response('delete-b'),
26+
new Response('delete-c')
27+
),
28+
])
29+
);
30+
31+
$server->start();
32+
33+
$method = function ( string $method ) {
34+
return stream_context_create([ 'http' => [ 'method' => $method ] ]);
35+
};
36+
37+
$this->assertSame('get-a', file_get_contents($path));
38+
$this->assertSame('delete-a', file_get_contents($path, false, $method('DELETE')));
39+
$this->assertSame('get-b', file_get_contents($path));
40+
$this->assertSame('delete-b', file_get_contents($path, false, $method('DELETE')));
41+
$this->assertSame('delete-c', file_get_contents($path, false, $method('DELETE')));
42+
$this->assertSame(false, @file_get_contents($path, false, $method('DELETE')));
43+
$this->assertSame('get-c', file_get_contents($path));
44+
$this->assertSame(false, @file_get_contents($path));
45+
46+
$server->stop();
47+
}
48+
49+
}

0 commit comments

Comments
 (0)