Skip to content

Commit 0991db4

Browse files
authored
Replaced php-http/message-factory with psr/http-factory (#2)
Replaced php-http/message-factory with psr/http-factory
1 parent 3fa8680 commit 0991db4

File tree

7 files changed

+113
-63
lines changed

7 files changed

+113
-63
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: 'echo "::set-output name=cache-dir::$(composer config cache-files-dir)"'
3434

3535
- name: 'Cache dependencies'
36-
uses: 'actions/cache@v2'
36+
uses: 'actions/cache@v4'
3737
with:
3838
path: '${{ steps.composer-cache.outputs.cache-dir }}'
3939
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"php-http/client-implementation": "^1.0",
1818
"php-http/httplug": "^1.0 || ^2.0",
1919
"php-http/message": "^1.0",
20-
"php-http/message-factory": "^1.0",
2120
"php-xapi/exception": "^1.0",
2221
"php-xapi/model": "^4.0",
2322
"php-xapi/serializer": "^4.0",
2423
"php-xapi/symfony-serializer": "^3.0",
25-
"psr/http-message": "^1.0"
24+
"psr/http-message": "^1.0",
25+
"psr/http-factory": "^1.0"
2626
},
2727
"require-dev": {
2828
"phpspec/phpspec": "^6.0 || ^7.0",

spec/Request/HandlerSpec.php

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

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

5-
use Http\Client\HttpClient;
6-
use Http\Message\RequestFactory;
75
use PhpSpec\ObjectBehavior;
6+
use Psr\Http\Client\ClientInterface;
7+
use Psr\Http\Message\RequestFactoryInterface;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use Xabbuh\XApi\Common\Exception\AccessDeniedException;
@@ -14,7 +14,7 @@
1414

1515
class HandlerSpec extends ObjectBehavior
1616
{
17-
function let(HttpClient $client, RequestFactory $requestFactory)
17+
function let(ClientInterface $client, RequestFactoryInterface $requestFactory)
1818
{
1919
$this->beConstructedWith($client, $requestFactory, 'http://example.com/xapi/', '1.0.1');
2020
}
@@ -24,8 +24,10 @@ function it_throws_an_exception_if_a_request_is_created_with_an_invalid_method()
2424
$this->shouldThrow('\InvalidArgumentException')->during('createRequest', array('options', '/xapi/statements'));
2525
}
2626

27-
function it_returns_get_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
28-
{
27+
function it_returns_get_request_created_by_the_http_client(
28+
RequestFactoryInterface $requestFactory,
29+
RequestInterface $request
30+
) {
2931
$requestFactory->createRequest('GET', 'http://example.com/xapi/statements', array(
3032
'X-Experience-API-Version' => '1.0.1',
3133
'Content-Type' => 'application/json',
@@ -35,8 +37,10 @@ function it_returns_get_request_created_by_the_http_client(RequestFactory $reque
3537
$this->createRequest('GET', '/statements')->shouldReturn($request);
3638
}
3739

38-
function it_returns_post_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
39-
{
40+
function it_returns_post_request_created_by_the_http_client(
41+
RequestFactoryInterface $requestFactory,
42+
RequestInterface $request
43+
) {
4044
$requestFactory->createRequest('POST', 'http://example.com/xapi/statements', array(
4145
'X-Experience-API-Version' => '1.0.1',
4246
'Content-Type' => 'application/json',
@@ -46,8 +50,10 @@ function it_returns_post_request_created_by_the_http_client(RequestFactory $requ
4650
$this->createRequest('POST', '/statements', array(), 'body')->shouldReturn($request);
4751
}
4852

49-
function it_returns_put_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
50-
{
53+
function it_returns_put_request_created_by_the_http_client(
54+
RequestFactoryInterface $requestFactory,
55+
RequestInterface $request
56+
) {
5157
$requestFactory->createRequest('PUT', 'http://example.com/xapi/statements', array(
5258
'X-Experience-API-Version' => '1.0.1',
5359
'Content-Type' => 'application/json',
@@ -57,8 +63,10 @@ function it_returns_put_request_created_by_the_http_client(RequestFactory $reque
5763
$this->createRequest('PUT', '/statements', array(), 'body')->shouldReturn($request);
5864
}
5965

60-
function it_returns_delete_request_created_by_the_http_client(RequestFactory $requestFactory, RequestInterface $request)
61-
{
66+
function it_returns_delete_request_created_by_the_http_client(
67+
RequestFactoryInterface $requestFactory,
68+
RequestInterface $request
69+
) {
6270
$requestFactory->createRequest('DELETE', 'http://example.com/xapi/statements', array(
6371
'X-Experience-API-Version' => '1.0.1',
6472
'Content-Type' => 'application/json',
@@ -68,53 +76,71 @@ function it_returns_delete_request_created_by_the_http_client(RequestFactory $re
6876
$this->createRequest('DELETE', '/statements')->shouldReturn($request);
6977
}
7078

71-
function it_throws_an_access_denied_exception_when_a_401_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
72-
{
79+
function it_throws_an_access_denied_exception_when_a_401_status_code_is_returned(
80+
ClientInterface $client,
81+
RequestInterface $request,
82+
ResponseInterface $response
83+
) {
7384
$client->sendRequest($request)->willReturn($response);
7485
$response->getStatusCode()->willReturn(401);
7586
$response->getBody()->willReturn('body');
7687

7788
$this->shouldThrow(AccessDeniedException::class)->during('executeRequest', array($request, array(200)));
7889
}
7990

80-
function it_throws_an_access_denied_exception_when_a_403_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
81-
{
91+
function it_throws_an_access_denied_exception_when_a_403_status_code_is_returned(
92+
ClientInterface $client,
93+
RequestInterface $request,
94+
ResponseInterface $response
95+
) {
8296
$client->sendRequest($request)->willReturn($response);
8397
$response->getStatusCode()->willReturn(403);
8498
$response->getBody()->willReturn('body');
8599

86100
$this->shouldThrow(AccessDeniedException::class)->during('executeRequest', array($request, array(200)));
87101
}
88102

89-
function it_throws_a_not_found_exception_when_a_404_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
90-
{
103+
function it_throws_a_not_found_exception_when_a_404_status_code_is_returned(
104+
ClientInterface $client,
105+
RequestInterface $request,
106+
ResponseInterface $response
107+
) {
91108
$client->sendRequest($request)->willReturn($response);
92109
$response->getStatusCode()->willReturn(404);
93110
$response->getBody()->willReturn('body');
94111

95112
$this->shouldThrow(NotFoundException::class)->during('executeRequest', array($request, array(200)));
96113
}
97114

98-
function it_throws_a_conflict_exception_when_a_409_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
99-
{
115+
function it_throws_a_conflict_exception_when_a_409_status_code_is_returned(
116+
ClientInterface $client,
117+
RequestInterface $request,
118+
ResponseInterface $response
119+
) {
100120
$client->sendRequest($request)->willReturn($response);
101121
$response->getStatusCode()->willReturn(409);
102122
$response->getBody()->willReturn('body');
103123

104124
$this->shouldThrow(ConflictException::class)->during('executeRequest', array($request, array(200)));
105125
}
106126

107-
function it_throws_an_xapi_exception_when_an_unexpected_status_code_is_returned(HttpClient $client, RequestInterface $request, ResponseInterface $response)
108-
{
127+
function it_throws_an_xapi_exception_when_an_unexpected_status_code_is_returned(
128+
ClientInterface $client,
129+
RequestInterface $request,
130+
ResponseInterface $response
131+
) {
109132
$client->sendRequest($request)->willReturn($response);
110133
$response->getStatusCode()->willReturn(204);
111134
$response->getBody()->willReturn('body');
112135

113136
$this->shouldThrow(XApiException::class)->during('executeRequest', array($request, array(200)));
114137
}
115138

116-
function it_returns_the_response_on_success(HttpClient $client, RequestInterface $request, ResponseInterface $response)
117-
{
139+
function it_returns_the_response_on_success(
140+
ClientInterface $client,
141+
RequestInterface $request,
142+
ResponseInterface $response
143+
) {
118144
$client->sendRequest($request)->willReturn($response);
119145
$response->getStatusCode()->willReturn(200);
120146
$response->getBody()->willReturn('body');

spec/XApiClientBuilderSpec.php

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

33
namespace spec\Xabbuh\XApi\Client;
44

5-
use Http\Client\HttpClient;
65
use Http\Discovery\HttpClientDiscovery;
76
use Http\Discovery\MessageFactoryDiscovery;
8-
use Http\Message\RequestFactory;
97
use PhpSpec\Exception\Example\SkippingException;
108
use PhpSpec\ObjectBehavior;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\RequestFactoryInterface;
1111
use Xabbuh\Http\Authentication\OAuth1;
1212
use Xabbuh\XApi\Client\XApiClientBuilderInterface;
1313
use Xabbuh\XApi\Client\XApiClientInterface;
@@ -19,15 +19,15 @@ function it_is_an_xapi_client_builder()
1919
$this->shouldHaveType(XApiClientBuilderInterface::class);
2020
}
2121

22-
function it_creates_an_xapi_client(HttpClient $httpClient, RequestFactory $requestFactory)
22+
function it_creates_an_xapi_client(ClientInterface $httpClient, RequestFactoryInterface $requestFactory)
2323
{
2424
$this->setHttpClient($httpClient);
2525
$this->setRequestFactory($requestFactory);
2626
$this->setBaseUrl('http://example.com/xapi/');
2727
$this->build()->shouldHaveType(XApiClientInterface::class);
2828
}
2929

30-
function its_methods_can_be_chained(HttpClient $httpClient, RequestFactory $requestFactory)
30+
function its_methods_can_be_chained(ClientInterface $httpClient, RequestFactoryInterface $requestFactory)
3131
{
3232
$this->setHttpClient($httpClient)->shouldReturn($this);
3333
$this->setRequestFactory($requestFactory)->shouldReturn($this);
@@ -37,10 +37,12 @@ function its_methods_can_be_chained(HttpClient $httpClient, RequestFactory $requ
3737
$this->setOAuthCredentials('consumer key', 'consumer secret', 'token', 'token secret')->shouldReturn($this);
3838
}
3939

40-
function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFactory $requestFactory)
40+
function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFactoryInterface $requestFactory)
4141
{
4242
if ($this->isAbleToDiscoverHttpClient()) {
43-
throw new SkippingException('The builder does not throw an exception if it can automatically discover an HTTP client.');
43+
throw new SkippingException(
44+
'The builder does not throw an exception if it can automatically discover an HTTP client.'
45+
);
4446
}
4547

4648
$this->setRequestFactory($requestFactory);
@@ -49,10 +51,12 @@ function it_throws_an_exception_if_the_http_client_is_not_configured(RequestFact
4951
$this->shouldThrow('\LogicException')->during('build');
5052
}
5153

52-
function it_throws_an_exception_if_the_request_factory_is_not_configured(HttpClient $httpClient)
54+
function it_throws_an_exception_if_the_request_factory_is_not_configured(ClientInterface $httpClient)
5355
{
5456
if ($this->isAbleToDiscoverRequestFactory()) {
55-
throw new SkippingException('The builder does not throw an exception if it can automatically discover a request factory.');
57+
throw new SkippingException(
58+
'The builder does not throw an exception if it can automatically discover a request factory.'
59+
);
5660
}
5761

5862
$this->setHttpClient($httpClient);
@@ -61,10 +65,16 @@ function it_throws_an_exception_if_the_request_factory_is_not_configured(HttpCli
6165
$this->shouldThrow('\LogicException')->during('build');
6266
}
6367

64-
function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and_the_request_factory_without_configuring_them_explicitly()
68+
function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and_the_request_factory_without_configuring_them_explicitly(
69+
)
6570
{
6671
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));
72+
throw new SkippingException(
73+
sprintf(
74+
'The "%s" class is required to let the builder auto discover the HTTP client and request factory.',
75+
HttpClientDiscovery::class
76+
)
77+
);
6878
}
6979

7080
if (!$this->isAbleToDiscoverHttpClient()) {
@@ -80,32 +90,44 @@ function it_can_build_the_client_when_it_is_able_to_discover_the_http_client_and
8090
$this->build()->shouldReturnAnInstanceOf(XApiClientInterface::class);
8191
}
8292

83-
function it_throws_an_exception_if_the_base_uri_is_not_configured(HttpClient $httpClient, RequestFactory $requestFactory)
84-
{
93+
function it_throws_an_exception_if_the_base_uri_is_not_configured(
94+
ClientInterface $httpClient,
95+
RequestFactoryInterface $requestFactory
96+
) {
8597
$this->setHttpClient($httpClient);
8698
$this->setRequestFactory($requestFactory);
8799

88100
$this->shouldThrow('\LogicException')->during('build');
89101
}
90102

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

97113
$this->setHttpClient($httpClient);
98114
$this->setRequestFactory($requestFactory);
99115
$this->setBaseUrl('http://example.com/xapi/');
100116
$this->setOAuthCredentials('consumer_key', 'consumer_secret', 'access_token', 'token_secret');
101117

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

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

111133
$this->setHttpClient($httpClient);

src/Request/Handler.php

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

1414
use Http\Client\Exception;
15-
use Http\Client\HttpClient;
1615
use Http\Message\RequestFactory;
16+
17+
use Psr\Http\Client\ClientInterface;
18+
use Psr\Http\Message\RequestFactoryInterface;
1719
use Psr\Http\Message\RequestInterface;
1820
use Xabbuh\XApi\Common\Exception\AccessDeniedException;
1921
use Xabbuh\XApi\Common\Exception\ConflictException;
@@ -33,12 +35,12 @@ final class Handler implements HandlerInterface
3335
private $version;
3436

3537
/**
36-
* @param HttpClient $httpClient The HTTP client sending requests to the remote LRS
37-
* @param RequestFactory $requestFactory The factory used to create PSR-7 HTTP requests
38-
* @param string $baseUri The APIs base URI (all end points will be created relatively to this URI)
39-
* @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
4042
*/
41-
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory, $baseUri, $version)
43+
public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, $baseUri, $version)
4244
{
4345
$this->httpClient = $httpClient;
4446
$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)