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

Commit f297af2

Browse files
committed
:octocat: clean up provider tests
1 parent 4792a10 commit f297af2

File tree

4 files changed

+34
-106
lines changed

4 files changed

+34
-106
lines changed

tests/Providers/OAuth1ProviderTestAbstract.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use chillerlan\HTTP\{Psr17, Psr7};
1616
use chillerlan\HTTP\Psr7\{Request, Response};
1717
use chillerlan\OAuth\Core\{AccessToken, OAuth1Interface, ProviderException};
18-
use Psr\Http\Client\ClientInterface;
19-
use Psr\Http\Message\{RequestInterface, ResponseInterface};
2018

2119
/**
2220
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
@@ -38,21 +36,6 @@ protected function setUp():void{
3836

3937
}
4038

41-
/**
42-
* @return \Psr\Http\Client\ClientInterface
43-
*/
44-
protected function initHttp():ClientInterface{
45-
return new class($this->responses, $this->logger) extends ProviderTestHttpClient{
46-
47-
public function sendRequest(RequestInterface $request):ResponseInterface{
48-
$stream = Psr17\create_stream_from_input($this->responses[$request->getUri()->getPath()]);
49-
50-
return $this->logRequest($request, (new Response)->withBody($stream));
51-
}
52-
53-
};
54-
}
55-
5639
public function testOAuth1Instance(){
5740
$this->assertInstanceOf(OAuth1Interface::class, $this->provider);
5841
}

tests/Providers/OAuth2ProviderTestAbstract.php

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,28 @@
1616
use chillerlan\HTTP\Psr7\{Request, Response};
1717
use chillerlan\OAuth\Core\{AccessToken, ClientCredentials, CSRFToken, OAuth2Interface, ProviderException, TokenRefresh};
1818
use chillerlan\OAuth\OAuthException;
19-
use Psr\Http\Client\ClientInterface;
20-
use Psr\Http\Message\{RequestInterface, ResponseInterface};
2119

2220
/**
2321
* @property \chillerlan\OAuth\Core\OAuth2Interface $provider
2422
*/
2523
abstract class OAuth2ProviderTestAbstract extends ProviderTestAbstract{
2624

2725
protected $responses = [
28-
'/oauth2/access_token' => [
29-
'access_token' => 'test_access_token',
30-
'expires_in' => 3600,
31-
'state' => 'test_state',
32-
],
33-
'/oauth2/refresh_token' => [
34-
'access_token' => 'test_refreshed_access_token',
35-
'expires_in' => 60,
36-
'state' => 'test_state',
37-
],
38-
'/oauth2/client_credentials' => [
39-
'access_token' => 'test_client_credentials_token',
40-
'expires_in' => 30,
41-
'state' => 'test_state',
42-
],
43-
'/oauth2/api/request' => [
44-
'data' => 'such data! much wow!'
45-
]
26+
'/oauth2/access_token' => '{"access_token":"test_access_token","expires_in":3600,"state":"test_state"}',
27+
'/oauth2/refresh_token' => '{"access_token":"test_refreshed_access_token","expires_in":60,"state":"test_state"}',
28+
'/oauth2/client_credentials' => '{"access_token":"test_client_credentials_token","expires_in":30,"state":"test_state"}',
29+
'/oauth2/api/request' => '{"data":"such data! much wow!"}',
4630
];
4731

4832
protected function setUp():void{
4933
parent::setUp();
5034

5135
$this->setProperty($this->provider, 'apiURL', 'https://localhost/oauth2/api');
5236
$this->setProperty($this->provider, 'accessTokenURL', 'https://localhost/oauth2/access_token');
37+
$this->setProperty($this->provider, 'refreshTokenURL', 'https://localhost/oauth2/refresh_token');
38+
$this->setProperty($this->provider, 'clientCredentialsTokenURL', 'https://localhost/oauth2/client_credentials');
5339

54-
if($this->provider instanceof TokenRefresh){
55-
$this->setProperty($this->provider, 'refreshTokenURL', 'https://localhost/oauth2/refresh_token');
56-
}
57-
58-
if($this->provider instanceof ClientCredentials){
59-
$this->setProperty($this->provider, 'clientCredentialsTokenURL', 'https://localhost/oauth2/client_credentials');
60-
}
61-
62-
if($this->provider instanceof CSRFToken){
63-
$this->storage->storeCSRFState($this->provider->serviceName, 'test_state');
64-
}
65-
66-
}
67-
68-
/**
69-
* @return \Psr\Http\Client\ClientInterface
70-
*/
71-
protected function initHttp():ClientInterface{
72-
return new class($this->responses, $this->logger) extends ProviderTestHttpClient{
73-
74-
public function sendRequest(RequestInterface $request):ResponseInterface{
75-
$stream = Psr17\create_stream_from_input(json_encode($this->responses[$request->getUri()->getPath()]));
76-
77-
return $this->logRequest($request, (new Response)->withBody($stream));
78-
}
79-
80-
};
40+
$this->storage->storeCSRFState($this->provider->serviceName, 'test_state');
8141
}
8242

8343
public function testOAuth2Instance(){

tests/Providers/ProviderTestAbstract.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
namespace chillerlan\OAuthTest\Providers;
1414

1515
use chillerlan\DotEnv\DotEnv;
16+
use chillerlan\HTTP\Psr18\LoggingClient;
17+
use chillerlan\HTTP\Psr7\Response;
1618
use chillerlan\OAuth\{OAuthOptions, Storage\MemoryStorage};
1719
use chillerlan\OAuth\Core\{AccessToken, OAuthInterface};
1820
use chillerlan\OAuthTest\OAuthTestLogger;
1921
use PHPUnit\Framework\TestCase;
2022
use Psr\Http\Client\ClientInterface;
23+
use Psr\Http\Message\{RequestInterface, ResponseInterface};
2124
use ReflectionClass, ReflectionMethod, ReflectionProperty;
2225

26+
use function chillerlan\HTTP\Psr17\create_stream_from_input;
27+
2328
abstract class ProviderTestAbstract extends TestCase{
2429

2530
/**
@@ -96,7 +101,28 @@ protected function setUp():void{
96101
/**
97102
* @return \Psr\Http\Client\ClientInterface
98103
*/
99-
abstract protected function initHttp():ClientInterface;
104+
protected function initHttp():ClientInterface{
105+
106+
$client = new class($this->responses) implements ClientInterface{
107+
108+
/** @var array */
109+
protected $responses;
110+
111+
public function __construct(array $responses){
112+
$this->responses = $responses;
113+
}
114+
115+
public function sendRequest(RequestInterface $request):ResponseInterface{
116+
$stream = create_stream_from_input($this->responses[$request->getUri()->getPath()]);
117+
118+
return (new Response)->withBody($stream);
119+
}
120+
121+
};
122+
123+
return new LoggingClient($client, $this->logger);
124+
}
125+
100126

101127
/**
102128
* @return \chillerlan\OAuth\Core\OAuthInterface

tests/Providers/ProviderTestHttpClient.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)