ApiTest.php (4961B)
1 <?php 2 3 /** 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 namespace fkooman\OAuth\Client; 19 20 use PDO; 21 use fkooman\OAuth\Common\Scope; 22 use Guzzle\Http\Client; 23 use Guzzle\Plugin\Mock\MockPlugin; 24 use Guzzle\Http\Message\Response; 25 26 class ApiTest extends \PHPUnit_Framework_TestCase 27 { 28 /** @var array */ 29 private $clientConfig; 30 31 /** @var fkooman\OAuth\Client\PdoStorage */ 32 private $storage; 33 34 public function setUp() 35 { 36 $this->clientConfig = array(); 37 38 $this->clientConfig[] = new ClientConfig( 39 array( 40 "client_id" => "foo", 41 "client_secret" => "bar", 42 "authorize_endpoint" => "http://www.example.org/authorize", 43 "token_endpoint" => "http://www.example.org/token", 44 ) 45 ); 46 47 $this->storage = new PdoStorage( 48 new PDO( 49 $GLOBALS['DB_DSN'], 50 $GLOBALS['DB_USER'], 51 $GLOBALS['DB_PASSWD'] 52 ) 53 ); 54 $this->storage->initDatabase(); 55 } 56 57 public function testGetAccessTokenWithoutToken() 58 { 59 $client = new Client(); 60 $mock = new MockPlugin(); 61 $mock->addResponse(new Response(200)); 62 $client->addSubscriber($mock); 63 64 $api = new Api("foo", $this->clientConfig[0], $this->storage, $client); 65 $context = new Context("a_user", array("foo", "bar")); 66 67 $this->assertFalse($api->getAccessToken($context)); 68 $this->assertEquals("http://www.example.org/authorize?client_id=foo&response_type=code&state=my_custom_state&scope=bar+foo", $api->getAuthorizeUri($context, "my_custom_state")); 69 } 70 71 public function testGetAccessTokenWithToken() 72 { 73 $client = new Client(); 74 $mock = new MockPlugin(); 75 $mock->addResponse(new Response(200)); 76 $client->addSubscriber($mock); 77 78 $api = new Api("foo", $this->clientConfig[0], $this->storage, $client); 79 $context = new Context("a_user", array("foo", "bar")); 80 81 $accessToken = new AccessToken( 82 array( 83 "client_config_id" => "foo", 84 "user_id" => "a_user", 85 "token_type" => "bearer", 86 "access_token" => "my_token_value", 87 "scope" => Scope::fromString("foo bar"), 88 "issue_time" => time() - 100, 89 "expires_in" => 3600, 90 ) 91 ); 92 $this->storage->storeAccessToken($accessToken); 93 94 $accessToken = $api->getAccessToken($context); 95 $this->assertEquals("my_token_value", $accessToken->getAccessToken()); 96 } 97 98 public function testGetAccessTokenWithExpiredAccessTokenAndRefreshToken() 99 { 100 $client = new Client(); 101 $mock = new MockPlugin(); 102 $mock->addResponse( 103 new Response( 104 200, 105 null, 106 json_encode( 107 array( 108 "access_token" => "my_new_access_token_value", 109 "token_type" => "Bearer", 110 ) 111 ) 112 ) 113 ); 114 $client->addSubscriber($mock); 115 116 $api = new Api("foo", $this->clientConfig[0], $this->storage, $client); 117 $context = new Context("a_user", array("foo", "bar")); 118 119 $accessToken = new AccessToken( 120 array( 121 "client_config_id" => "foo", 122 "user_id" => "a_user", 123 "token_type" => "bearer", 124 "access_token" => "my_token_value", 125 "scope" => Scope::fromString("foo bar"), 126 "issue_time" => time() - 4000, 127 "expires_in" => 3600, 128 ) 129 ); 130 $this->storage->storeAccessToken($accessToken); 131 132 $refreshToken = new RefreshToken( 133 array( 134 "client_config_id" => "foo", 135 "user_id" => "a_user", 136 "refresh_token" => "my_refresh_token_value", 137 "scope" => Scope::fromString("foo bar"), 138 "issue_time" => time() - 10000, 139 ) 140 ); 141 $this->storage->storeRefreshToken($refreshToken); 142 143 $accessToken = $api->getAccessToken($context); 144 $this->assertEquals("my_new_access_token_value", $accessToken->getAccessToken()); 145 //$this->assertFalse($accessToken); 146 } 147 }