diff --git a/composer.json b/composer.json index 250c02c..c8bee7d 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=5.5.0", - "guzzlehttp/guzzle": "^6.0", + "guzzlehttp/guzzle": "^5.0|^6.0", "symfony/serializer": "^2.0|^3.0" }, "require-dev": { diff --git a/src/Josser/Client/Transport/Guzzle5Transport.php b/src/Josser/Client/Transport/Guzzle5Transport.php new file mode 100644 index 0000000..840e28d --- /dev/null +++ b/src/Josser/Client/Transport/Guzzle5Transport.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Josser\Client\Transport; + +use GuzzleHttp\Client; +use Josser\Exception\TransportFailureException; + +/** + * JSON-RPC http transport with Guzzle 5. + * + * @author Alan Gabriel Bem + */ +class Guzzle5Transport implements TransportInterface +{ + /** + * Guzzle http client. + * + * @var Client + */ + private $guzzle; + + /** + * @param Client $guzzle + */ + public function __construct(Client $guzzle) + { + $this->guzzle = $guzzle; + } + + /** + * @return Client + */ + public function getGuzzle() + { + return $this->guzzle; + } + + /** + * Send data to remote JSON-RPC service over HTTP. + * + * @throws \Josser\Exception\TransportFailureException + * @param mixed $data + * @return string + */ + public function send($data) + { + try { + $response = $this->guzzle->post(null, [ + 'body' => $data, + 'headers' => [ + 'Content-Type' => 'application/json', + ] + ]); + return $response->getBody()->getContents(); + } catch (\Exception $e) { + $error = sprintf('JSON-RPC http connection failed. Remote service at "%s" is not responding.', $this->guzzle->getBaseUrl()); + throw new TransportFailureException($error, null, $e); + } + } +} diff --git a/src/Josser/Client/Transport/HttpTransport.php b/src/Josser/Client/Transport/Guzzle6Transport.php similarity index 94% rename from src/Josser/Client/Transport/HttpTransport.php rename to src/Josser/Client/Transport/Guzzle6Transport.php index 5fe9802..887505d 100644 --- a/src/Josser/Client/Transport/HttpTransport.php +++ b/src/Josser/Client/Transport/Guzzle6Transport.php @@ -15,11 +15,11 @@ use Josser\Exception\TransportFailureException; /** - * JSON-RPC http transport. + * JSON-RPC http transport with Guzzle 6. * * @author Alan Gabriel Bem */ -class HttpTransport implements TransportInterface +class Guzzle6Transport implements TransportInterface { /** * Guzzle http client. diff --git a/tests/Josser/Tests/Transport/HttpTransportTest.php b/tests/Josser/Tests/Transport/HttpTransportTest.php deleted file mode 100644 index d66862b..0000000 --- a/tests/Josser/Tests/Transport/HttpTransportTest.php +++ /dev/null @@ -1,93 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Josser\Tests\Transport; - -use GuzzleHttp\Client; -use GuzzleHttp\Exception\RequestException; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; -use Josser\Tests\TestCase as JosserTestCase; -use Josser\Client\Transport\HttpTransport; - -/** - * Test class for Josser\Transport\HttpTransport. - */ -class HttpTransportTest extends JosserTestCase -{ - /** - * - * Test getters of transport object. - */ - public function testGetters() - { - $guzzle = $this->getMock(Client::class); - $transport = new HttpTransport($guzzle); - - $this->assertSame($guzzle, $transport->getGuzzle()); - } - - public function testSend() - { - $mock = new MockHandler([ - new Response(200, [], '1'), - ]); - - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - - $transport = new HttpTransport($guzzle); - - $result = $transport->send('[]'); - - $this->assertEquals('1', $result); - } - - /** - * Test transport if there is no connection. - */ - public function testNoConnection() - { - $mock = new MockHandler([ - new RequestException("Error Communicating with Server", new Request('GET', 'test')) - ]); - - $handler = HandlerStack::create($mock); - $guzzle = new Client(['handler' => $handler]); - - $transport = new HttpTransport($guzzle); - - $this->setExpectedException('Josser\Exception\TransportFailureException'); - - $transport->send('[]'); - } - - /** - * Fixtures - * - * @return array - */ - public function connectionsProvider() - { - return array( - array('localhost', 'user', 'password', 8332, true, 'https://user:password@localhost:8332'), - array('localhost', 'user', 'password', 8332, false, 'http://user:password@localhost:8332'), - array('localhost', 'user', 'password', 9000, true, 'https://user:password@localhost:9000'), - array('localhost', 'user', 'password', 9000, false, 'http://user:password@localhost:9000'), - array('127.0.0.1', 'user', 'password', 8332, true, 'https://user:password@127.0.0.1:8332'), - array('127.0.0.1', 'user', 'password', 8332, false, 'http://user:password@127.0.0.1:8332'), - array('127.0.0.1', 'user', 'password', 9000, true, 'https://user:password@127.0.0.1:9000'), - array('127.0.0.1', 'user', 'password', 9000, false, 'http://user:password@127.0.0.1:9000'), - ); - } -}