• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

donatj / mock-webserver / 19079054503

04 Nov 2025 06:31PM UTC coverage: 77.119% (-1.7%) from 78.841%
19079054503

push

github

web-flow
Merge pull request #82 from donatj/fix/ResponseByMethod-call-next

Make ResponseByMethod respect sub MultiResponseInterfaces

0 of 10 new or added lines in 1 file covered. (0.0%)

273 of 354 relevant lines covered (77.12%)

4.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.67
/src/ResponseByMethod.php
1
<?php
2

3
namespace donatj\MockWebServer;
4

5
/**
6
 * ResponseByMethod is used to vary the response to a request by the called HTTP Method.
7
 */
8
class ResponseByMethod implements MultiResponseInterface {
9

10
        public const METHOD_GET     = 'GET';
11
        public const METHOD_POST    = 'POST';
12
        public const METHOD_PUT     = 'PUT';
13
        public const METHOD_PATCH   = 'PATCH';
14
        public const METHOD_DELETE  = 'DELETE';
15
        public const METHOD_HEAD    = 'HEAD';
16
        public const METHOD_OPTIONS = 'OPTIONS';
17
        public const METHOD_TRACE   = 'TRACE';
18

19
        /** @var ResponseInterface[] */
20
        private $responses = [];
21

22
        /** @var ResponseInterface */
23
        private $defaultResponse;
24

25
        /** @var string|null */
26
        private $latestMethod;
27

28
        /**
29
         * MethodResponse constructor.
30
         *
31
         * @param array<string, ResponseInterface> $responses       A map of responses keyed by their method.
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.
35
         */
36
        public function __construct( array $responses = [], ?ResponseInterface $defaultResponse = null ) {
37
                foreach( $responses as $method => $response ) {
3✔
38
                        $this->setMethodResponse($method, $response);
1✔
39
                }
40

41
                if( $defaultResponse ) {
3✔
42
                        $this->defaultResponse = $defaultResponse;
1✔
43
                } else {
44
                        $this->defaultResponse = new Response('MethodResponse - Method Not Defined', [], 501);
2✔
45
                }
46
        }
47

48
        public function getRef() : string {
49
                $refBase = $this->defaultResponse->getRef();
3✔
50
                foreach( $this->responses as $response ) {
3✔
51
                        $refBase .= $response->getRef();
2✔
52
                }
53

54
                return md5($refBase);
3✔
55
        }
56

57
        public function getBody( RequestInfo $request ) : string {
58
                return $this->getMethodResponse($request)->getBody($request);
×
59
        }
60

61
        public function getHeaders( RequestInfo $request ) : array {
62
                return $this->getMethodResponse($request)->getHeaders($request);
×
63
        }
64

65
        public function getStatus( RequestInfo $request ) : int {
66
                return $this->getMethodResponse($request)->getStatus($request);
×
67
        }
68

69
        private function getMethodResponse( RequestInfo $request ) : ResponseInterface {
NEW
70
                $method             = $request->getRequestMethod();
×
NEW
71
                $this->latestMethod = $method;
×
72

73
                return $this->responses[$method] ?? $this->defaultResponse;
×
74
        }
75

76
        /**
77
         * Set the Response for the Given Method
78
         */
79
        public function setMethodResponse( string $method, ResponseInterface $response ) : void {
80
                $this->responses[$method] = $response;
2✔
81
        }
82

83
        public function next() : bool {
NEW
84
                $method = $this->latestMethod;
×
NEW
85
                if( !$method ) {
×
NEW
86
                        return false;
×
87
                }
88

NEW
89
                if( !isset($this->responses[$method]) ) {
×
NEW
90
                        return false;
×
91
                }
92

NEW
93
                if( !$this->responses[$method] instanceof MultiResponseInterface ) {
×
NEW
94
                        return false;
×
95
                }
96

NEW
97
                return $this->responses[$method]->next();
×
98
        }
99

100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc