33 * Class APITestAbstract
44 *
55 * @filesource APITestAbstract.php
6- * @created 10.07.2017
6+ * @created 09.04.2018
77 * @package chillerlan\OAuthTest\API
88 * @author Smiley <smiley@chillerlan.net>
9- * @copyright 2017 Smiley
9+ * @copyright 2018 Smiley
1010 * @license MIT
1111 */
1212
1313namespace chillerlan \OAuthTest \API ;
1414
15- use chillerlan \Database \{
16- Database , DatabaseOptionsTrait , Drivers \MySQLiDrv
17- };
1815use chillerlan \HTTP \{
19- CurlClient , GuzzleClient , HTTPClientAbstract , HTTPClientInterface , HTTPOptionsTrait , HTTPResponseInterface , StreamClient , TinyCurlClient
16+ HTTPClientAbstract , HTTPOptionsTrait , HTTPResponseInterface , TinyCurlClient
2017};
2118use chillerlan \Logger \{
2219 Log , LogOptions , Output \LogOutputAbstract
2320};
2421use chillerlan \OAuth \{
25- OAuthOptions , Providers \ClientCredentials , Providers \OAuth2Interface , Providers \OAuthInterface , Storage \DBTokenStorage , Token
22+ OAuthOptions , Providers \ClientCredentials , Providers \OAuth2Interface , Providers \OAuthInterface , Storage \MemoryTokenStorage , Token
2623};
2724use chillerlan \TinyCurl \Request ;
2825use chillerlan \Traits \{
2926 ContainerInterface , DotEnv
3027};
31- use GuzzleHttp \Client ;
3228use PHPUnit \Framework \TestCase ;
33- use Psr \Log \LogLevel ;
3429
3530abstract class APITestAbstract extends TestCase{
3631
37- protected $ CFGDIR = __DIR__ .'/../../config ' ;
38- protected $ STORAGE = __DIR__ .'/../../tokenstorage ' ;
39-
40- const UA = 'chillerlanPhpOAuth/2.0.0 +https://github.com/chillerlan/php-oauth ' ;
41- const SLEEP_SECONDS = 1.0 ;
42- const TABLE_TOKEN = 'storagetest ' ;
43- const TABLE_PROVIDER = 'storagetest_providers ' ;
32+ protected $ CFGDIR = __DIR__ .'/../../config ' ;
33+ protected $ TOKEN_EXT = 'token.json ' ;
4434
4535 /**
4636 * @var \chillerlan\OAuth\Storage\TokenStorageInterface
@@ -87,6 +77,9 @@ abstract class APITestAbstract extends TestCase{
8777 */
8878 protected $ scopes = [];
8979
80+ /**
81+ * this is ugly. don't look at it - it works.
82+ */
9083 protected function setUp (){
9184 ini_set ('date.timezone ' , 'Europe/Amsterdam ' );
9285
@@ -95,37 +88,22 @@ protected function setUp(){
9588 $ options = [
9689 'key ' => $ this ->env ->get ($ this ->envvar .'_KEY ' ),
9790 'secret ' => $ this ->env ->get ($ this ->envvar .'_SECRET ' ),
98- 'callbackURL ' => $ this ->env ->get ($ this ->envvar .'_CALLBACK_URL ' ),
99- 'dbTokenTable ' => $ this ::TABLE_TOKEN ,
100- 'dbProviderTable ' => $ this ::TABLE_PROVIDER ,
101- 'storageCryptoKey ' => '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f ' ,
102- 'dbUserID ' => 1 ,
10391 'tokenAutoRefresh ' => true ,
10492 // HTTPOptionsTrait
10593 'ca_info ' => $ this ->CFGDIR .'/cacert.pem ' ,
106- 'userAgent ' => $ this ::UA ,
107- // DatabaseOptionsTrait
108- 'driver ' => MySQLiDrv::class,
109- 'host ' => $ this ->env ->MYSQL_HOST ,
110- 'port ' => $ this ->env ->MYSQL_PORT ,
111- 'database ' => $ this ->env ->MYSQL_DATABASE ,
112- 'username ' => $ this ->env ->MYSQL_USERNAME ,
113- 'password ' => $ this ->env ->MYSQL_PASSWORD ,
94+ 'userAgent ' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/chillerlan/php-oauth ' ,
11495 // testHTTPClient
115- 'testclient ' => ' tinycurl ' ,
96+ 'sleep ' => 0.25 ,
11697 ];
11798
11899 $ this ->options = new class ($ options ) extends OAuthOptions{
119- use HTTPOptionsTrait, DatabaseOptionsTrait ;
100+ use HTTPOptionsTrait;
120101
121- protected $ testclient ;
102+ protected $ sleep ;
122103 };
123- $ this ->storage = new DBTokenStorage ($ this ->options , new Database ($ this ->options ));
124- $ this ->http = $ this ->initHTTP ();
125- $ this ->provider = new $ this ->FQCN ($ this ->http , $ this ->storage , $ this ->options , $ this ->scopes );
126104
127105 $ logger = (new Log )->addInstance (
128- new class (new LogOptions (['minLogLevel ' => LogLevel:: DEBUG ])) extends LogOutputAbstract{
106+ new class (new LogOptions (['minLogLevel ' => ' debug ' ])) extends LogOutputAbstract{
129107
130108 protected function __log (string $ level , string $ message , array $ context = null ):void {
131109 echo $ message .PHP_EOL .print_r ($ context , true ).PHP_EOL ;
@@ -135,67 +113,47 @@ protected function __log(string $level, string $message, array $context = null):
135113 'console '
136114 );
137115
138-
139- $ this ->provider ->setLogger ($ logger );
140- $ this ->storage ->storeAccessToken ($ this ->provider ->serviceName , $ this ->getToken ());
141- }
142-
143- protected function tearDown (){
144- if ($ this ->response instanceof HTTPResponseInterface){
145-
146- $ json = $ this ->response ->json ;
147-
148- !empty ($ json )
149- ? print_r ($ json )
150- : print_r ($ this ->response ->body );
151- }
152- }
153-
154- protected function initHTTP ():HTTPClientInterface {
155- return new class ($ this ->options ) extends HTTPClientAbstract{
116+ $ this ->http = new class ($ this ->options ) extends HTTPClientAbstract{
156117 protected $ client ;
157118
158119 public function __construct (ContainerInterface $ options ){
159120 parent ::__construct ($ options );
160- $ this ->client = call_user_func ([ $ this , $ this ->options -> testclient ] );
121+ $ this ->client = new TinyCurlClient ( $ this -> options , new Request ( $ this ->options ) );
161122 }
162123
163124 public function request (string $ url , array $ params = null , string $ method = null , $ body = null , array $ headers = null ):HTTPResponseInterface {
164125 $ args = func_get_args ();
165- # print_r($args);
166126 $ response = $ this ->client ->request (...$ args );
167- # print_r($response);
168- usleep (APITestAbstract::SLEEP_SECONDS * 1000000 );
127+ usleep ($ this ->options ->sleep * 1000000 );
169128 return $ response ;
170129 }
171130
172- protected function guzzle (){
173- return new GuzzleClient ($ this ->options , new Client (['cacert ' => $ this ->options ->ca_info , 'headers ' => ['User-Agent ' => $ this ->options ->userAgent ]]));
174- }
131+ };
175132
176- protected function tinycurl (){
177- return new TinyCurlClient ($ this ->options , new Request ($ this ->options ));
178- }
133+ $ this ->storage = new MemoryTokenStorage ;
134+ $ this ->provider = new $ this ->FQCN ($ this ->http , $ this ->storage , $ this ->options , $ this ->scopes );
179135
180- protected function curl (){
181- return new CurlClient ($ this ->options );
182- }
136+ /** @noinspection PhpUndefinedMethodInspection */
137+ $ this ->provider ->setLogger ($ logger );
183138
184- protected function stream (){
185- return new StreamClient ($ this ->options );
186- }
139+ $ tokenfile = $ this ->CFGDIR .'/ ' .$ this ->provider ->serviceName .'. ' .$ this ->TOKEN_EXT ;
187140
188- };
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 );
189146 }
190147
191- protected function getToken (): Token {
192- $ file = $ this ->STORAGE . ' / ' . $ this -> provider -> serviceName . ' .token.json ' ;
148+ protected function tearDown () {
149+ if ( $ this ->response instanceof HTTPResponseInterface){
193150
194- if (is_file ($ file )){
195- return (new Token )->__fromJSON (file_get_contents ($ file ));
196- }
151+ $ json = $ this ->response ->json ;
197152
198- return new Token (['accessToken ' => '' ]);
153+ !empty ($ json )
154+ ? print_r ($ json )
155+ : print_r ($ this ->response ->body );
156+ }
199157 }
200158
201159 public function testInstance (){
0 commit comments