|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Common; |
| 4 | + |
| 5 | +use Http\Client\BatchResult; |
| 6 | +use Http\Client\HttpClient; |
| 7 | +use Http\Client\Common\HttpMethodsClient; |
| 8 | +use Http\Message\MessageFactory; |
| 9 | +use Psr\Http\Message\RequestInterface; |
| 10 | +use Psr\Http\Message\ResponseInterface; |
| 11 | +use PhpSpec\ObjectBehavior; |
| 12 | + |
| 13 | +class HttpMethodsClientSpec extends ObjectBehavior |
| 14 | +{ |
| 15 | + function let(HttpClient $client, MessageFactory $messageFactory) |
| 16 | + { |
| 17 | + $this->beAnInstanceOf( |
| 18 | + 'spec\Http\Client\Common\HttpMethodsClientStub', [ |
| 19 | + $client, |
| 20 | + $messageFactory |
| 21 | + ] |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + function it_sends_a_get_request() |
| 26 | + { |
| 27 | + $data = HttpMethodsClientStub::$requestData; |
| 28 | + |
| 29 | + $this->get($data['uri'], $data['headers'])->shouldReturn(true); |
| 30 | + } |
| 31 | + |
| 32 | + function it_sends_a_head_request() |
| 33 | + { |
| 34 | + $data = HttpMethodsClientStub::$requestData; |
| 35 | + |
| 36 | + $this->head($data['uri'], $data['headers'])->shouldReturn(true); |
| 37 | + } |
| 38 | + |
| 39 | + function it_sends_a_trace_request() |
| 40 | + { |
| 41 | + $data = HttpMethodsClientStub::$requestData; |
| 42 | + |
| 43 | + $this->trace($data['uri'], $data['headers'])->shouldReturn(true); |
| 44 | + } |
| 45 | + |
| 46 | + function it_sends_a_post_request() |
| 47 | + { |
| 48 | + $data = HttpMethodsClientStub::$requestData; |
| 49 | + |
| 50 | + $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); |
| 51 | + } |
| 52 | + |
| 53 | + function it_sends_a_put_request() |
| 54 | + { |
| 55 | + $data = HttpMethodsClientStub::$requestData; |
| 56 | + |
| 57 | + $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); |
| 58 | + } |
| 59 | + |
| 60 | + function it_sends_a_patch_request() |
| 61 | + { |
| 62 | + $data = HttpMethodsClientStub::$requestData; |
| 63 | + |
| 64 | + $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); |
| 65 | + } |
| 66 | + |
| 67 | + function it_sends_a_delete_request() |
| 68 | + { |
| 69 | + $data = HttpMethodsClientStub::$requestData; |
| 70 | + |
| 71 | + $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); |
| 72 | + } |
| 73 | + |
| 74 | + function it_sends_a_options_request() |
| 75 | + { |
| 76 | + $data = HttpMethodsClientStub::$requestData; |
| 77 | + |
| 78 | + $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturn(true); |
| 79 | + } |
| 80 | + |
| 81 | + function it_sends_request_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response) |
| 82 | + { |
| 83 | + $client->sendRequest($request)->shouldBeCalled()->willReturn($response); |
| 84 | + |
| 85 | + $this->beConstructedWith($client, $messageFactory); |
| 86 | + $this->sendRequest($request)->shouldReturn($response); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class HttpMethodsClientStub extends HttpMethodsClient |
| 91 | +{ |
| 92 | + public static $requestData = [ |
| 93 | + 'uri' => '/uri', |
| 94 | + 'headers' => [ |
| 95 | + 'Content-Type' => 'text/plain', |
| 96 | + ], |
| 97 | + 'body' => 'body' |
| 98 | + ]; |
| 99 | + |
| 100 | + /** |
| 101 | + * {@inheritdoc} |
| 102 | + */ |
| 103 | + public function send($method, $uri, array $headers = [], $body = null) |
| 104 | + { |
| 105 | + if (in_array($method, ['GET', 'HEAD', 'TRACE'])) { |
| 106 | + return $uri === self::$requestData['uri'] && |
| 107 | + $headers === self::$requestData['headers'] && |
| 108 | + is_null($body); |
| 109 | + } |
| 110 | + |
| 111 | + return in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) && |
| 112 | + $uri === self::$requestData['uri'] && |
| 113 | + $headers === self::$requestData['headers'] && |
| 114 | + $body === self::$requestData['body']; |
| 115 | + } |
| 116 | +} |
0 commit comments