Skip to content

Commit

Permalink
Merge pull request #10 from toin0u/psr7
Browse files Browse the repository at this point in the history
Use PSR-7 and HTTPlug
  • Loading branch information
toin0u committed Mar 11, 2016
2 parents 9b2403e + a83196e commit 3e38b8b
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 124 deletions.
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -19,14 +19,17 @@
],
"require": {
"php": ">=5.4.0",
"egeloen/http-adapter": "0.6.*@dev"
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0"
},
"require-dev": {
"tedivm/stash": "dev-master",
"phpspec/phpspec": "~2.1",
"henrikbjorn/phpspec-code-coverage" : "~1.0"
},
"suggest": {
"php-http/client-implementation": "An HTTP Client implementation",
"php-http/message": "Provides Message Factory implementations",
"tedivm/stash": "Used for caching URLs"
},
"autoload": {
Expand Down
56 changes: 43 additions & 13 deletions spec/Provider/BitlySpec.php
Expand Up @@ -2,16 +2,18 @@

namespace spec\Concise\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Psr\Http\Message\IncomingResponseInterface as Response;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class BitlySpec extends ObjectBehavior
{
function let(HttpAdapterInterface $adapter)
function let(HttpClient $httpClient, RequestFactory $requestFactory)
{
$this->beConstructedWith($adapter, 'access_token');
$this->beConstructedWith('access_token', $httpClient, $requestFactory);
}

function it_is_initializable()
Expand All @@ -24,18 +26,46 @@ function it_is_a_provider()
$this->shouldImplement('Concise\Provider');
}

function it_shortens_a_url(HttpAdapterInterface $adapter, Response $response)
{
$response->getBody()->willReturn('{"data": {"url": "http://bit.ly/shortened"}}');
$adapter->get(Argument::type('string'))->willReturn($response);
function it_shortens_a_url(
RequestFactory $requestFactory,
RequestInterface $request,
HttpClient $httpClient,
ResponseInterface $response,
StreamInterface $body
) {
$requestFactory
->createRequest(
'GET',
'https://api-ssl.bitly.com/v3/shorten?access_token=access_token&longUrl='.urlencode('http://any.url')
)
->willReturn($request);
;

$httpClient->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->__toString()->willReturn('{"data": {"url": "http://bit.ly/shortened"}}');

$this->shorten('http://any.url')->shouldReturn("http://bit.ly/shortened");
}

function it_expands_a_url(HttpAdapterInterface $adapter, Response $response)
{
$response->getBody()->willReturn('{"data": {"expand": [{"long_url": "http://any.url"}]}}');
$adapter->get(Argument::type('string'))->willReturn($response);
function it_expands_a_url(
RequestFactory $requestFactory,
RequestInterface $request,
HttpClient $httpClient,
ResponseInterface $response,
StreamInterface $body
) {
$requestFactory
->createRequest(
'GET',
'https://api-ssl.bitly.com/v3/expand?access_token=access_token&shortUrl='.urlencode('http://bit.ly/shortened')
)
->willReturn($request);
;

$httpClient->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->__toString()->willReturn('{"data": {"expand": [{"long_url": "http://any.url"}]}}');

$this->expand('http://bit.ly/shortened')->shouldReturn("http://any.url");
}
Expand Down
61 changes: 48 additions & 13 deletions spec/Provider/GoogleSpec.php
Expand Up @@ -2,16 +2,18 @@

namespace spec\Concise\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Psr\Http\Message\IncomingResponseInterface as Response;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class GoogleSpec extends ObjectBehavior
{
function let(HttpAdapterInterface $adapter)
function let(HttpClient $httpClient, RequestFactory $requestFactory)
{
$this->beConstructedWith($adapter);
$this->beConstructedWith($httpClient, $requestFactory);
}

function it_is_initializable()
Expand All @@ -24,18 +26,51 @@ function it_is_a_provider()
$this->shouldImplement('Concise\Provider');
}

function it_shortens_a_url(HttpAdapterInterface $adapter, Response $response)
{
$response->getBody()->willReturn('{"id": "http://goo.gl/shortened"}');
$adapter->post(Argument::type('string'), Argument::type('array'), Argument::type('string'))->willReturn($response);
function it_shortens_a_url(
RequestFactory $requestFactory,
RequestInterface $request,
HttpClient $httpClient,
ResponseInterface $response,
StreamInterface $body
) {
$requestFactory
->createRequest(
'POST',
'https://www.googleapis.com/urlshortener/v1/url',
['Content-Type' => 'application/json'],
json_encode([
'key' => null,
'longUrl' => 'http://any.url',
])
)
->willReturn($request);
;

$httpClient->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->__toString()->willReturn('{"id": "http://goo.gl/shortened"}');

$this->shorten('http://any.url')->shouldReturn("http://goo.gl/shortened");
}

function it_expands_a_url(HttpAdapterInterface $adapter, Response $response)
{
$response->getBody()->willReturn('{"longUrl": "http://any.url"}');
$adapter->get(Argument::type('string'))->willReturn($response);
function it_expands_a_url(
RequestFactory $requestFactory,
RequestInterface $request,
HttpClient $httpClient,
ResponseInterface $response,
StreamInterface $body
) {
$requestFactory
->createRequest(
'GET',
'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.urlencode('http://goo.gl/shortened')
)
->willReturn($request);
;

$httpClient->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->__toString()->willReturn('{"longUrl": "http://any.url"}');

$this->expand('http://goo.gl/shortened')->shouldReturn("http://any.url");
}
Expand Down
73 changes: 60 additions & 13 deletions spec/Provider/TinyccSpec.php
Expand Up @@ -2,16 +2,26 @@

namespace spec\Concise\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Psr\Http\Message\IncomingResponseInterface as Response;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TinyccSpec extends ObjectBehavior
{
function let(HttpAdapterInterface $adapter)
private $params = [
'c' => 'rest_api',
'version' => '2.0.3',
'format' => 'json',
'login' => 'login',
'apiKey' => 'apiKey',
];

function let(HttpClient $httpClient, RequestFactory $requestFactory)
{
$this->beConstructedWith($adapter, 'login', 'apiKey');
$this->beConstructedWith('login', 'apiKey', $httpClient, $requestFactory);
}

function it_is_initializable()
Expand All @@ -24,18 +34,55 @@ function it_is_a_provider()
$this->shouldImplement('Concise\Provider');
}

function it_shortens_a_url(HttpAdapterInterface $adapter, Response $response)
{
$response->getBody()->willReturn('{"results": {"short_url": "http://tiny.cc/shortened"}}');
$adapter->get(Argument::type('string'))->willReturn($response);
function it_shortens_a_url(
RequestFactory $requestFactory,
RequestInterface $request,
HttpClient $httpClient,
ResponseInterface $response,
StreamInterface $body
) {
$params = array_merge($this->params, [
'm' => 'shorten',
'longUrl' => 'http://any.url',
]);
$requestFactory
->createRequest(
'GET',
sprintf('http://tiny.cc?%s', http_build_query($params))
)
->willReturn($request);
;

$httpClient->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->__toString()->willReturn('{"results": {"short_url": "http://tiny.cc/shortened"}}');

$this->shorten('http://any.url')->shouldReturn("http://tiny.cc/shortened");
}

function it_expands_a_url(HttpAdapterInterface $adapter, Response $response)
{
$response->getBody()->willReturn('{"results": {"longUrl": "http://any.url"}}');
$adapter->get(Argument::type('string'))->willReturn($response);
function it_expands_a_url(
RequestFactory $requestFactory,
RequestInterface $request,
HttpClient $httpClient,
ResponseInterface $response,
StreamInterface $body
) {
$params = array_merge($this->params, [
'm' => 'expand',
'shortUrl' => 'http://tiny.cc/shortened',
]);

$requestFactory
->createRequest(
'GET',
sprintf('http://tiny.cc?%s', http_build_query($params))
)
->willReturn($request);
;

$httpClient->sendRequest($request)->willReturn($response);
$response->getBody()->willReturn($body);
$body->__toString()->willReturn('{"results": {"longUrl": "http://any.url"}}');

$this->expand('http://tiny.cc/shortened')->shouldReturn("http://any.url");
}
Expand Down
43 changes: 31 additions & 12 deletions src/Provider/Bitly.php
Expand Up @@ -11,12 +11,14 @@

namespace Concise\Provider;

use Ivory\HttpAdapter\HttpAdapterInterface;
use Concise\Provider;
use Http\Client\HttpClient;
use Http\Message\RequestFactory;

/**
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
class Bitly extends HttpAdapterAware
class Bitly implements Provider
{
/**
* @var string
Expand All @@ -26,17 +28,28 @@ class Bitly extends HttpAdapterAware
/**
* @var string
*/
protected $accessToken;
private $accessToken;

/**
* @param HttpAdapterInterface $adapter
* @param string $accessToken
* @var HttpClient
*/
public function __construct(HttpAdapterInterface $adapter, $accessToken)
{
parent::__construct($adapter);
private $httpClient;

/**
* @var RequestFactory
*/
private $requestFactory;

/**
* @param string $accessToken
* @param HttpClient $httpClient
* @param RequestFactory $requestFactory
*/
public function __construct($accessToken, HttpClient $httpClient, RequestFactory $requestFactory)
{
$this->accessToken = $accessToken;
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
}

/**
Expand All @@ -49,8 +62,11 @@ public function shorten($url)
'longUrl' => trim($url),
]));

$response = $this->adapter->get($url);
$response = json_decode($response->getBody());
$request = $this->requestFactory->createRequest('GET', $url);

$response = $this->httpClient->sendRequest($request);

$response = json_decode((string) $response->getBody());

return $response->data->url;
}
Expand All @@ -65,8 +81,11 @@ public function expand($url)
'shortUrl' => trim($url),
]));

$response = $this->adapter->get($url);
$response = json_decode($response->getBody());
$request = $this->requestFactory->createRequest('GET', $url);

$response = $this->httpClient->sendRequest($request);

$response = json_decode((string) $response->getBody());

return $response->data->expand[0]->long_url;
}
Expand Down

0 comments on commit 3e38b8b

Please sign in to comment.