Skip to content

Commit

Permalink
Adding SessionToken and implementing it into the Client
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 24, 2014
1 parent b49f91e commit 41524a5
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/Tmdb/ApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($api_token = null)
* @param null $apiToken
* @return $this
*/
public function setApiToken($apiToken)
public function setToken($apiToken)
{
$this->apiToken = $apiToken;
return $this;
Expand All @@ -38,7 +38,7 @@ public function setApiToken($apiToken)
/**
* @return null
*/
public function getApiToken()
public function getToken()
{
return $this->apiToken;
}
Expand Down
25 changes: 25 additions & 0 deletions lib/Tmdb/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class Client {
*/
private $token;

/**
* Stores API user session token
*
* @var SessionToken
*/
private $sessionToken;

/**
* Whether the request is supposed to use a secure schema
*
Expand Down Expand Up @@ -322,4 +329,22 @@ public function getSecure()
{
return $this->secure;
}

/**
* @param SessionToken $sessionToken
* @return $this
*/
public function setSessionToken($sessionToken)
{
$this->sessionToken = $sessionToken;
return $this;
}

/**
* @return SessionToken
*/
public function getSessionToken()
{
return $this->sessionToken;
}
}
2 changes: 1 addition & 1 deletion lib/Tmdb/HttpClient/Plugin/ApiTokenPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function onBeforeSend(Event $event)
{
$url = $event['request']->getUrl(true);

$url->getQuery()->set('api_key', $this->token->getApiToken());
$url->getQuery()->set('api_key', $this->token->getToken());

$event['request']->setUrl($url);
}
Expand Down
45 changes: 45 additions & 0 deletions lib/Tmdb/SessionToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb;

class SessionToken {
private $sessionToken = null;

/**
* Token bag
*
* @param $session_token
*/
public function __construct($session_token = null)
{
$this->sessionToken = $session_token;
}

/**
* @param null $sessionToken
* @return $this
*/
public function setToken($sessionToken)
{
$this->sessionToken = $sessionToken;
return $this;
}

/**
* @return null
*/
public function getToken()
{
return $this->sessionToken;
}
}
6 changes: 4 additions & 2 deletions test/Tmdb/Tests/ApiTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
*/
class ApiTokenTest extends \PHPUnit_Framework_TestCase
{
const API_TOKEN = 'abcdefg';

/**
* @test
*/
public function testSetGet()
{
$token = new \Tmdb\ApiToken();
$token->setApiToken('abcdefg');
$token->setToken(self::API_TOKEN);

$this->assertEquals('abcdefg', $token->getApiToken());
$this->assertEquals(self::API_TOKEN, $token->getToken());
}
}
20 changes: 19 additions & 1 deletion test/Tmdb/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@
*/
class ClientTest extends \Tmdb\Tests\TestCase
{
const API_TOKEN = 'abcdef';
const SESSION_TOKEN = '80b2bf99520cd795ff54e31af97917bc9e3a7c8c';

/**
* @var Tmdb\Client
*/
private $client = null;

public function setUp()
{
$token = new \Tmdb\ApiToken('abcdef');
$token = new \Tmdb\ApiToken(self::API_TOKEN);
$sessionToken = new \Tmdb\SessionToken(self::SESSION_TOKEN);

$client = new \Tmdb\Client($token);
$client->setSessionToken($sessionToken);

$this->client = $client;
}
Expand All @@ -30,6 +39,15 @@ public function shouldNotHaveToPassHttpClientToConstructor()
$this->assertInstanceOf('Tmdb\HttpClient\HttpClient', $this->client->getHttpClient());
}

/**
* @test
*/
public function shouldContainSessionToken()
{
$this->assertInstanceOf('Tmdb\SessionToken', $this->client->getSessionToken());
$this->assertEquals(self::SESSION_TOKEN, $this->client->getSessionToken()->getToken());
}

/**
* @test
*/
Expand Down
27 changes: 27 additions & 0 deletions test/Tmdb/Tests/SessionTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
class SessionTokenTest extends \PHPUnit_Framework_TestCase
{
const SESSION_TOKEN = '80b2bf99520cd795ff54e31af97917bc9e3a7c8c';

/**
* @test
*/
public function testSetGet()
{
$token = new \Tmdb\SessionToken();
$token->setToken(self::SESSION_TOKEN);

$this->assertEquals(self::SESSION_TOKEN, $token->getToken());
}
}

0 comments on commit 41524a5

Please sign in to comment.