-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyccSpec.php
89 lines (76 loc) · 2.51 KB
/
TinyccSpec.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace spec\Concise\Provider;
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;
class TinyccSpec extends ObjectBehavior
{
private $params = [
'c' => 'rest_api',
'version' => '2.0.3',
'format' => 'json',
'login' => 'login',
'apiKey' => 'apiKey',
];
function let(HttpClient $httpClient, RequestFactory $requestFactory)
{
$this->beConstructedWith('login', 'apiKey', $httpClient, $requestFactory);
}
function it_is_initializable()
{
$this->shouldHaveType('Concise\Provider\Tinycc');
}
function it_is_a_provider()
{
$this->shouldImplement('Concise\Provider');
}
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(
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");
}
}