1313namespace chillerlan \OAuthTest \API ;
1414
1515use chillerlan \HTTP \{
16- HTTPClientAbstract , HTTPOptionsTrait , HTTPResponseInterface , TinyCurlClient
16+ HTTPClientAbstract , HTTPClientInterface , HTTPOptionsTrait , HTTPResponseInterface , TinyCurlClient
1717};
1818use chillerlan \Logger \{
19- Log , LogOptions , Output \LogOutputAbstract
19+ Log , LogOptions , LogOptionsTrait , Output \LogOutputAbstract
2020};
2121use chillerlan \OAuth \{
22- OAuthOptions , Providers \ClientCredentials , Providers \ OAuth2Interface , Providers \ OAuthInterface , Storage \MemoryTokenStorage , Token
22+ OAuthOptions , Providers \OAuthInterface , Storage \ MemoryTokenStorage , Storage \TokenStorageInterface , Token
2323};
2424use chillerlan \TinyCurl \Request ;
2525use chillerlan \Traits \{
2626 ContainerInterface , DotEnv
2727};
2828use PHPUnit \Framework \TestCase ;
29+ use Psr \Log \LoggerInterface ;
2930
3031abstract 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}
0 commit comments