Skip to content

Commit

Permalink
:octocat: use FileStorage in tests & examples
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Apr 2, 2024
1 parent 48ddfb9 commit 0589105
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 205 deletions.
1 change: 1 addition & 0 deletions .config/.filestorage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
15 changes: 12 additions & 3 deletions examples/OAuthExampleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
*/
declare(strict_types=1);

require_once __DIR__.'/OAuthExampleSessionStorage.php';

use chillerlan\DotEnv\DotEnv;
use chillerlan\OAuth\Core\OAuth1Interface;
use chillerlan\OAuth\Core\OAuth2Interface;
use chillerlan\OAuth\Core\OAuthInterface;
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\OAuthProviderFactory;
use chillerlan\OAuth\Storage\FileStorage;
use chillerlan\OAuth\Storage\MemoryStorage;
use chillerlan\OAuth\Storage\OAuthStorageInterface;
use chillerlan\OAuth\Storage\SessionStorage;
use chillerlan\Settings\SettingsContainerInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NullHandler;
Expand Down Expand Up @@ -82,12 +83,20 @@ public function getProvider(
$storage = new MemoryStorage;

if($sessionStorage === true){
$storage = new OAuthExampleSessionStorage(options: $options, storagepath: $this->cfgDir);
$storage = new SessionStorage(options: $options);
}

return $this->factory->getProvider($providerFQN, $options, $storage);
}

public function getFileStorage():OAuthStorageInterface{
$options = new OAuthOptions;

$options->fileStoragePath = $this->cfgDir.'/.filestorage';

return new FileStorage('oauth-example', $options, $this->logger);
}

public function getEnvVar(string $var):mixed{
return $this->dotEnv->get($var);
}
Expand Down
86 changes: 0 additions & 86 deletions examples/OAuthExampleSessionStorage.php

This file was deleted.

37 changes: 8 additions & 29 deletions tests/Providers/ProviderLiveTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@

use chillerlan\DotEnv\DotEnv;
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Storage\FileStorage;
use chillerlan\OAuth\Storage\OAuthStorageInterface;
use InvalidArgumentException;
use function constant;
use function defined;
use function ltrim;
use function realpath;
use function sprintf;

/**
*
Expand All @@ -30,7 +27,6 @@ abstract class ProviderLiveTestAbstract extends ProviderUnitTestAbstract{

protected DotEnv $dotEnv;
protected string $ENV_PREFIX;
protected string $CFG_DIR;

/** a test username for live API tests, defined in .env as {ENV-PREFIX}_TESTUSER*/
protected string $TEST_USER = '';
Expand All @@ -40,16 +36,11 @@ abstract class ProviderLiveTestAbstract extends ProviderUnitTestAbstract{
*/
protected function setUp():void{

// are we running on CI? (initializing this here too, so we can back out before initializing the provider)
$this->ENV_IS_CI = defined('TEST_IS_CI') && constant('TEST_IS_CI') === true;

if($this->ENV_IS_CI){
// are we running on CI? (checking this here, so we can back out before initializing the provider)
if(defined('TEST_IS_CI') && constant('TEST_IS_CI') === true){
$this->markTestSkipped('not on CI (set TEST_IS_CI in phpunit.xml to "false" if you want to run live API tests)');
}

// set the config dir and .env config before initializing the provider
$this->initEnvConfig();

// init provider etc.
parent::setUp();
}
Expand All @@ -59,30 +50,18 @@ protected function setUp():void{
*/
abstract protected function getEnvPrefix():string;

protected function initEnvConfig():void{

foreach(['TEST_CFGDIR', 'TEST_ENVFILE'] as $constant){
if(!defined($constant)){
throw new InvalidArgumentException(sprintf('constant "%s" not set -> see phpunit.xml', $constant));
}
}

$cfgdir = constant('TEST_CFGDIR');
$this->CFG_DIR = realpath($this::PROJECT_ROOT.ltrim($cfgdir, '/\\'));

if($this->CFG_DIR === false){
throw new InvalidArgumentException(sprintf('invalid config dir "%s" (relative from project root)', $cfgdir));
}
protected function initConfig():void{
parent::initConfig();

$this->dotEnv = (new DotEnv($this->CFG_DIR, constant('TEST_ENVFILE'), false))->load();
$this->ENV_PREFIX = $this->getEnvPrefix();
$this->TEST_USER = (string)$this->dotEnv->get($this->ENV_PREFIX.'_TESTUSER');

$this->TEST_USER = (string)$this->dotEnv->get($this->ENV_PREFIX.'_TESTUSER');
}

protected function initOptions():OAuthOptions{
$options = new OAuthOptions;
$options->tokenAutoRefresh = true;
$options->fileStoragePath = $this->CFG_DIR.'/.filestorage';

if(!empty($this->ENV_PREFIX)){
$options->key = ($this->dotEnv->get($this->ENV_PREFIX.'_KEY') ?? '');
Expand All @@ -93,7 +72,7 @@ protected function initOptions():OAuthOptions{
}

protected function initStorage(OAuthOptions $options):OAuthStorageInterface{
return new ProviderLiveTestMemoryStorage($options, $this->CFG_DIR);
return new FileStorage('oauth-example', $options);
}

}
80 changes: 0 additions & 80 deletions tests/Providers/ProviderLiveTestMemoryStorage.php

This file was deleted.

37 changes: 30 additions & 7 deletions tests/Providers/ProviderUnitTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use chillerlan\OAuth\Storage\MemoryStorage;
use chillerlan\OAuth\Storage\OAuthStorageInterface;
use chillerlan\PHPUnitHttp\HttpFactoryTrait;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
Expand All @@ -28,6 +29,7 @@
use function constant;
use function defined;
use function ini_set;
use function ltrim;
use function realpath;
use function sprintf;

Expand All @@ -46,7 +48,8 @@ abstract class ProviderUnitTestAbstract extends TestCase{

protected string $HTTP_CLIENT_FACTORY = ProviderUnitTestHttpClientFactory::class;

protected bool $ENV_IS_CI;
protected string $CFG_DIR;
protected bool $ENV_IS_CI;

protected const PROJECT_ROOT = __DIR__.'/../../';
protected const CACERT = __DIR__.'/../cacert.pem';
Expand All @@ -58,6 +61,7 @@ protected function setUp():void{
$this->ENV_IS_CI = defined('TEST_IS_CI') && constant('TEST_IS_CI') === true;

try{
$this->initConfig();
$this->initFactories(realpath($this::CACERT));

$this->logger = (new ProviderTestLoggerFactory)->getLogger($this->ENV_IS_CI); // PSR-3 logger
Expand Down Expand Up @@ -96,13 +100,32 @@ abstract protected function getProviderFQCN():string;
* init dependencies
*/

protected function initConfig():void{

foreach(['TEST_CFGDIR', 'TEST_ENVFILE'] as $constant){
if(!defined($constant)){
throw new InvalidArgumentException(sprintf('constant "%s" not set -> see phpunit.xml', $constant));
}
}

$cfgdir = constant('TEST_CFGDIR');
$this->CFG_DIR = realpath($this::PROJECT_ROOT.ltrim($cfgdir, '/\\'));

if($this->CFG_DIR === false){
throw new InvalidArgumentException(sprintf('invalid config dir "%s" (relative from project root)', $cfgdir));
}

}

protected function initOptions():OAuthOptions{
return new OAuthOptions([
'key' => 'testclient',
'secret' => 'testsecret',
'callbackURL' => 'https://localhost/callback',
'tokenAutoRefresh' => true,
]);
$options = new OAuthOptions;

$options->key = 'testclient';
$options->secret = 'testsecret';
$options->callbackURL = 'https://localhost/callback';
$options->tokenAutoRefresh = true;

return $options;
}

protected function initStorage(OAuthOptions $options):OAuthStorageInterface{
Expand Down

0 comments on commit 0589105

Please sign in to comment.