|
5 | 5 | use GuzzleHttp\Psr7\Response; |
6 | 6 | use Http\Client\Common\HttpMethodsClient; |
7 | 7 | use Http\Client\HttpClient; |
8 | | -use Http\Message\MessageFactory; |
| 8 | +use Http\Message\RequestFactory; |
9 | 9 | use PhpSpec\ObjectBehavior; |
10 | 10 | use Psr\Http\Message\RequestInterface; |
11 | 11 | use Psr\Http\Message\ResponseInterface; |
12 | 12 |
|
13 | 13 | class HttpMethodsClientSpec extends ObjectBehavior |
14 | 14 | { |
15 | | - public function let(HttpClient $client, MessageFactory $messageFactory) |
| 15 | + private static $requestData = [ |
| 16 | + 'uri' => '/uri', |
| 17 | + 'headers' => [ |
| 18 | + 'Content-Type' => 'text/plain', |
| 19 | + ], |
| 20 | + 'body' => 'body', |
| 21 | + ]; |
| 22 | + |
| 23 | + public function let(HttpClient $client, RequestFactory $requestFactory) |
16 | 24 | { |
17 | 25 | $this->beAnInstanceOf( |
18 | | - HttpMethodsClientStub::class, [ |
| 26 | + HttpMethodsClient::class, [ |
19 | 27 | $client, |
20 | | - $messageFactory, |
| 28 | + $requestFactory, |
21 | 29 | ] |
22 | 30 | ); |
23 | 31 | } |
24 | 32 |
|
25 | | - public function it_sends_a_get_request() |
| 33 | + public function it_sends_a_get_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
26 | 34 | { |
27 | | - $data = HttpMethodsClientStub::$requestData; |
28 | | - |
29 | | - $this->get($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 35 | + $this->assert($client, $requestFactory, $request, $response, 'get'); |
30 | 36 | } |
31 | 37 |
|
32 | | - public function it_sends_a_head_request() |
33 | | - { |
34 | | - $data = HttpMethodsClientStub::$requestData; |
35 | 38 |
|
36 | | - $this->head($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 39 | + public function it_sends_a_head_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
| 40 | + { |
| 41 | + $this->assert($client, $requestFactory, $request, $response, 'head'); |
37 | 42 | } |
38 | 43 |
|
39 | | - public function it_sends_a_trace_request() |
| 44 | + public function it_sends_a_trace_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
40 | 45 | { |
41 | | - $data = HttpMethodsClientStub::$requestData; |
42 | | - |
43 | | - $this->trace($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 46 | + $this->assert($client, $requestFactory, $request, $response, 'trace'); |
44 | 47 | } |
45 | 48 |
|
46 | | - public function it_sends_a_post_request() |
| 49 | + public function it_sends_a_post_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
47 | 50 | { |
48 | | - $data = HttpMethodsClientStub::$requestData; |
49 | | - |
50 | | - $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 51 | + $this->assert($client, $requestFactory, $request, $response, 'post', self::$requestData['body']); |
51 | 52 | } |
52 | 53 |
|
53 | | - public function it_sends_a_put_request() |
| 54 | + public function it_sends_a_put_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
54 | 55 | { |
55 | | - $data = HttpMethodsClientStub::$requestData; |
56 | | - |
57 | | - $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 56 | + $this->assert($client, $requestFactory, $request, $response, 'put', self::$requestData['body']); |
58 | 57 | } |
59 | 58 |
|
60 | | - public function it_sends_a_patch_request() |
| 59 | + public function it_sends_a_patch_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
61 | 60 | { |
62 | | - $data = HttpMethodsClientStub::$requestData; |
63 | | - |
64 | | - $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 61 | + $this->assert($client, $requestFactory, $request, $response, 'patch', self::$requestData['body']); |
65 | 62 | } |
66 | 63 |
|
67 | | - public function it_sends_a_delete_request() |
| 64 | + public function it_sends_a_delete_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
68 | 65 | { |
69 | | - $data = HttpMethodsClientStub::$requestData; |
70 | | - |
71 | | - $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 66 | + $this->assert($client, $requestFactory, $request, $response, 'delete', self::$requestData['body']); |
72 | 67 | } |
73 | 68 |
|
74 | | - public function it_sends_a_options_request() |
| 69 | + public function it_sends_an_options_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
75 | 70 | { |
76 | | - $data = HttpMethodsClientStub::$requestData; |
77 | | - |
78 | | - $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 71 | + $this->assert($client, $requestFactory, $request, $response, 'options', self::$requestData['body']); |
79 | 72 | } |
80 | 73 |
|
81 | | - public function it_sends_request_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response) |
| 74 | + /** |
| 75 | + * Run the actual test. |
| 76 | + * |
| 77 | + * As there is no data provider in phpspec, we keep separate methods to get new mocks for each test. |
| 78 | + */ |
| 79 | + private function assert(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response, string $method, string $body = null) |
82 | 80 | { |
83 | 81 | $client->sendRequest($request)->shouldBeCalled()->willReturn($response); |
| 82 | + $this->mockFactory($requestFactory, $request, strtoupper($method), $body); |
84 | 83 |
|
85 | | - $this->beConstructedWith($client, $messageFactory); |
86 | | - $this->sendRequest($request)->shouldReturn($response); |
87 | | - } |
88 | | -} |
| 84 | + $this->$method(self::$requestData['uri'], self::$requestData['headers'], self::$requestData['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
89 | 85 |
|
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 | | - ]; |
| 86 | + } |
99 | 87 |
|
100 | | - /** |
101 | | - * {@inheritdoc} |
102 | | - */ |
103 | | - public function send($method, $uri, array $headers = [], $body = null): ResponseInterface |
| 88 | + private function mockFactory(RequestFactory $requestFactory, RequestInterface $request, string $method, string $body = null) |
104 | 89 | { |
105 | | - if ($uri !== self::$requestData['uri']) { |
106 | | - throw new \InvalidArgumentException('Invalid URI: '.$uri); |
107 | | - } |
108 | | - |
109 | | - if ($headers !== self::$requestData['headers']) { |
110 | | - throw new \InvalidArgumentException('Invalid headers: '.print_r($headers, true)); |
111 | | - } |
112 | | - |
113 | | - switch ($method) { |
114 | | - case 'GET': |
115 | | - case 'HEAD': |
116 | | - case 'TRACE': |
117 | | - if (null !== $body) { |
118 | | - throw new \InvalidArgumentException('Non-empty body'); |
119 | | - } |
120 | | - |
121 | | - return new Response(); |
122 | | - case 'POST': |
123 | | - case 'PUT': |
124 | | - case 'PATCH': |
125 | | - case 'DELETE': |
126 | | - case 'OPTIONS': |
127 | | - if ($body !== self::$requestData['body']) { |
128 | | - throw new \InvalidArgumentException('Invalid body: '.print_r($body, true)); |
129 | | - } |
130 | | - |
131 | | - return new Response(); |
132 | | - default: |
133 | | - throw new \InvalidArgumentException('Invalid method: '.$method); |
134 | | - } |
| 90 | + $requestFactory->createRequest($method, self::$requestData['uri'], self::$requestData['headers'], $body)->willReturn($request); |
135 | 91 | } |
136 | 92 | } |
0 commit comments