Skip to content

Commit 1166b91

Browse files
committed
Feature: More tests
1 parent 33e63a5 commit 1166b91

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

tests/functional/CurlTest.php

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22

33
namespace Test\Functional;
44

5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
57
use SocialConnect\HttpClient\Curl;
68
use SocialConnect\HttpClient\Request;
79
use SocialConnect\HttpClient\StreamFactory;
810

911
class CurlTest extends \PHPUnit\Framework\TestCase
1012
{
11-
public function testGetMethod()
13+
protected function executeRequest(RequestInterface $request)
1214
{
1315
$client = new Curl();
14-
$response = $client->sendRequest(
15-
new Request('GET', 'http://127.0.0.1:5555/test-get')
16-
);
16+
return $client->sendRequest($request);
17+
}
1718

19+
protected function assertOk(ResponseInterface $response)
20+
{
1821
parent::assertSame(200, $response->getStatusCode());
1922
parent::assertSame('OK', $response->getReasonPhrase());
20-
parent::assertSame($response->getHeaderLine('content-type'), 'application/json');
23+
parent::assertSame('application/json', $response->getHeaderLine('content-type'));
24+
}
25+
26+
public function testGetMethod()
27+
{
28+
$response = $this->executeRequest(new Request('GET', 'http://127.0.0.1:5555/test-get'));
29+
30+
$this->assertOk($response);
2131

2232
$content = $response->getBody()->getContents();
2333
$result = json_decode($content, true);
@@ -36,11 +46,9 @@ public function testPostMethod()
3646
$streamFactory->createStream('payload')
3747
);
3848

39-
$client = new Curl();
40-
$response = $client->sendRequest($request);
49+
$response = $this->executeRequest($request);
4150

42-
parent::assertSame(200, $response->getStatusCode());
43-
parent::assertSame('OK', $response->getReasonPhrase());
51+
$this->assertOk($response);
4452
parent::assertSame($response->getHeaderLine('content-type'), 'application/json');
4553

4654
$content = $response->getBody()->getContents();
@@ -51,16 +59,33 @@ public function testPostMethod()
5159
parent::assertEquals($result['headers']['x-my-header'], ['5']);
5260
}
5361

54-
public function testDeleteMethod()
62+
public function testPutMethod()
5563
{
56-
$client = new Curl();
57-
$response = $client->sendRequest(
58-
new Request('DELETE', 'http://127.0.0.1:5555/test-delete')
64+
$streamFactory = new StreamFactory();
65+
66+
$request = new Request('PUT', 'http://127.0.0.1:5555/test-put');
67+
$request = $request->withHeader('X-MY-Header', '5');
68+
$request = $request->withBody(
69+
$streamFactory->createStream('payload')
5970
);
6071

61-
parent::assertSame(200, $response->getStatusCode());
62-
parent::assertSame('OK', $response->getReasonPhrase());
63-
parent::assertSame($response->getHeaderLine('content-type'), 'application/json');
72+
$response = $this->executeRequest($request);
73+
74+
$this->assertOk($response);
75+
76+
$content = $response->getBody()->getContents();
77+
$result = json_decode($content, true);
78+
79+
parent::assertEquals($result['method'], 'PUT');
80+
parent::assertEquals($result['uri'], 'http://127.0.0.1:5555/test-put');
81+
parent::assertEquals($result['headers']['x-my-header'], ['5']);
82+
}
83+
84+
public function testDeleteMethod()
85+
{
86+
$response = $this->executeRequest(new Request('DELETE', 'http://127.0.0.1:5555/test-delete'));
87+
88+
$this->assertOk($response);
6489

6590
$content = $response->getBody()->getContents();
6691
$result = json_decode($content, true);

0 commit comments

Comments
 (0)