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

Commit 29056cf

Browse files
committed
:octocat: use function/const
1 parent f297af2 commit 29056cf

File tree

7 files changed

+65
-37
lines changed

7 files changed

+65
-37
lines changed

tests/API/APITestAbstract.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
namespace chillerlan\OAuthTest\API;
1414

1515
use chillerlan\DotEnv\DotEnv;
16-
use chillerlan\HTTP\Psr7;
1716
use chillerlan\Settings\SettingsContainerInterface;
18-
use chillerlan\OAuth\{Core\AccessToken, Core\OAuthInterface, OAuthOptions};
17+
use chillerlan\OAuth\{Core\OAuthInterface, OAuthOptions};
1918
use chillerlan\OAuthTest\{OAuthTestHttpClient, OAuthTestLogger, OAuthTestMemoryStorage};
2019
use PHPUnit\Framework\TestCase;
2120
use Psr\Http\Client\ClientInterface;
2221
use Psr\Http\Message\ResponseInterface;
2322
use Psr\Log\LoggerInterface;
2423

24+
use function chillerlan\HTTP\Psr7\{get_json, get_xml};
25+
2526
abstract class APITestAbstract extends TestCase{
2627

2728
/**
@@ -141,7 +142,7 @@ protected function getProvider():OAuthInterface{
141142
protected function responseJson(ResponseInterface $response){
142143
$response->getBody()->rewind();
143144

144-
return Psr7\get_json($response);
145+
return get_json($response);
145146
}
146147

147148
/**
@@ -152,7 +153,7 @@ protected function responseJson(ResponseInterface $response){
152153
protected function responseXML(ResponseInterface $response){
153154
$response->getBody()->rewind();
154155

155-
return Psr7\get_xml($response);
156+
return get_xml($response);
156157
}
157158

158159
public function testOAuthInstance(){

tests/OAuthTestLogger.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Psr\Log\{AbstractLogger, LogLevel};
1616
use Exception;
1717

18+
use function array_key_exists, date, sprintf, str_repeat, str_replace, strtolower, substr, trim, var_export;
19+
1820
class OAuthTestLogger extends AbstractLogger{
1921

2022
protected const E_NONE = 0x00;
@@ -60,9 +62,9 @@ public function __construct(string $loglevel = null){
6062
* @throws \Exception
6163
*/
6264
public function setLoglevel(string $loglevel):void{
63-
$loglevel = \strtolower($loglevel);
65+
$loglevel = strtolower($loglevel);
6466

65-
if(!\array_key_exists($loglevel, $this::LEVELS)){
67+
if(!array_key_exists($loglevel, $this::LEVELS)){
6668
throw new Exception('invalid loglevel');
6769
}
6870

@@ -81,19 +83,21 @@ public function log($level, $message, array $context = []):void{
8183
}
8284

8385
if($this::LEVELS[$level] >= $this::LEVELS[$this->loglevel]){
84-
echo \sprintf(
86+
echo sprintf(
8587
'[%s][%s] %s',
86-
\date('Y-m-d H:i:s'),
87-
\substr($level, 0, 4),
88-
\str_replace("\n", "\n".\str_repeat(' ', 28), \trim($message))
88+
date('Y-m-d H:i:s'),
89+
substr($level, 0, 4),
90+
str_replace("\n", "\n".str_repeat(' ', 28), trim($message))
8991
)."\n";
9092

9193
if(!empty($context)){
92-
$c = "\n".'--- CONTEXT START ---'."\n";
94+
$c = "\n--- CONTEXT START ---\n";
95+
9396
foreach($context as $k => $v){
9497
$c .= '\''.$k.'\' => '.var_export($v, true)."\n";
9598
}
96-
$c .= '--- CONTEXT END ---'."\n\n";
99+
100+
$c .= "--- CONTEXT END ---\n\n";
97101

98102
echo $c;
99103
}

tests/OAuthTestMemoryStorage.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use chillerlan\OAuth\Storage\{MemoryStorage, OAuthStorageException};
1717
use chillerlan\Settings\SettingsContainerInterface;
1818

19+
use function file_exists, file_get_contents, file_put_contents;
20+
1921
class OAuthTestMemoryStorage extends MemoryStorage{
2022

2123
/**
@@ -41,7 +43,7 @@ public function __construct(SettingsContainerInterface $options = null, string $
4143
public function storeAccessToken(string $service, AccessToken $token):bool{
4244
parent::storeAccessToken($service, $token);
4345

44-
if(@\file_put_contents($this->storagepath.'/'.$service.'.token.json', $token->toJSON()) === false){
46+
if(@file_put_contents($this->storagepath.'/'.$service.'.token.json', $token->toJSON()) === false){
4547
throw new OAuthStorageException('unable to access file storage');
4648
}
4749

@@ -58,7 +60,7 @@ public function getAccessToken(string $service):AccessToken{
5860
}
5961

6062
$tokenfile = $this->storagepath.'/'.$service.'.token.json';
61-
if(\file_exists($tokenfile)){
63+
if(file_exists($tokenfile)){
6264
return (new AccessToken)->fromJSON(file_get_contents($tokenfile));
6365
}
6466

tests/OAuthTestSessionStorage.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use chillerlan\OAuth\Storage\{OAuthStorageException, SessionStorage};
1717
use chillerlan\Settings\SettingsContainerInterface;
1818

19+
use function file_exists, file_get_contents, file_put_contents;
20+
1921
class OAuthTestSessionStorage extends SessionStorage{
2022

2123
/**
@@ -41,7 +43,7 @@ public function __construct(SettingsContainerInterface $options = null, string $
4143
public function storeAccessToken(string $service, AccessToken $token):bool{
4244
parent::storeAccessToken($service, $token);
4345

44-
if(@\file_put_contents($this->storagepath.'/'.$service.'.token.json', $token->toJSON()) === false){
46+
if(@file_put_contents($this->storagepath.'/'.$service.'.token.json', $token->toJSON()) === false){
4547
throw new OAuthStorageException('unable to access file storage');
4648
}
4749

@@ -58,7 +60,7 @@ public function getAccessToken(string $service):AccessToken{
5860
}
5961

6062
$tokenfile = $this->storagepath.'/'.$service.'.token.json';
61-
if(\file_exists($tokenfile)){
63+
if(file_exists($tokenfile)){
6264
return (new AccessToken)->fromJSON(file_get_contents($tokenfile));
6365
}
6466

tests/Providers/OAuth1ProviderTestAbstract.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212

1313
namespace chillerlan\OAuthTest\Providers;
1414

15-
use chillerlan\HTTP\{Psr17, Psr7};
1615
use chillerlan\HTTP\Psr7\{Request, Response};
1716
use chillerlan\OAuth\Core\{AccessToken, OAuth1Interface, ProviderException};
1817

18+
use function chillerlan\HTTP\Psr17\create_stream_from_input;
19+
use function chillerlan\HTTP\Psr7\get_json;
20+
21+
use function parse_str, parse_url;
22+
23+
use const PHP_URL_QUERY;
24+
1925
/**
2026
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
2127
*/
@@ -41,7 +47,7 @@ public function testOAuth1Instance(){
4147
}
4248

4349
public function testGetAuthURL(){
44-
\parse_str(\parse_url($this->provider->getAuthURL(), \PHP_URL_QUERY), $query);
50+
parse_str(parse_url($this->provider->getAuthURL(), PHP_URL_QUERY), $query);
4551

4652
$this->assertSame('test_request_token', $query['oauth_token']);
4753
}
@@ -86,7 +92,7 @@ public function testParseTokenResponseErrorException(){
8692

8793
$this
8894
->getMethod('parseTokenResponse')
89-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('error=whatever'))])
95+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('error=whatever'))])
9096
;
9197
}
9298

@@ -96,7 +102,7 @@ public function testParseTokenResponseNoTokenException(){
96102

97103
$this
98104
->getMethod('parseTokenResponse')
99-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('oauth_token=whatever'))])
105+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('oauth_token=whatever'))])
100106
;
101107
}
102108

@@ -106,7 +112,7 @@ public function testParseTokenResponseCallbackUnconfirmedException(){
106112

107113
$this
108114
->getMethod('parseTokenResponse')
109-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('oauth_token=whatever&oauth_token_secret=whatever_secret')), true])
115+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('oauth_token=whatever&oauth_token_secret=whatever_secret')), true])
110116
;
111117
}
112118

@@ -127,7 +133,7 @@ public function testRequest(){
127133
$token = new AccessToken(['accessTokenSecret' => 'test_token']);
128134
$this->storage->storeAccessToken($this->provider->serviceName, $token);
129135

130-
$this->assertSame('such data! much wow!', Psr7\get_json($this->provider->request('/request'))->data);
136+
$this->assertSame('such data! much wow!', get_json($this->provider->request('/request'))->data);
131137

132138
// coverage, @todo
133139
$this->provider->request('/request', null, 'POST', ['foo' => 'bar'], ['Content-Type' => 'application/json']);

tests/Providers/OAuth2ProviderTestAbstract.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212

1313
namespace chillerlan\OAuthTest\Providers;
1414

15-
use chillerlan\HTTP\{Psr17, Psr7};
1615
use chillerlan\HTTP\Psr7\{Request, Response};
1716
use chillerlan\OAuth\Core\{AccessToken, ClientCredentials, CSRFToken, OAuth2Interface, ProviderException, TokenRefresh};
1817
use chillerlan\OAuth\OAuthException;
1918

19+
use function chillerlan\HTTP\Psr17\create_stream_from_input;
20+
use function chillerlan\HTTP\Psr7\get_json;
21+
22+
use function explode, parse_str, parse_url, sleep, time;
23+
24+
use const PHP_URL_QUERY;
25+
2026
/**
2127
* @property \chillerlan\OAuth\Core\OAuth2Interface $provider
2228
*/
@@ -46,19 +52,19 @@ public function testOAuth2Instance(){
4652

4753
public function testGetAuthURL(){
4854
$url = $this->provider->getAuthURL(['client_secret' => 'foo'], ['some_scope']);
49-
\parse_str(\parse_url($url, \PHP_URL_QUERY), $query);
55+
parse_str(parse_url($url, PHP_URL_QUERY), $query);
5056

5157
$this->assertArrayNotHasKey('client_secret', $query);
5258
$this->assertSame($this->options->key, $query['client_id']);
5359
$this->assertSame('code', $query['response_type']);
54-
$this->assertSame(\explode('?', $url)[0], $this->getProperty('authURL')->getValue($this->provider));
60+
$this->assertSame(explode('?', $url)[0], $this->getProperty('authURL')->getValue($this->provider));
5561
}
5662

5763
public function testGetAccessToken(){
5864
$token = $this->provider->getAccessToken('foo', 'test_state');
5965

6066
$this->assertSame('test_access_token', $token->accessToken);
61-
$this->assertGreaterThan(\time(), $token->expires);
67+
$this->assertGreaterThan(time(), $token->expires);
6268
}
6369

6470
public function testParseTokenResponseNoDataException(){
@@ -67,7 +73,7 @@ public function testParseTokenResponseNoDataException(){
6773

6874
$this
6975
->getMethod('parseTokenResponse')
70-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input(''))])
76+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input(''))])
7177
;
7278
}
7379

@@ -77,7 +83,7 @@ public function testParseTokenResponseErrorException(){
7783

7884
$this
7985
->getMethod('parseTokenResponse')
80-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('{"error":"whatever"}'))])
86+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('{"error":"whatever"}'))])
8187
;
8288
}
8389

@@ -87,7 +93,7 @@ public function testParseTokenResponseNoTokenException(){
8793

8894
$this
8995
->getMethod('parseTokenResponse')
90-
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('{"foo":"bar"}'))])
96+
->invokeArgs($this->provider, [(new Response)->withBody(create_stream_from_input('{"foo":"bar"}'))])
9197
;
9298
}
9399

@@ -99,11 +105,17 @@ public function testGetRequestAuthorization(){
99105

100106
// header (default)
101107
if(isset(OAuth2Interface::AUTH_METHODS_HEADER[$authMethod])){
102-
$this->assertStringContainsString(OAuth2Interface::AUTH_METHODS_HEADER[$authMethod].'test_token', $this->provider->getRequestAuthorization($request, $token)->getHeaderLine('Authorization'));
108+
$this->assertStringContainsString(
109+
OAuth2Interface::AUTH_METHODS_HEADER[$authMethod].'test_token',
110+
$this->provider->getRequestAuthorization($request, $token)->getHeaderLine('Authorization')
111+
);
103112
}
104113
// query
105114
elseif(isset(OAuth2Interface::AUTH_METHODS_QUERY[$authMethod])){
106-
$this->assertStringContainsString(OAuth2Interface::AUTH_METHODS_QUERY[$authMethod].'=test_token', $this->provider->getRequestAuthorization($request, $token)->getUri()->getQuery());
115+
$this->assertStringContainsString(
116+
OAuth2Interface::AUTH_METHODS_QUERY[$authMethod].'=test_token',
117+
$this->provider->getRequestAuthorization($request, $token)->getUri()->getQuery()
118+
);
107119
}
108120

109121
}
@@ -112,7 +124,7 @@ public function testRequest(){
112124
$token = new AccessToken(['accessToken' => 'test_access_token_secret', 'expires' => 1]);
113125
$this->storage->storeAccessToken($this->provider->serviceName, $token);
114126

115-
$this->assertSame('such data! much wow!', Psr7\get_json($this->provider->request('/request'))->data);
127+
$this->assertSame('such data! much wow!', get_json($this->provider->request('/request'))->data);
116128
}
117129

118130
public function testRequestInvalidAuthTypeException(){
@@ -198,7 +210,7 @@ public function testRefreshAccessToken(){
198210

199211
$this->assertSame('test_refresh_token', $token->refreshToken);
200212
$this->assertSame('test_refreshed_access_token', $token->accessToken);
201-
$this->assertGreaterThan(\time(), $token->expires);
213+
$this->assertGreaterThan(time(), $token->expires);
202214
}
203215

204216
public function testRequestWithTokenRefresh(){
@@ -211,9 +223,9 @@ public function testRequestWithTokenRefresh(){
211223
$token = new AccessToken(['accessToken' => 'test_access_token', 'refreshToken' => 'test_refresh_token', 'expires' => 1]);
212224
$this->storage->storeAccessToken($this->provider->serviceName, $token);
213225

214-
\sleep(2);
226+
sleep(2);
215227

216-
$this->assertSame('such data! much wow!', Psr7\get_json($this->provider->request('/request'))->data);
228+
$this->assertSame('such data! much wow!', get_json($this->provider->request('/request'))->data);
217229
}
218230

219231
public function testGetClientCredentials(){
@@ -226,7 +238,7 @@ public function testGetClientCredentials(){
226238
$token = $this->provider->getClientCredentialsToken(['some_scope']);
227239

228240
$this->assertSame('test_client_credentials_token', $token->accessToken);
229-
$this->assertGreaterThan(\time(), $token->expires);
241+
$this->assertGreaterThan(time(), $token->expires);
230242
}
231243

232244
}

tests/Providers/ProviderTestAbstract.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use ReflectionClass, ReflectionMethod, ReflectionProperty;
2525

2626
use function chillerlan\HTTP\Psr17\create_stream_from_input;
27+
use function file_exists;
2728

2829
abstract class ProviderTestAbstract extends TestCase{
2930

@@ -79,7 +80,7 @@ abstract class ProviderTestAbstract extends TestCase{
7980

8081
protected function setUp():void{
8182

82-
$file = \file_exists($this->CFG.'/.env') ? '.env' : '.env_travis';
83+
$file = file_exists($this->CFG.'/.env') ? '.env' : '.env_travis';
8384

8485
$this->dotEnv = (new DotEnv($this->CFG, $file))->load();
8586
$this->is_ci = $this->dotEnv->get('IS_CI') === 'TRUE';

0 commit comments

Comments
 (0)