Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit e6b13aa

Browse files
committed
🚿 tests clean-up
1 parent 3ec2ddb commit e6b13aa

File tree

4 files changed

+29
-61
lines changed

4 files changed

+29
-61
lines changed

tests/Deezer/DeezerTest.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212

1313
namespace chillerlan\OAuthTest\Providers;
1414

15-
use chillerlan\HTTP\{Psr17, Psr7\Response};
15+
use chillerlan\HTTP\Psr7\Response;
1616
use chillerlan\OAuth\Core\{AccessToken, ProviderException};
1717
use chillerlan\OAuth\Providers\Deezer\Deezer;
18-
use Psr\Http\Client\ClientInterface;
19-
use Psr\Http\Message\{RequestInterface, ResponseInterface};
20-
use ReflectionClass;
18+
19+
use function chillerlan\HTTP\Psr17\create_stream_from_input;
2120

2221
/**
2322
* @property \chillerlan\OAuth\Providers\Deezer\Deezer $provider
@@ -26,21 +25,12 @@ class DeezerTest extends OAuth2ProviderTestAbstract{
2625

2726
protected $FQN = Deezer::class;
2827

29-
protected function initHttp():ClientInterface{
30-
return new class($this->responses, $this->logger) extends ProviderTestHttpClient{
31-
32-
public function sendRequest(RequestInterface $request):ResponseInterface{
33-
$path = $request->getUri()->getPath();
34-
$body = $this->responses[$path];
35-
36-
$body = $path === '/oauth2/api/request'
37-
? json_encode($body)
38-
: http_build_query($body);
28+
protected function setUp():void{
3929

40-
return $this->logRequest($request, (new Response)->withBody(Psr17\create_stream_from_input($body)));
41-
}
30+
$this->responses['/oauth2/access_token'] = 'access_token=test_access_token&expires_in=3600&state=test_state';
4231

43-
};
32+
// setup after adding responses -> ProviderTestAbstract::initHTTP()
33+
parent::setUp();
4434
}
4535

4636
public function testGetAuthURL(){
@@ -57,7 +47,7 @@ public function testGetAuthURL(){
5747
public function testParseTokenResponse(){
5848
$token = $this
5949
->getMethod('parseTokenResponse')
60-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input(http_build_query(['access_token' => 'whatever'])))]);
50+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('access_token=whatever'))]);
6151

6252
$this->assertInstanceOf(AccessToken::class, $token);
6353
$this->assertSame('whatever', $token->accessToken);
@@ -69,7 +59,7 @@ public function testParseTokenResponseErrorException(){
6959

7060
$this
7161
->getMethod('parseTokenResponse')
72-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input(http_build_query(['error_reason' => 'whatever'])))]);
62+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('error_reason=whatever'))]);
7363
}
7464

7565
}

tests/GuildWars2/GuildWars2Test.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ class GuildWars2Test extends OAuth2ProviderTest{
2525

2626
public function setUp():void{
2727

28-
$this->responses['/gw2/auth/tokeninfo'] = [
29-
'id' => '00000000-1111-2222-3333-444444444444',
30-
'name' => 'GW2Token',
31-
'permissions' => ['foo', 'bar'],
32-
];
28+
$this->responses['/gw2/auth/tokeninfo'] = '{"id":"00000000-1111-2222-3333-444444444444","name":"GW2Token","permissions":["foo","bar"]}';
3329

30+
// setup after adding responses -> ProviderTestAbstract::initHTTP()
3431
parent::setUp();
3532
}
3633

tests/LastFM/LastFMTest.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212

1313
namespace chillerlan\OAuthTest\Providers\LastFM;
1414

15-
use chillerlan\OAuthTest\Providers\ProviderTestHttpClient;
16-
use chillerlan\HTTP\{Psr17, Psr7};
1715
use chillerlan\HTTP\Psr7\Response;
1816
use chillerlan\OAuth\Core\ProviderException;
1917
use chillerlan\OAuth\Providers\LastFM\LastFM;
2018
use chillerlan\OAuthTest\Providers\ProviderTestAbstract;
21-
use Psr\Http\Client\ClientInterface;
22-
use Psr\Http\Message\{RequestInterface, ResponseInterface};
19+
20+
use function chillerlan\HTTP\Psr17\create_stream;
21+
use function chillerlan\HTTP\Psr7\get_json;
2322

2423
/**
2524
* @property \chillerlan\OAuth\Providers\LastFM\LastFM $provider
@@ -30,31 +29,15 @@ class LastFMTest extends ProviderTestAbstract{
3029

3130
public function setUp():void{
3231

33-
$this->responses['/lastfm/auth'] = [
34-
'session' => ['key' => 'session_key'],
35-
];
36-
37-
$this->responses['/lastfm/api/request'] = [
38-
'data' => 'such data! much wow!',
39-
];
32+
$this->responses['/lastfm/auth'] = '{"session":{"key":"session_key"}}';
33+
$this->responses['/lastfm/api/request'] = '{"data":"such data! much wow!"}';
4034

35+
// setup after adding responses -> ProviderTestAbstract::initHTTP()
4136
parent::setUp();
4237

4338
$this->setProperty($this->provider, 'apiURL', '/lastfm/api/request');
4439
}
4540

46-
protected function initHttp():ClientInterface{
47-
return new class($this->responses, $this->logger) extends ProviderTestHttpClient{
48-
49-
public function sendRequest(RequestInterface $request):ResponseInterface{
50-
$stream = Psr17\create_stream_from_input(json_encode($this->responses[$request->getUri()->getPath()]));
51-
52-
return $this->logRequest($request, (new Response)->withBody($stream));
53-
}
54-
55-
};
56-
}
57-
5841
public function testGetAuthURL(){
5942
$url = $this->provider->getAuthURL(['foo' => 'bar']);
6043

@@ -70,7 +53,7 @@ public function testGetSignature(){
7053
}
7154

7255
public function testParseTokenResponse(){
73-
$r = (new Response)->withBody(Psr17\create_stream(json_encode(['session' => ['key' => 'whatever']])));
56+
$r = (new Response)->withBody(create_stream('{"session":{"key":"whatever"}}'));
7457

7558
$token = $this
7659
->getMethod('parseTokenResponse')
@@ -92,7 +75,8 @@ public function testParseTokenResponseError(){
9275
$this->expectException(ProviderException::class);
9376
$this->expectExceptionMessage('error retrieving access token:');
9477

95-
$r = (new Response)->withBody(Psr17\create_stream(json_encode(['error' => 42, 'message' => 'whatever'])));
78+
$r = (new Response)->withBody(create_stream('{"error":42,"message":"whatever"}'));
79+
9680
$this
9781
->getMethod('parseTokenResponse')
9882
->invokeArgs($this->provider, [$r]);
@@ -102,7 +86,8 @@ public function testParseTokenResponseNoToken(){
10286
$this->expectException(ProviderException::class);
10387
$this->expectExceptionMessage('token missing');
10488

105-
$r = (new Response)->withBody(Psr17\create_stream(json_encode(['session' => []])));
89+
$r = (new Response)->withBody(create_stream('{"session":[]}'));
90+
10691
$this
10792
->getMethod('parseTokenResponse')
10893
->invokeArgs($this->provider, [$r]);
@@ -131,14 +116,14 @@ public function testGetAccessToken(){
131116
public function testRequest(){
132117
$r = $this->provider->request('');
133118

134-
$this->assertSame('such data! much wow!', Psr7\get_json($r)->data);
119+
$this->assertSame('such data! much wow!', get_json($r)->data);
135120
}
136121

137122
// coverage
138123
public function testRequestPost(){
139124
$r = $this->provider->request('', [], 'POST', ['foo' => 'bar'], ['Content-Type' => 'whatever']);
140125

141-
$this->assertSame('such data! much wow!', Psr7\get_json($r)->data);
126+
$this->assertSame('such data! much wow!', get_json($r)->data);
142127
}
143128

144129
}

tests/MailChimp/MailChimpTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
namespace chillerlan\OAuthTest\Providers\MailChimp;
1414

15-
use chillerlan\HTTP\Psr7;
1615
use chillerlan\OAuth\Core\AccessToken;
1716
use chillerlan\OAuth\OAuthException;
1817
use chillerlan\OAuth\Providers\MailChimp\MailChimp;
1918
use chillerlan\OAuthTest\Providers\OAuth2ProviderTest;
2019

20+
use function chillerlan\HTTP\Psr7\get_json;
21+
2122
/**
2223
* @property \chillerlan\OAuth\Providers\MailChimp\MailChimp $provider
2324
*/
@@ -29,13 +30,8 @@ class MailChimpTest extends OAuth2ProviderTest{
2930

3031
public function setUp():void{
3132

32-
$this->responses['/3.0'] = [
33-
'data' => 'such data! much wow! (/3.0)'
34-
];
35-
36-
$this->responses['/oauth2/metadata'] = [
37-
'metadata' => 'whatever'
38-
];
33+
$this->responses['/3.0'] = '{"data":"such data! much wow! (/3.0)"}';
34+
$this->responses['/oauth2/metadata'] = '{"metadata":"whatever"}';
3935

4036
// setup after adding responses -> ProviderTestAbstract::initHTTP()
4137
parent::setUp();
@@ -46,7 +42,7 @@ public function setUp():void{
4642
public function testRequest(){
4743
$this->storage->storeAccessToken($this->provider->serviceName, $this->token);
4844

49-
$this->assertSame('such data! much wow! (/3.0)', Psr7\get_json($this->provider->request(''))->data);
45+
$this->assertSame('such data! much wow! (/3.0)', get_json($this->provider->request(''))->data);
5046
}
5147

5248
public function testRequestInvalidAuthTypeException(){

0 commit comments

Comments
 (0)