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

Commit 2adcefc

Browse files
committed
1 parent 9c5d15f commit 2adcefc

File tree

8 files changed

+325
-141
lines changed

8 files changed

+325
-141
lines changed

tests/API/APITestAbstract.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
/**
3+
* Class APITestAbstract
4+
*
5+
* @filesource APITestAbstract.php
6+
* @created 08.09.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\DotEnv\DotEnv;
16+
use chillerlan\HTTP\{CurlClient, HTTPOptionsTrait, Psr7};
17+
use chillerlan\Logger\{Log, LogOptionsTrait, Output\LogOutputAbstract};
18+
use chillerlan\OAuth\{OAuthOptionsTrait, Core\AccessToken, Core\OAuthInterface, Storage\MemoryStorage};
19+
use chillerlan\Settings\{SettingsContainerAbstract, SettingsContainerInterface};
20+
use Http\Client\HttpClient;
21+
use PHPUnit\Framework\TestCase;
22+
use Psr\Http\Message\{RequestInterface, ResponseInterface};
23+
use Psr\Log\LoggerInterface;
24+
25+
class APITestAbstract extends TestCase{
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $CFG = __DIR__.'/../../config';
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $FQN;
36+
37+
/**
38+
* @var string
39+
*/
40+
protected $ENV;
41+
42+
/**
43+
* @var \chillerlan\OAuth\Core\OAuthInterface
44+
*/
45+
protected $provider;
46+
47+
/**
48+
* @var \chillerlan\OAuth\Storage\OAuthStorageInterface
49+
*/
50+
protected $storage;
51+
52+
/**
53+
* @var \Psr\Log\LoggerInterface
54+
*/
55+
protected $logger;
56+
57+
/**
58+
* @var \chillerlan\DotEnv\DotEnv
59+
*/
60+
protected $dotEnv;
61+
62+
protected function setUp(){
63+
ini_set('date.timezone', 'Europe/Amsterdam');
64+
65+
$file = file_exists($this->CFG.'/.env') ? '.env' : '.env_travis';
66+
$this->dotEnv = (new DotEnv($this->CFG, $file))->load();
67+
68+
if($this->dotEnv->get('IS_CI') === 'TRUE'){
69+
$this->markTestSkipped('not on CI');
70+
71+
return;
72+
}
73+
74+
$options = [
75+
'key' => $this->dotEnv->get($this->ENV.'_KEY'),
76+
'secret' => $this->dotEnv->get($this->ENV.'_SECRET'),
77+
'tokenAutoRefresh' => true,
78+
// HTTPOptionsTrait
79+
'ca_info' => $this->CFG.'/cacert.pem',
80+
'userAgent' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/chillerlan/php-oauth',
81+
// testHTTPClient
82+
'sleep' => 0.25,
83+
// logger
84+
'minLogLevel' => 'debug',
85+
];
86+
87+
$options = new class($options) extends SettingsContainerAbstract{
88+
use OAuthOptionsTrait, HTTPOptionsTrait, LogOptionsTrait;
89+
protected $sleep;
90+
};
91+
92+
$this->logger = $this->initLog($options);
93+
$http = $this->initHttp($options, $this->logger);
94+
$this->storage = new MemoryStorage;
95+
$this->provider = new $this->FQN($http, $this->storage, $options);
96+
$this->provider->setLogger($this->logger);
97+
98+
$tokenfile = $this->CFG.'/'.$this->provider->serviceName.'.token.json';
99+
$token = is_file($tokenfile)
100+
? (new AccessToken)->fromJSON(file_get_contents($tokenfile))
101+
: new AccessToken(['accessToken' => 'nope']);
102+
103+
$this->storage->storeAccessToken($this->provider->serviceName, $token);
104+
}
105+
106+
/**
107+
* @param $options
108+
*
109+
* @return \Psr\Log\LoggerInterface
110+
*/
111+
protected function initLog($options):LoggerInterface{
112+
113+
return (new Log)->addInstance(
114+
new class($options) extends LogOutputAbstract{
115+
116+
protected function __log(string $level, string $message, array $context = null):void{
117+
echo $message;//.PHP_EOL.print_r($context, true).PHP_EOL;
118+
}
119+
120+
},
121+
'console'
122+
);
123+
}
124+
125+
/**
126+
* @param $options
127+
*
128+
* @return \Http\Client\HttpClient
129+
*/
130+
protected function initHttp($options, $logger):HttpClient{
131+
return new class($options, $logger) implements HttpClient{
132+
/** @var \Http\Client\HttpClient */
133+
protected $client;
134+
/** @var \chillerlan\Settings\SettingsContainerInterface */
135+
protected $options;
136+
/** @var \Psr\Log\LoggerInterface */
137+
protected $logger;
138+
139+
public function __construct(SettingsContainerInterface $options, LoggerInterface $logger){
140+
$this->options = $options;
141+
$this->logger = $logger;
142+
$this->client = new CurlClient($this->options);
143+
}
144+
145+
public function sendRequest(RequestInterface $request):ResponseInterface{
146+
usleep($this->options->sleep * 1000000);
147+
148+
$response = $this->client->sendRequest($request);
149+
150+
$this->logger->debug("\n-----REQUEST-----\n".Psr7\message_to_string($request));
151+
$this->logger->debug("\n-----RESPONSE-----\n".Psr7\message_to_string($response));
152+
153+
$response->getBody()->rewind();
154+
return $response;
155+
}
156+
};
157+
}
158+
159+
/**
160+
* @param \Psr\Http\Message\ResponseInterface $response
161+
*
162+
* @return mixed
163+
*/
164+
protected function responseJson(ResponseInterface $response){
165+
$response->getBody()->rewind();
166+
167+
return json_decode($response->getBody()->getContents());
168+
}
169+
170+
public function testOAuthInstance(){
171+
$this->assertInstanceOf(OAuthInterface::class, $this->provider);
172+
$this->assertInstanceOf($this->FQN, $this->provider);
173+
}
174+
175+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Class OAuth1APITestAbstract
4+
*
5+
* @filesource OAuth1APITestAbstract.php
6+
* @created 08.09.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\Core\OAuth1Interface;
16+
17+
/**
18+
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
19+
*/
20+
class OAuth1APITestAbstract extends APITestAbstract{
21+
22+
public function testOAuth1Instance(){
23+
$this->assertInstanceOf(OAuth1Interface::class, $this->provider);
24+
}
25+
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Class OAuth2APITestAbstract
4+
*
5+
* @filesource OAuth2APITestAbstract.php
6+
* @created 08.09.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\Core\{AccessToken, ClientCredentials, OAuth2Interface};
16+
17+
/**
18+
* @property \chillerlan\OAuth\Core\OAuth2Interface $provider
19+
*/
20+
class OAuth2APITestAbstract extends APITestAbstract{
21+
22+
public function testOAuth2Instance(){
23+
$this->assertInstanceOf(OAuth2Interface::class, $this->provider);
24+
}
25+
26+
public function testRequestCredentialsToken(){
27+
28+
if(!$this->provider instanceof ClientCredentials){
29+
$this->markTestSkipped('ClientCredentials N/A');
30+
31+
return;
32+
}
33+
34+
$token = $this->provider->getClientCredentialsToken();
35+
36+
$this->assertInstanceOf(AccessToken::class, $token);
37+
$this->assertInternalType('string', $token->accessToken);
38+
39+
if($token->expires !== AccessToken::EOL_NEVER_EXPIRES){
40+
$this->assertGreaterThan(time(), $token->expires);
41+
}
42+
43+
$this->logger->debug('OAuth2ClientCredentials', $token->toArray());
44+
}
45+
46+
47+
}

tests/Storage/AccessTokenTest.php renamed to tests/Core/AccessTokenTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@
33
*
44
* @filesource AccessTokenTest.php
55
* @created 09.07.2017
6-
* @package chillerlan\OAuthTest\Storage
6+
* @package chillerlan\OAuthTest\Core
77
* @author Smiley <smiley@chillerlan.net>
88
* @copyright 2017 Smiley
99
* @license MIT
1010
*/
1111

12-
namespace chillerlan\OAuthTest\Storage;
12+
namespace chillerlan\OAuthTest\Core;
1313

1414
use chillerlan\OAuth\Core\AccessToken;
15-
use chillerlan\OAuthTest\OAuthTestAbstract;
15+
use PHPUnit\Framework\TestCase;
1616

17-
class AccessTokenTest extends OAuthTestAbstract{
17+
class AccessTokenTest extends TestCase{
1818

1919
/**
2020
* @var \chillerlan\OAuth\Core\AccessToken
2121
*/
2222
protected $token;
2323

2424
protected function setUp(){
25-
parent::setUp();
26-
2725
$this->token = new AccessToken;
2826
}
2927

@@ -38,6 +36,10 @@ public function tokenDataProvider(){
3836

3937
/**
4038
* @dataProvider tokenDataProvider
39+
*
40+
* @param $property
41+
* @param $value
42+
* @param $data
4143
*/
4244
public function testDefaultsGetSet($property, $value, $data){
4345
// test defaults
@@ -62,6 +64,9 @@ public function expiryDataProvider(){
6264

6365
/**
6466
* @dataProvider expiryDataProvider
67+
*
68+
* @param $expires
69+
* @param $expected
6570
*/
6671
public function testSetExpiry($expires, $expected){
6772
$this->token->expires = $expires;
@@ -79,6 +84,9 @@ public function isExpiredDataProvider(){
7984

8085
/**
8186
* @dataProvider isExpiredDataProvider
87+
*
88+
* @param $expires
89+
* @param $isExpired
8290
*/
8391
public function testIsExpired($expires, $isExpired){
8492
$this->token->setExpiry($expires);

tests/OAuthTestAbstract.php

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

0 commit comments

Comments
 (0)