Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 133 #134

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 1 addition & 5 deletions src/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
32 changes: 30 additions & 2 deletions tests/LinkedInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Happyr\LinkedIn;

use GuzzleHttp\Psr7\Response;
use Nyholm\NSA;

/**
* @author Tobias Nyholm <[email protected]>
Expand Down Expand Up @@ -77,17 +78,44 @@ 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
$this->assertEquals($token, $linkedIn->getAccessToken());
$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');
Expand Down