Skip to content

Commit

Permalink
Feature: More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jul 23, 2019
1 parent 33e63a5 commit 1166b91
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions tests/functional/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@

namespace Test\Functional;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SocialConnect\HttpClient\Curl;
use SocialConnect\HttpClient\Request;
use SocialConnect\HttpClient\StreamFactory;

class CurlTest extends \PHPUnit\Framework\TestCase
{
public function testGetMethod()
protected function executeRequest(RequestInterface $request)
{
$client = new Curl();
$response = $client->sendRequest(
new Request('GET', 'http://127.0.0.1:5555/test-get')
);
return $client->sendRequest($request);
}

protected function assertOk(ResponseInterface $response)
{
parent::assertSame(200, $response->getStatusCode());
parent::assertSame('OK', $response->getReasonPhrase());
parent::assertSame($response->getHeaderLine('content-type'), 'application/json');
parent::assertSame('application/json', $response->getHeaderLine('content-type'));
}

public function testGetMethod()
{
$response = $this->executeRequest(new Request('GET', 'http://127.0.0.1:5555/test-get'));

$this->assertOk($response);

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

$client = new Curl();
$response = $client->sendRequest($request);
$response = $this->executeRequest($request);

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

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

public function testDeleteMethod()
public function testPutMethod()
{
$client = new Curl();
$response = $client->sendRequest(
new Request('DELETE', 'http://127.0.0.1:5555/test-delete')
$streamFactory = new StreamFactory();

$request = new Request('PUT', 'http://127.0.0.1:5555/test-put');
$request = $request->withHeader('X-MY-Header', '5');
$request = $request->withBody(
$streamFactory->createStream('payload')
);

parent::assertSame(200, $response->getStatusCode());
parent::assertSame('OK', $response->getReasonPhrase());
parent::assertSame($response->getHeaderLine('content-type'), 'application/json');
$response = $this->executeRequest($request);

$this->assertOk($response);

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

parent::assertEquals($result['method'], 'PUT');
parent::assertEquals($result['uri'], 'http://127.0.0.1:5555/test-put');
parent::assertEquals($result['headers']['x-my-header'], ['5']);
}

public function testDeleteMethod()
{
$response = $this->executeRequest(new Request('DELETE', 'http://127.0.0.1:5555/test-delete'));

$this->assertOk($response);

$content = $response->getBody()->getContents();
$result = json_decode($content, true);
Expand Down

0 comments on commit 1166b91

Please sign in to comment.