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

Commit cf2a161

Browse files
committed
🛀
1 parent f8266cb commit cf2a161

File tree

5 files changed

+190
-85
lines changed

5 files changed

+190
-85
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"require-dev": {
3131
"chillerlan/php-curl": "^1.0",
3232
"chillerlan/php-database": "^2.0",
33+
"chillerlan/php-log": "^1.0",
3334
"phpunit/phpunit": "^7.1"
3435
},
3536
"autoload": {

examples/oauth-example-common.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
HTTPClientAbstract, HTTPResponseInterface, TinyCurlClient
1515
};
1616
use chillerlan\Logger\{
17-
Log, LogOptions, LogTrait, Output\LogOutputAbstract
17+
Log, LogOptions, LogOptionsTrait, LogTrait, Output\LogOutputAbstract
1818
};
1919
use chillerlan\OAuth\{
2020
OAuthOptions, Storage\SessionTokenStorage
@@ -53,16 +53,19 @@
5353
// HTTPOptions
5454
'ca_info' => $CFGDIR.'/cacert.pem',
5555
'userAgent' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/codemasher/php-oauth',
56+
57+
// log
58+
'minLogLevel' => 'debug',
5659
];
5760

5861
/** @var \chillerlan\Traits\ContainerInterface $options */
5962
$options = new class($options_arr) extends OAuthOptions{
60-
use DatabaseOptionsTrait;
63+
use DatabaseOptionsTrait, LogOptionsTrait;
6164
};
6265

6366
/** @var \Psr\Log\LoggerInterface $logger */
6467
$logger = (new Log)->addInstance(
65-
new class (new LogOptions(['minLogLevel' => 'debug'])) extends LogOutputAbstract{
68+
new class ($options) extends LogOutputAbstract{
6669

6770
protected function __log(string $level, string $message, array $context = null):void{
6871
echo $message.PHP_EOL;

tests/API/APITestAbstract.php

Lines changed: 88 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,43 @@
1313
namespace chillerlan\OAuthTest\API;
1414

1515
use chillerlan\HTTP\{
16-
HTTPClientAbstract, HTTPOptionsTrait, HTTPResponseInterface, TinyCurlClient
16+
HTTPClientAbstract, HTTPClientInterface, HTTPOptionsTrait, HTTPResponseInterface, TinyCurlClient
1717
};
1818
use chillerlan\Logger\{
19-
Log, LogOptions, Output\LogOutputAbstract
19+
Log, LogOptions, LogOptionsTrait, Output\LogOutputAbstract
2020
};
2121
use chillerlan\OAuth\{
22-
OAuthOptions, Providers\ClientCredentials, Providers\OAuth2Interface, Providers\OAuthInterface, Storage\MemoryTokenStorage, Token
22+
OAuthOptions, Providers\OAuthInterface, Storage\MemoryTokenStorage, Storage\TokenStorageInterface, Token
2323
};
2424
use chillerlan\TinyCurl\Request;
2525
use chillerlan\Traits\{
2626
ContainerInterface, DotEnv
2727
};
2828
use PHPUnit\Framework\TestCase;
29+
use Psr\Log\LoggerInterface;
2930

3031
abstract class APITestAbstract extends TestCase{
3132

33+
/**
34+
* @var string
35+
*/
3236
protected $CFGDIR = __DIR__.'/../../config';
37+
38+
/**
39+
* @var string
40+
*/
3341
protected $TOKEN_EXT = 'token.json';
3442

43+
/**
44+
* @var string
45+
*/
46+
protected $FQCN;
47+
48+
/**
49+
* @var string
50+
*/
51+
protected $envvar;
52+
3553
/**
3654
* @var \chillerlan\OAuth\Storage\TokenStorageInterface
3755
*/
@@ -52,11 +70,6 @@ abstract class APITestAbstract extends TestCase{
5270
*/
5371
protected $http;
5472

55-
/**
56-
* @var string
57-
*/
58-
protected $FQCN;
59-
6073
/**
6174
* @var \chillerlan\Traits\DotEnv
6275
*/
@@ -68,42 +81,71 @@ abstract class APITestAbstract extends TestCase{
6881
protected $options;
6982

7083
/**
71-
* @var string
84+
* @var \Psr\Log\LoggerInterface
7285
*/
73-
protected $envvar;
74-
75-
/**
76-
* @var array
77-
*/
78-
protected $scopes = [];
86+
protected $logger;
7987

8088
/**
8189
* this is ugly. don't look at it - it works.
8290
*/
8391
protected function setUp(){
8492
ini_set('date.timezone', 'Europe/Amsterdam');
8593

86-
$this->env = (new DotEnv($this->CFGDIR, file_exists($this->CFGDIR.'/.env') ? '.env' : '.env_travis'))->load();
94+
$options = $this->getOptions($this->CFGDIR);
95+
96+
$this->options = new class($options) extends OAuthOptions{
97+
use HTTPOptionsTrait, LogOptionsTrait;
98+
99+
protected $sleep;
100+
};
101+
102+
$this->storage = new MemoryTokenStorage;
103+
$this->logger = $this->initLogger($this->options);
104+
$this->http = $this->initHttp($this->options);
105+
$this->provider = $this->initProvider($this->http, $this->storage, $this->options);
106+
107+
/** @noinspection PhpUndefinedMethodInspection */
108+
$this->provider->setLogger($this->logger);
109+
110+
$tokenfile = $this->CFGDIR.'/'.$this->provider->serviceName.'.'.$this->TOKEN_EXT;
111+
112+
$token = is_file($tokenfile)
113+
? (new Token)->__fromJSON(file_get_contents($tokenfile))
114+
: new Token(['accessToken' => '']);
115+
116+
$this->storage->storeAccessToken($this->provider->serviceName, $token);
117+
}
118+
119+
/**
120+
* @param string $cfgdir
121+
*
122+
* @return array
123+
*/
124+
protected function getOptions(string $cfgdir):array {
125+
$this->env = (new DotEnv($cfgdir, file_exists($cfgdir.'/.env') ? '.env' : '.env_travis'))->load();
87126

88-
$options = [
127+
return [
89128
'key' => $this->env->get($this->envvar.'_KEY'),
90129
'secret' => $this->env->get($this->envvar.'_SECRET'),
91130
'tokenAutoRefresh' => true,
92131
// HTTPOptionsTrait
93-
'ca_info' => $this->CFGDIR.'/cacert.pem',
132+
'ca_info' => $cfgdir.'/cacert.pem',
94133
'userAgent' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/chillerlan/php-oauth',
134+
// log
135+
'minLogLevel' => 'debug',
95136
// testHTTPClient
96137
'sleep' => 0.25,
97138
];
139+
}
98140

99-
$this->options = new class($options) extends OAuthOptions{
100-
use HTTPOptionsTrait;
101-
102-
protected $sleep;
103-
};
104-
105-
$logger = (new Log)->addInstance(
106-
new class (new LogOptions(['minLogLevel' => 'debug'])) extends LogOutputAbstract{
141+
/**
142+
* @param \chillerlan\Traits\ContainerInterface $options
143+
*
144+
* @return \Psr\Log\LoggerInterface
145+
*/
146+
protected function initLogger(ContainerInterface $options):LoggerInterface{
147+
return (new Log)->addInstance(
148+
new class ($options) extends LogOutputAbstract{
107149

108150
protected function __log(string $level, string $message, array $context = null):void{
109151
echo $message.PHP_EOL.print_r($context, true).PHP_EOL;
@@ -112,8 +154,26 @@ protected function __log(string $level, string $message, array $context = null):
112154
},
113155
'console'
114156
);
157+
}
158+
159+
/**
160+
* @param \chillerlan\HTTP\HTTPClientInterface $http
161+
* @param \chillerlan\OAuth\Storage\TokenStorageInterface $storage
162+
* @param \chillerlan\Traits\ContainerInterface $options
163+
*
164+
* @return \chillerlan\OAuth\Providers\OAuthInterface
165+
*/
166+
protected function initProvider(HTTPClientInterface $http, TokenStorageInterface $storage, ContainerInterface $options){
167+
return new $this->FQCN($http, $storage, $options);
168+
}
115169

116-
$this->http = new class($this->options) extends HTTPClientAbstract{
170+
/**
171+
* @param \chillerlan\Traits\ContainerInterface $options
172+
*
173+
* @return \chillerlan\HTTP\HTTPClientInterface
174+
*/
175+
protected function initHttp(ContainerInterface $options):HTTPClientInterface{
176+
return new class($options) extends HTTPClientAbstract{
117177
protected $client;
118178

119179
public function __construct(ContainerInterface $options){
@@ -127,22 +187,7 @@ public function request(string $url, array $params = null, string $method = null
127187
usleep($this->options->sleep * 1000000);
128188
return $response;
129189
}
130-
131190
};
132-
133-
$this->storage = new MemoryTokenStorage;
134-
$this->provider = new $this->FQCN($this->http, $this->storage, $this->options, $this->scopes);
135-
136-
/** @noinspection PhpUndefinedMethodInspection */
137-
$this->provider->setLogger($logger);
138-
139-
$tokenfile = $this->CFGDIR.'/'.$this->provider->serviceName.'.'.$this->TOKEN_EXT;
140-
141-
$token = is_file($tokenfile)
142-
? (new Token)->__fromJSON(file_get_contents($tokenfile))
143-
: new Token(['accessToken' => '']);
144-
145-
$this->storage->storeAccessToken($this->provider->serviceName, $token);
146191
}
147192

148193
protected function tearDown(){
@@ -156,48 +201,9 @@ protected function tearDown(){
156201
}
157202
}
158203

159-
public function testInstance(){
204+
public function testOAuthInstance(){
160205
$this->assertInstanceOf(OAuthInterface::class, $this->provider);
161206
$this->assertInstanceOf($this->FQCN, $this->provider);
162207
}
163208

164-
public function testRequestCredentialsToken(){
165-
166-
if(!$this->provider instanceof OAuth2Interface){
167-
$this->markTestSkipped('OAuth2 only');
168-
}
169-
170-
if(!$this->provider instanceof ClientCredentials){
171-
$this->markTestSkipped('not supported');
172-
}
173-
174-
$token = $this->provider->getClientCredentialsToken();
175-
176-
$this->assertInstanceOf(Token::class, $token);
177-
$this->assertInternalType('string', $token->accessToken);
178-
179-
if($token->expires !== Token::EOL_NEVER_EXPIRES){
180-
$this->assertGreaterThan(time(), $token->expires);
181-
}
182-
183-
print_r($token);
184-
}
185-
186-
/**
187-
* @expectedException \chillerlan\OAuth\Providers\ProviderException
188-
* @expectedExceptionMessage not supported
189-
*/
190-
public function testRequestCredentialsTokenNotSupportedException(){
191-
192-
if(!$this->provider instanceof OAuth2Interface){
193-
$this->markTestSkipped('OAuth2 only');
194-
}
195-
196-
if($this->provider instanceof ClientCredentials){
197-
$this->markTestSkipped('does not apply');
198-
}
199-
200-
$this->provider->getClientCredentialsToken();
201-
}
202-
203209
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Class OAuth1APITestAbstract
4+
*
5+
* @filesource OAuth1APITestAbstract.php
6+
* @created 09.04.2018
7+
* @package chillerlan\OAuthTest\API
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\API;
14+
15+
use chillerlan\OAuth\Providers\OAuth1Interface;
16+
17+
abstract class OAuth1APITestAbstract extends APITestAbstract{
18+
19+
public function testOAuth1Instance(){
20+
$this->assertInstanceOf(OAuth1Interface::class, $this->provider);
21+
}
22+
23+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Class OAuth2APITestAbstract
4+
*
5+
* @filesource OAuth2APITestAbstract.php
6+
* @created 09.04.2018
7+
* @package chillerlan\OAuthTest\API
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\API;
14+
15+
use chillerlan\HTTP\HTTPClientInterface;
16+
use chillerlan\OAuth\{
17+
Providers\ClientCredentials, Providers\OAuth2Interface, Storage\TokenStorageInterface, Token
18+
};
19+
use chillerlan\Traits\ContainerInterface;
20+
21+
/**
22+
*/
23+
abstract class OAuth2APITestAbstract extends APITestAbstract{
24+
25+
/**
26+
* @var array
27+
*/
28+
protected $scopes = [];
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function initProvider(HTTPClientInterface $http, TokenStorageInterface $storage, ContainerInterface $options){
34+
return new $this->FQCN($http, $storage, $options, $this->scopes);
35+
}
36+
37+
public function testOAuth2Instance(){
38+
$this->assertInstanceOf(OAuth2Interface::class, $this->provider);
39+
}
40+
41+
public function testRequestCredentialsToken(){
42+
43+
if(!$this->provider instanceof ClientCredentials){
44+
$this->markTestSkipped('not supported');
45+
}
46+
47+
$token = $this->provider->getClientCredentialsToken();
48+
49+
$this->assertInstanceOf(Token::class, $token);
50+
$this->assertInternalType('string', $token->accessToken);
51+
52+
if($token->expires !== Token::EOL_NEVER_EXPIRES){
53+
$this->assertGreaterThan(time(), $token->expires);
54+
}
55+
56+
print_r($token);
57+
}
58+
59+
/**
60+
* @expectedException \chillerlan\OAuth\Providers\ProviderException
61+
* @expectedExceptionMessage not supported
62+
*/
63+
public function testRequestCredentialsTokenNotSupportedException(){
64+
65+
if($this->provider instanceof ClientCredentials){
66+
$this->markTestSkipped('does not apply');
67+
}
68+
69+
$this->provider->getClientCredentialsToken();
70+
}
71+
72+
}

0 commit comments

Comments
 (0)