|
| 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 | +} |
0 commit comments