Skip to content

Commit a740560

Browse files
committed
Switched to using psr/http-factory instead of php-http/message-factory
1 parent 6c4bca8 commit a740560

File tree

5 files changed

+109
-58
lines changed

5 files changed

+109
-58
lines changed

spec/Request/HandlerSpec.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace spec\Xabbuh\XApi\Client\Request;
44

5-
use Http\Client\HttpClient;
65
use Http\Message\RequestFactory;
76
use PhpSpec\ObjectBehavior;
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestFactoryInterface;
89
use Psr\Http\Message\RequestInterface;
910
use Psr\Http\Message\ResponseInterface;
1011
use Xabbuh\XApi\Common\Exception\AccessDeniedException;
@@ -14,7 +15,7 @@
1415

1516
class HandlerSpec extends ObjectBehavior
1617
{
17-
function let(HttpClient $client, RequestFactory $requestFactory)
18+
function let(ClientInterface $client, RequestFactoryInterface $requestFactory)
1819
{
1920
$this->beConstructedWith($client, $requestFactory, 'http://example.com/xapi/', '1.0.1');
2021
}
@@ -24,8 +25,10 @@ function it_throws_an_exception_if_a_request_is_created_with_an_invalid_method()
2425
$this->shouldThrow('\InvalidArgumentException')->during('createRequest', array('options', '/xapi/statements'));
2526
}
2627

27-
function it_returns_get_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
28-
{
28+
function it_returns_get_request_created_by_the_http_client(
29+
RequestFactoryInterface $requestFactory,
30+
RequestInterface $request
31+
) {
2932
$requestFactory->createRequest('GET', 'http://example.com/xapi/statements', array(
3033
'X-Experience-API-Version' => '1.0.1',
3134
'Content-Type' => 'application/json',
@@ -35,8 +38,10 @@ function it_returns_get_request_created_by_the_http_client(RequestFactory $reque
3538
$this->createRequest('GET', '/statements')->shouldReturn($request);
3639
}
3740

38-
function it_returns_post_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
39-
{
41+
function it_returns_post_request_created_by_the_http_client(
42+
RequestFactoryInterface $requestFactory,
43+
RequestInterface $request
44+
) {
4045
$requestFactory->createRequest('POST', 'http://example.com/xapi/statements', array(
4146
'X-Experience-API-Version' => '1.0.1',
4247
'Content-Type' => 'application/json',
@@ -46,8 +51,10 @@ function it_returns_post_request_created_by_the_http_client(RequestFactory $requ
4651
$this->createRequest('POST', '/statements', array(), 'body')->shouldReturn($request);
4752
}
4853

49-
function it_returns_put_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
50-
{
54+
function it_returns_put_request_created_by_the_http_client(
55+
RequestFactoryInterface $requestFactory,
56+
RequestInterface $request
57+
) {
5158
$requestFactory->createRequest('PUT', 'http://example.com/xapi/statements', array(
5259
'X-Experience-API-Version' => '1.0.1',
5360
'Content-Type' => 'application/json',
@@ -57,8 +64,10 @@ function it_returns_put_request_created_by_the_http_client(RequestFactory $reque
5764
$this->createRequest('PUT', '/statements', array(), 'body')->shouldReturn($request);
5865
}
5966

60-
function it_returns_delete_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
61-
{
67+
function it_returns_delete_request_created_by_the_http_client(
68+
RequestFactoryInterface $requestFactory,
69+
RequestInterface $request
70+
) {
6271
$requestFactory->createRequest('DELETE', 'http://example.com/xapi/statements', array(
6372
'X-Experience-API-Version' => '1.0.1',
6473
'Content-Type' => 'application/json',
@@ -68,53 +77,71 @@ function it_returns_delete_request_created_by_the_http_client(RequestFactory $re
6877
$this->createRequest('DELETE', '/statements')->shouldReturn($request);
6978
}
7079

71-
function it_throws_an_access_denied_exception_when_a_401_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
72-
{
80+
function it_throws_an_access_denied_exception_when_a_401_status_code_is_returned(
81+
ClientInterface $client,
82+
RequestInterface $request,
83+
ResponseInterface $response
84+
) {
7385
$client->sendRequest($request)->willReturn($response);
7486
$response->getStatusCode()->willReturn(401);
7587
$response->getBody()->willReturn('body');
7688

7789
$this->shouldThrow(AccessDeniedException::class)->during('executeRequest', array($request, array(200)));
7890
}
7991

80-
function it_throws_an_access_denied_exception_when_a_403_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
81-
{
92+
function it_throws_an_access_denied_exception_when_a_403_status_code_is_returned(
93+
ClientInterface $client,
94+
RequestInterface $request,
95+
ResponseInterface $response
96+
) {
8297
$client->sendRequest($request)->willReturn($response);
8398
$response->getStatusCode()->willReturn(403);
8499
$response->getBody()->willReturn('body');
85100

86101
$this->shouldThrow(AccessDeniedException::class)->during('executeRequest', array($request, array(200)));
87102
}
88103

89-
function it_throws_a_not_found_exception_when_a_404_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
90-
{
104+
function it_throws_a_not_found_exception_when_a_404_status_code_is_returned(
105+
ClientInterface $client,
106+
RequestInterface $request,
107+
ResponseInterface $response
108+
) {
91109
$client->sendRequest($request)->willReturn($response);
92110
$response->getStatusCode()->willReturn(404);
93111
$response->getBody()->willReturn('body');
94112

95113
$this->shouldThrow(NotFoundException::class)->during('executeRequest', array($request, array(200)));
96114
}
97115

98-
function it_throws_a_conflict_exception_when_a_409_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
99-
{
116+
function it_throws_a_conflict_exception_when_a_409_status_code_is_returned(
117+
ClientInterface $client,
118+
RequestInterface $request,
119+
ResponseInterface $response
120+
) {
100121
$client->sendRequest($request)->willReturn($response);
101122
$response->getStatusCode()->willReturn(409);
102123
$response->getBody()->willReturn('body');
103124

104125
$this->shouldThrow(ConflictException::class)->during('executeRequest', array($request, array(200)));
105126
}
106127

107-
function it_throws_an_xapi_exception_when_an_unexpected_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
108-
{
128+
function it_throws_an_xapi_exception_when_an_unexpected_status_code_is_returned(
129+
ClientInterface $client,
130+
RequestInterface $request,
131+
ResponseInterface $response
132+
) {
109133
$client->sendRequest($request)->willReturn($response);
110134
$response->getStatusCode()->willReturn(204);
111135
$response->getBody()->willReturn('body');
112136

113137
$this->shouldThrow(XApiException::class)->during('executeRequest', array($request, array(200)));
114138
}
115139

116-
function it_returns_the_response_on_success(HttpClient $client, RequestInterface $request, ResponseInterface $response)
117-
{
140+
function it_returns_the_response_on_success(
141+
ClientInterface $client,
142+
RequestInterface $request,
143+
ResponseInterface $response
144+
) {
118145
$client->sendRequest($request)->willReturn($response);
119146
$response->getStatusCode()->willReturn(200);
120147
$response->getBody()->willReturn('body');

spec/XApiClientBuilderSpec.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace spec\Xabbuh\XApi\Client;
44

5-
use Http\Client\HttpClient;
65
use Http\Discovery\HttpClientDiscovery;
76
use Http\Discovery\MessageFactoryDiscovery;
87
use Http\Message\RequestFactory;
98
use PhpSpec\Exception\Example\SkippingException;
109
use PhpSpec\ObjectBehavior;
10+
use Psr\Http\Client\ClientInterface;
11+
use Psr\Http\Message\RequestFactoryInterface;
1112
use Xabbuh\Http\Authentication\OAuth1;
1213
use Xabbuh\XApi\Client\XApiClientBuilderInterface;
1314
use Xabbuh\XApi\Client\XApiClientInterface;
@@ -19,15 +20,15 @@ function it_is_an_xapi_client_builder()
1920
$this->shouldHaveType(XApiClientBuilderInterface::class);
2021
}
2122

22-
function it_creates_an_xapi_client(HttpClient $httpClient, RequestFactory $requestFactory)
23+
function it_creates_an_xapi_client(ClientInterface $httpClient, RequestFactoryInterface $requestFactory)
2324
{
2425
$this->setHttpClient($httpClient);
2526
$this->setRequestFactory($requestFactory);
2627
$this->setBaseUrl('http://example.com/xapi/');
2728
$this->build()->shouldHaveType(XApiClientInterface::class);
2829
}
2930

30-
function its_methods_can_be_chained(HttpClient $httpClient, RequestFactory $requestFactory)
31+
function its_methods_can_be_chained(ClientInterface $httpClient, RequestFactoryInterface $requestFactory)
3132
{
3233
$this->setHttpClient($httpClient)->shouldReturn($this);
3334
$this->setRequestFactory($requestFactory)->shouldReturn($this);
@@ -37,10 +38,12 @@ function its_methods_can_be_chained(HttpClient $httpClient, RequestFactory $requ
3738
$this->setOAuthCredentials('consumer key', 'consumer secret', 'token', 'token secret')->shouldReturn($this);
3839
}
3940

40-
function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFactory $requestFactory)
41+
function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFactoryInterface $requestFactory)
4142
{
4243
if ($this->isAbleToDiscoverHttpClient()) {
43-
throw new SkippingException('The builder does not throw an exception if it can automatically discover an HTTP client.');
44+
throw new SkippingException(
45+
'The builder does not throw an exception if it can automatically discover an HTTP client.'
46+
);
4447
}
4548

4649
$this->setRequestFactory($requestFactory);
@@ -49,10 +52,12 @@ function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFact
4952
$this->shouldThrow('\LogicException')->during('build');
5053
}
5154

52-
function it_throws_an_exception_if_the_request_factory_is_not_configured(HttpClient $httpClient)
55+
function it_throws_an_exception_if_the_request_factory_is_not_configured(ClientInterface $httpClient)
5356
{
5457
if ($this->isAbleToDiscoverRequestFactory()) {
55-
throw new SkippingException('The builder does not throw an exception if it can automatically discover a request factory.');
58+
throw new SkippingException(
59+
'The builder does not throw an exception if it can automatically discover a request factory.'
60+
);
5661
}
5762

5863
$this->setHttpClient($httpClient);
@@ -61,10 +66,16 @@ function it_throws_an_exception_if_the_request_factory_is_not_configured(HttpCli
6166
$this->shouldThrow('\LogicException')->during('build');
6267
}
6368

64-
function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and_the_request_factory_without_configuring_them_explicitly()
69+
function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and_the_request_factory_without_configuring_them_explicitly(
70+
)
6571
{
6672
if (!class_exists(HttpClientDiscovery::class)) {
67-
throw new SkippingException(sprintf('The "%s" class is required to let the builder auto discover the HTTP client and request factory.', HttpClientDiscovery::class));
73+
throw new SkippingException(
74+
sprintf(
75+
'The "%s" class is required to let the builder auto discover the HTTP client and request factory.',
76+
HttpClientDiscovery::class
77+
)
78+
);
6879
}
6980

7081
if (!$this->isAbleToDiscoverHttpClient()) {
@@ -80,32 +91,44 @@ function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and
8091
$this->build()->shouldReturnAnInstanceOf(XApiClientInterface::class);
8192
}
8293

83-
function it_throws_an_exception_if_the_base_uri_is_not_configured(HttpClient $httpClient, RequestFactory $requestFactory)
84-
{
94+
function it_throws_an_exception_if_the_base_uri_is_not_configured(
95+
ClientInterface $httpClient,
96+
RequestFactoryInterface $requestFactory
97+
) {
8598
$this->setHttpClient($httpClient);
8699
$this->setRequestFactory($requestFactory);
87100

88101
$this->shouldThrow('\LogicException')->during('build');
89102
}
90103

91-
function it_throws_an_exception_when_oauth_credentials_are_configured_but_the_auth_package_is_missing(HttpClient $httpClient, RequestFactory $requestFactory)
92-
{
104+
function it_throws_an_exception_when_oauth_credentials_are_configured_but_the_auth_package_is_missing(
105+
ClientInterface $httpClient,
106+
RequestFactoryInterface $requestFactory
107+
) {
93108
if (class_exists(OAuth1::class)) {
94-
throw new SkippingException('OAuth1 credentials can be used when the "xabbuh/oauth1-authentication" package is present.');
109+
throw new SkippingException(
110+
'OAuth1 credentials can be used when the "xabbuh/oauth1-authentication" package is present.'
111+
);
95112
}
96113

97114
$this->setHttpClient($httpClient);
98115
$this->setRequestFactory($requestFactory);
99116
$this->setBaseUrl('http://example.com/xapi/');
100117
$this->setOAuthCredentials('consumer_key', 'consumer_secret', 'access_token', 'token_secret');
101118

102-
$this->shouldThrow(new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.'))->during('build');
119+
$this->shouldThrow(
120+
new \LogicException('The "xabbuh/oauth1-authentication package is needed to use OAuth1 authorization.')
121+
)->during('build');
103122
}
104123

105-
function it_accepts_oauth_credentials_when_the_auth_package_is_present(HttpClient $httpClient, RequestFactory $requestFactory)
106-
{
124+
function it_accepts_oauth_credentials_when_the_auth_package_is_present(
125+
ClientInterface $httpClient,
126+
RequestFactoryInterface $requestFactory
127+
) {
107128
if (!class_exists(OAuth1::class)) {
108-
throw new SkippingException('OAuth1 credentials cannot be used when the "xabbuh/oauth1-authentication" package is missing.');
129+
throw new SkippingException(
130+
'OAuth1 credentials cannot be used when the "xabbuh/oauth1-authentication" package is missing.'
131+
);
109132
}
110133

111134
$this->setHttpClient($httpClient);

src/Request/Handler.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
namespace Xabbuh\XApi\Client\Request;
1313

1414
use Http\Client\Exception;
15-
use Http\Client\HttpClient;
1615
use Http\Message\RequestFactory;
1716

17+
use Psr\Http\Client\ClientInterface;
18+
use Psr\Http\Message\RequestFactoryInterface;
1819
use Psr\Http\Message\RequestInterface;
1920
use Xabbuh\XApi\Common\Exception\AccessDeniedException;
2021
use Xabbuh\XApi\Common\Exception\ConflictException;
@@ -34,12 +35,12 @@ final class Handler implements HandlerInterface
3435
private $version;
3536

3637
/**
37-
* @param HttpClient $httpClient The HTTP client sending requests to the remote LRS
38-
* @param RequestFactory $requestFactory The factory used to create PSR-7 HTTP requests
39-
* @param string $baseUri The APIs base URI (all end points will be created relatively to this URI)
40-
* @param string $version The xAPI version
38+
* @param ClientInterface $httpClient The HTTP client sending requests to the remote LRS
39+
* @param RequestFactoryInterface $requestFactory The factory used to create PSR-7 HTTP requests
40+
* @param string $baseUri The APIs base URI (all end points will be created relatively to this URI)
41+
* @param string $version The xAPI version
4142
*/
42-
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory, $baseUri, $version)
43+
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, $baseUri, $version)
4344
{
4445
$this->httpClient = $httpClient;
4546
$this->requestFactory = $requestFactory;

src/XApiClientBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner;
1919
use Http\Client\Common\Plugin\AuthenticationPlugin;
2020
use Http\Client\Common\PluginClient;
21-
use Http\Client\HttpClient;
2221
use Http\Discovery\HttpClientDiscovery;
2322
use Http\Discovery\MessageFactoryDiscovery;
2423
use Http\Message\Authentication\BasicAuth;
25-
use Http\Message\RequestFactory;
24+
use Psr\Http\Client\ClientInterface;
25+
use Psr\Http\Message\RequestFactoryInterface;
2626
use Xabbuh\Http\Authentication\OAuth1;
2727
use Xabbuh\XApi\Client\Request\Handler;
2828
use Xabbuh\XApi\Serializer\SerializerFactoryInterface;
@@ -39,12 +39,12 @@ final class XApiClientBuilder implements XApiClientBuilderInterface
3939
private $serializerFactory;
4040

4141
/**
42-
* @var HttpClient|null
42+
* @var ClientInterface|null
4343
*/
4444
private $httpClient;
4545

4646
/**
47-
* @var RequestFactory|null
47+
* @var RequestFactoryInterface|null
4848
*/
4949
private $requestFactory;
5050

@@ -65,7 +65,7 @@ public function __construct(SerializerFactoryInterface $serializerFactory = null
6565
/**
6666
* {@inheritdoc}
6767
*/
68-
public function setHttpClient(HttpClient $httpClient)
68+
public function setHttpClient(ClientInterface $httpClient)
6969
{
7070
$this->httpClient = $httpClient;
7171

@@ -75,7 +75,7 @@ public function setHttpClient(HttpClient $httpClient)
7575
/**
7676
* {@inheritdoc}
7777
*/
78-
public function setRequestFactory(RequestFactory $requestFactory)
78+
public function setRequestFactory(RequestFactoryInterface $requestFactory)
7979
{
8080
$this->requestFactory = $requestFactory;
8181

0 commit comments

Comments
 (0)