<commit_before><?php
/**
 * Class TokenStorageTestAbstract
 *
 * @filesource   TokenStorageTestAbstract.php
 * @created      11.07.2016
 * @package      chillerlan\OAuthTest\Storage
 * @author       Smiley <<EMAIL>>
 * @copyright    2016 Smiley
 * @license      MIT
 */

namespace chillerlan\OAuthTest\Storage;

use chillerlan\OAuth\{
	OAuthOptions, Token, Storage\TokenStorageInterface
};
use PHPUnit\Framework\TestCase;

abstract class TokenStorageTestAbstract extends TestCase{

	const SERVICE_NAME = 'Spotify';

	/**
	 * @var \chillerlan\OAuth\Storage\TokenStorageInterface
	 */
	protected $storage;

	/**
	 * @var \chillerlan\OAuth\Token
	 */
	protected $token;

	/**
	 * @var \chillerlan\OAuth\OAuthOptions
	 */
	protected $options;

	/**
	 * @var string FQCN
	 */
	protected $FQCN;

	protected function setUp(){

		$this->options = new OAuthOptions([
			'storageCryptoKey' => '<KEY>',
			'dbUserID' => 1,
		]);

		$this->storage = new $this->FQCN($this->options);
		$this->token   = new Token(['accessToken' => 'foobar']);
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testInterface(){
		$this->assertInstanceOf($this->FQCN, $this->storage);
		$this->assertInstanceOf(TokenStorageInterface::class, $this->storage);
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testTokenStorage(){
		$this->storage->storeAccessToken(self::SERVICE_NAME, $this->token);
		$this->assertTrue($this->storage->hasAccessToken(self::SERVICE_NAME));
		$this->assertSame('foobar', $this->storage->retrieveAccessToken(self::SERVICE_NAME)->accessToken);

		$this->storage->storeAuthorizationState(self::SERVICE_NAME, 'foobar');
		$this->assertTrue($this->storage->hasAuthorizationState(self::SERVICE_NAME));
		$this->assertSame('foobar', $this->storage->retrieveAuthorizationState(self::SERVICE_NAME));

		$this->storage->clearAuthorizationState(self::SERVICE_NAME);
		$this->assertFalse($this->storage->hasAuthorizationState(self::SERVICE_NAME));

		$this->storage->clearAccessToken(self::SERVICE_NAME);
		$this->assertFalse($this->storage->hasAccessToken(self::SERVICE_NAME));
	}


	/**
	 * @runInSeparateProcess
	 */
	public function testClearAllAccessTokens(){
		$range = ['Spotify', 'LastFM', 'Twitter'];
		$this->storage->clearAllAccessTokens();

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAccessToken($k));
			$this->storage->storeAccessToken($k, $this->token);
			$this->assertTrue($this->storage->hasAccessToken($k));
		}

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAuthorizationState($k));
			$this->storage->storeAuthorizationState($k, 'foobar');
			$this->assertTrue($this->storage->hasAuthorizationState($k));
		}

		$this->storage->clearAllAuthorizationStates();

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAuthorizationState($k));
		}

		$this->storage->clearAllAccessTokens();

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAccessToken($k));
		}

	}


	/**
	 * @runInSeparateProcess
	 * @expectedException \chillerlan\OAuth\Storage\TokenStorageException
	 * @expectedExceptionMessage state not found
	 */
	public function testRetrieveAuthorizationStateNotFoundException(){
		$this->storage->retrieveAuthorizationState('LOLNOPE');
	}

	/**
	 * @runInSeparateProcess
	 * @expectedException \chillerlan\OAuth\Storage\TokenStorageException
	 * @expectedExceptionMessage token not found
	 */
	public function testRetrieveAccessTokenNotFoundException(){
		$this->storage->retrieveAccessToken('LO<PASSWORD>');
	}

}<commit_msg>:octocat: coverage<commit_after><?php
/**
 * Class TokenStorageTestAbstract
 *
 * @filesource   TokenStorageTestAbstract.php
 * @created      11.07.2016
 * @package      chillerlan\OAuthTest\Storage
 * @author       Smiley <<EMAIL>>
 * @copyright    2016 Smiley
 * @license      MIT
 */

namespace chillerlan\OAuthTest\Storage;

use chillerlan\OAuth\{
	OAuthOptions, Token, Storage\TokenStorageInterface
};
use PHPUnit\Framework\TestCase;

abstract class TokenStorageTestAbstract extends TestCase{

	const SERVICE_NAME = 'Spotify';

	/**
	 * @var \chillerlan\OAuth\Storage\TokenStorageInterface
	 */
	protected $storage;

	/**
	 * @var \chillerlan\OAuth\Token
	 */
	protected $token;

	/**
	 * @var \chillerlan\OAuth\OAuthOptions
	 */
	protected $options;

	/**
	 * @var string FQCN
	 */
	protected $FQCN;

	protected function setUp(){

		$this->options = new OAuthOptions([
			'storageCryptoKey' => '<KEY>',
			'dbUserID' => 1,
		]);

		$this->storage = new $this->FQCN($this->options);
		$this->token   = new Token(['accessToken' => 'foobar']);
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testInterface(){
		$this->assertInstanceOf($this->FQCN, $this->storage);
		$this->assertInstanceOf(TokenStorageInterface::class, $this->storage);
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testTokenStorage(){
		$this->storage->storeAccessToken(self::SERVICE_NAME, $this->token);
		$this->assertTrue($this->storage->hasAccessToken(self::SERVICE_NAME));
		$this->assertSame('foobar', $this->storage->retrieveAccessToken(self::SERVICE_NAME)->accessToken);

		$this->storage->storeAuthorizationState(self::SERVICE_NAME, 'foobar');
		$this->assertTrue($this->storage->hasAuthorizationState(self::SERVICE_NAME));
		$this->assertSame('foobar', $this->storage->retrieveAuthorizationState(self::SERVICE_NAME));

		$this->storage->clearAuthorizationState(self::SERVICE_NAME);
		$this->assertFalse($this->storage->hasAuthorizationState(self::SERVICE_NAME));

		$this->storage->clearAccessToken(self::SERVICE_NAME);
		$this->assertFalse($this->storage->hasAccessToken(self::SERVICE_NAME));
	}


	/**
	 * @runInSeparateProcess
	 */
	public function testClearAllAccessTokens(){
		$range = ['Spotify', 'LastFM', 'Twitter'];
		$this->storage->clearAllAccessTokens();

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAccessToken($k));
			$this->storage->storeAccessToken($k, $this->token);
			$this->assertTrue($this->storage->hasAccessToken($k));
		}

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAuthorizationState($k));
			$this->storage->storeAuthorizationState($k, 'foobar');
			$this->assertTrue($this->storage->hasAuthorizationState($k));
		}

		$this->storage->clearAllAuthorizationStates();

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAuthorizationState($k));
		}

		$this->storage->clearAllAccessTokens();

		foreach($range as $k){
			$this->assertFalse($this->storage->hasAccessToken($k));
		}

	}


	/**
	 * @runInSeparateProcess
	 * @expectedException \chillerlan\OAuth\Storage\TokenStorageException
	 * @expectedExceptionMessage state not found
	 */
	public function testRetrieveAuthorizationStateNotFoundException(){
		$this->storage->retrieveAuthorizationState('LOLNOPE');
	}

	/**
	 * @runInSeparateProcess
	 * @expectedException \chillerlan\OAuth\Storage\TokenStorageException
	 * @expectedExceptionMessage token not found
	 */
	public function testRetrieveAccessTokenNotFoundException(){
		$this->storage->retrieveAccessToken('<PASSWORD>');
	}

	/**
	 * @runInSeparateProcess
	 */
	public function testToStorage(){
		$a = $this->storage->toStorage($this->token);
		$b = $this->storage->fromStorage($a);

		$this->assertInternalType('string', $a);
		$this->assertInstanceOf(Token::class, $b);
		$this->assertEquals($this->token, $b);
	}

}
{'fraction_non_alphanumeric': 0.16909462219196733, 'fraction_numerical': 0.0035398230088495575, 'mean_word_length': 5.289383561643835, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 168, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2128805', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2285, 'n_words': 313}