|
2 | 2 |
|
3 | 3 | namespace spec\Http\Client\Common; |
4 | 4 |
|
5 | | -use GuzzleHttp\Psr7\Response; |
6 | 5 | use Http\Client\Common\HttpMethodsClient; |
7 | 6 | use Http\Client\HttpClient; |
8 | | -use Http\Message\MessageFactory; |
| 7 | +use Http\Message\RequestFactory; |
9 | 8 | use PhpSpec\ObjectBehavior; |
10 | 9 | use Psr\Http\Message\RequestInterface; |
11 | 10 | use Psr\Http\Message\ResponseInterface; |
12 | 11 |
|
13 | 12 | class HttpMethodsClientSpec extends ObjectBehavior |
14 | 13 | { |
15 | | - public function let(HttpClient $client, MessageFactory $messageFactory) |
| 14 | + private static $requestData = [ |
| 15 | + 'uri' => '/uri', |
| 16 | + 'headers' => [ |
| 17 | + 'Content-Type' => 'text/plain', |
| 18 | + ], |
| 19 | + 'body' => 'body', |
| 20 | + ]; |
| 21 | + |
| 22 | + public function let(HttpClient $client, RequestFactory $requestFactory) |
16 | 23 | { |
17 | 24 | $this->beAnInstanceOf( |
18 | | - HttpMethodsClientStub::class, [ |
| 25 | + HttpMethodsClient::class, [ |
19 | 26 | $client, |
20 | | - $messageFactory, |
| 27 | + $requestFactory, |
21 | 28 | ] |
22 | 29 | ); |
23 | 30 | } |
24 | 31 |
|
25 | | - public function it_sends_a_get_request() |
| 32 | + public function it_sends_a_get_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
26 | 33 | { |
27 | | - $data = HttpMethodsClientStub::$requestData; |
28 | | - |
29 | | - $this->get($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 34 | + $this->assert($client, $requestFactory, $request, $response, 'get'); |
30 | 35 | } |
31 | 36 |
|
32 | | - public function it_sends_a_head_request() |
| 37 | + public function it_sends_a_head_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
33 | 38 | { |
34 | | - $data = HttpMethodsClientStub::$requestData; |
35 | | - |
36 | | - $this->head($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 39 | + $this->assert($client, $requestFactory, $request, $response, 'head'); |
37 | 40 | } |
38 | 41 |
|
39 | | - public function it_sends_a_trace_request() |
| 42 | + public function it_sends_a_trace_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
40 | 43 | { |
41 | | - $data = HttpMethodsClientStub::$requestData; |
42 | | - |
43 | | - $this->trace($data['uri'], $data['headers'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 44 | + $this->assert($client, $requestFactory, $request, $response, 'trace'); |
44 | 45 | } |
45 | 46 |
|
46 | | - public function it_sends_a_post_request() |
| 47 | + public function it_sends_a_post_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
47 | 48 | { |
48 | | - $data = HttpMethodsClientStub::$requestData; |
49 | | - |
50 | | - $this->post($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 49 | + $this->assert($client, $requestFactory, $request, $response, 'post', self::$requestData['body']); |
51 | 50 | } |
52 | 51 |
|
53 | | - public function it_sends_a_put_request() |
| 52 | + public function it_sends_a_put_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
54 | 53 | { |
55 | | - $data = HttpMethodsClientStub::$requestData; |
56 | | - |
57 | | - $this->put($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 54 | + $this->assert($client, $requestFactory, $request, $response, 'put', self::$requestData['body']); |
58 | 55 | } |
59 | 56 |
|
60 | | - public function it_sends_a_patch_request() |
| 57 | + public function it_sends_a_patch_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
61 | 58 | { |
62 | | - $data = HttpMethodsClientStub::$requestData; |
63 | | - |
64 | | - $this->patch($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 59 | + $this->assert($client, $requestFactory, $request, $response, 'patch', self::$requestData['body']); |
65 | 60 | } |
66 | 61 |
|
67 | | - public function it_sends_a_delete_request() |
| 62 | + public function it_sends_a_delete_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
68 | 63 | { |
69 | | - $data = HttpMethodsClientStub::$requestData; |
70 | | - |
71 | | - $this->delete($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 64 | + $this->assert($client, $requestFactory, $request, $response, 'delete', self::$requestData['body']); |
72 | 65 | } |
73 | 66 |
|
74 | | - public function it_sends_a_options_request() |
| 67 | + public function it_sends_an_options_request(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response) |
75 | 68 | { |
76 | | - $data = HttpMethodsClientStub::$requestData; |
77 | | - |
78 | | - $this->options($data['uri'], $data['headers'], $data['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
| 69 | + $this->assert($client, $requestFactory, $request, $response, 'options', self::$requestData['body']); |
79 | 70 | } |
80 | 71 |
|
81 | | - public function it_sends_request_with_underlying_client(HttpClient $client, MessageFactory $messageFactory, RequestInterface $request, ResponseInterface $response) |
| 72 | + /** |
| 73 | + * Run the actual test. |
| 74 | + * |
| 75 | + * As there is no data provider in phpspec, we keep separate methods to get new mocks for each test. |
| 76 | + */ |
| 77 | + private function assert(HttpClient $client, RequestFactory $requestFactory, RequestInterface $request, ResponseInterface $response, string $method, string $body = null) |
82 | 78 | { |
83 | 79 | $client->sendRequest($request)->shouldBeCalled()->willReturn($response); |
| 80 | + $this->mockFactory($requestFactory, $request, strtoupper($method), $body); |
84 | 81 |
|
85 | | - $this->beConstructedWith($client, $messageFactory); |
86 | | - $this->sendRequest($request)->shouldReturn($response); |
| 82 | + $this->$method(self::$requestData['uri'], self::$requestData['headers'], self::$requestData['body'])->shouldReturnAnInstanceOf(ResponseInterface::class); |
87 | 83 | } |
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 | 84 |
|
100 | | - /** |
101 | | - * {@inheritdoc} |
102 | | - */ |
103 | | - public function send($method, $uri, array $headers = [], $body = null): ResponseInterface |
| 85 | + private function mockFactory(RequestFactory $requestFactory, RequestInterface $request, string $method, string $body = null) |
104 | 86 | { |
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 | | - } |
| 87 | + $requestFactory->createRequest($method, self::$requestData['uri'], self::$requestData['headers'], $body)->willReturn($request); |
135 | 88 | } |
136 | 89 | } |
0 commit comments