diff --git a/composer.json b/composer.json index dea640b..57ae74e 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ }, "require-dev": { "phpunit/phpunit": "^4.5 || ^5.0", + "nyholm/nsa": "^1.0", "php-http/guzzle5-adapter": "^1.0", "guzzlehttp/psr7": "^1.2", "mockery/mockery": "^0.9", diff --git a/src/AccessToken.php b/src/AccessToken.php index 926d443..db82d7b 100644 --- a/src/AccessToken.php +++ b/src/AccessToken.php @@ -34,6 +34,31 @@ public function __construct($token = null, $expiresIn = null) } } + /** + * Restore a stored access token. + * + * @param string|AccessToken $token + * + * @return AccessToken + */ + public static function create($token) + { + if (empty($token)) { + return new self(); + } + + if ($token instanceof AccessToken) { + return $token; + } + + $unserialized = @unserialize($token); + if ($unserialized instanceof AccessToken) { + return $unserialized; + } + + return new self($token); + } + /** * @return string */ diff --git a/src/Authenticator.php b/src/Authenticator.php index a98f5fd..1b217e2 100644 --- a/src/Authenticator.php +++ b/src/Authenticator.php @@ -81,7 +81,7 @@ public function fetchNewAccessToken(LinkedInUrlGeneratorInterface $urlGenerator) } $storage->set('code', $code); - $storage->set('access_token', $accessToken); + $storage->set('access_token', serialize($accessToken)); return $accessToken; } diff --git a/src/LinkedIn.php b/src/LinkedIn.php index bef60cf..a277b21 100644 --- a/src/LinkedIn.php +++ b/src/LinkedIn.php @@ -304,11 +304,7 @@ public function getAccessToken() */ public function setAccessToken($accessToken) { - if (!($accessToken instanceof AccessToken)) { - $accessToken = new AccessToken($accessToken); - } - - $this->accessToken = $accessToken; + $this->accessToken = AccessToken::create($accessToken); return $this; } diff --git a/tests/AccessTokenTest.php b/tests/AccessTokenTest.php index 9762c5c..4c87641 100644 --- a/tests/AccessTokenTest.php +++ b/tests/AccessTokenTest.php @@ -36,4 +36,35 @@ public function testSetExpiresAt() $token->setExpiresAt(new \DateTime('+2minutes')); $this->assertInstanceOf('\DateTime', $token->getExpiresAt()); } + + public function testCreateWithString() + { + $token = AccessToken::create('foobar'); + $this->assertInstanceOf(AccessToken::class, $token); + $this->assertEquals('foobar', $token->__toString()); + } + + public function testCreateWithNoData() + { + $token = AccessToken::create(''); + $this->assertInstanceOf(AccessToken::class, $token); + $this->assertEquals('', $token->__toString()); + } + + public function testCreateWithAccessToken() + { + $orgToken = new AccessToken('foobar', 10); + $token = AccessToken::create($orgToken); + $this->assertInstanceOf(AccessToken::class, $token); + $this->assertEquals('foobar', $token->__toString()); + } + + public function testCreateWithSerializedAccessToken() + { + $orgToken = new AccessToken('foobar', 10); + $orgTokenSerialized = serialize($orgToken); + $token = AccessToken::create($orgTokenSerialized); + $this->assertInstanceOf(AccessToken::class, $token); + $this->assertEquals('foobar', $token->__toString()); + } } diff --git a/tests/LinkedInTest.php b/tests/LinkedInTest.php index 7f93b4d..d6ad964 100644 --- a/tests/LinkedInTest.php +++ b/tests/LinkedInTest.php @@ -3,6 +3,7 @@ namespace Happyr\LinkedIn; use GuzzleHttp\Psr7\Response; +use Nyholm\NSA; /** * @author Tobias Nyholm @@ -77,10 +78,16 @@ public function testAccessTokenAccessors() { $token = 'token'; - $auth = $this->getMock('Happyr\LinkedIn\Authenticator', ['fetchNewAccessToken'], [], '', false); + $auth = $this->getMockBuilder('Happyr\LinkedIn\Authenticator') + ->setMethods(['fetchNewAccessToken']) + ->disableOriginalConstructor() + ->getMock(); $auth->expects($this->once())->method('fetchNewAccessToken')->will($this->returnValue($token)); - $linkedIn = $this->getMock('Happyr\LinkedIn\LinkedIn', ['getAuthenticator'], [], '', false); + $linkedIn = $this->getMockBuilder('Happyr\LinkedIn\LinkedIn') + ->setMethods(['getAuthenticator']) + ->disableOriginalConstructor() + ->getMock(); $linkedIn->expects($this->once())->method('getAuthenticator')->willReturn($auth); // Make sure we go to the authenticator only once @@ -88,6 +95,27 @@ public function testAccessTokenAccessors() $this->assertEquals($token, $linkedIn->getAccessToken()); } + public function testAccessTokenSetterWithSerializedToken() + { + $linkedIn = new LinkedIn(self::APP_ID, self::APP_SECRET); + $token = new AccessToken('foobar'); + $serializedToken = serialize($token); + $linkedIn->setAccessToken($serializedToken); + + $storedToken = NSA::getProperty($linkedIn, 'accessToken'); + $this->assertEquals('foobar', $storedToken->__toString()); + } + + public function testAccessTokenSetterWithTokenObject() + { + $linkedIn = new LinkedIn(self::APP_ID, self::APP_SECRET); + $token = new AccessToken('foobar'); + $linkedIn->setAccessToken($token); + + $storedToken = NSA::getProperty($linkedIn, 'accessToken'); + $this->assertEquals('foobar', $storedToken->__toString()); + } + public function testGeneratorAccessors() { $get = new \ReflectionMethod('Happyr\LinkedIn\LinkedIn', 'getUrlGenerator');