CallbackTest.php (2848B)
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 CallbackTest extends \PHPUnit_Framework_TestCase 27 { 28 /** @var array */ 29 private $clientConfig; 30 31 public function setUp() 32 { 33 $this->clientConfig = array(); 34 35 $this->clientConfig[] = new ClientConfig( 36 array( 37 "client_id" => "foo", 38 "client_secret" => "bar", 39 "authorize_endpoint" => "http://www.example.org/authorize", 40 "token_endpoint" => "http://www.example.org/token", 41 ) 42 ); 43 44 $this->storage = new PdoStorage( 45 new PDO( 46 $GLOBALS['DB_DSN'], 47 $GLOBALS['DB_USER'], 48 $GLOBALS['DB_PASSWD'] 49 ) 50 ); 51 $this->storage->initDatabase(); 52 } 53 54 public function testXYZ() 55 { 56 $client = new Client(); 57 $mock = new MockPlugin(); 58 $mock->addResponse( 59 new Response( 60 200, 61 null, 62 json_encode( 63 array( 64 "access_token" => "my_access_token", 65 "token_type" => "BeArEr", 66 "refresh_token" => "why_not_a_refresh_token", 67 ) 68 ) 69 ) 70 ); 71 $client->addSubscriber($mock); 72 73 $state = new State( 74 array( 75 "state" => "my_state", 76 "client_config_id" => "foo", 77 "issue_time" => time() - 100, 78 "user_id" => "my_user_id", 79 "scope" => Scope::fromString("foo bar"), 80 ) 81 ); 82 $this->storage->storeState($state); 83 84 $callback = new Callback("foo", $this->clientConfig[0], $this->storage, $client); 85 86 $tokenResponse = $callback->handleCallback( 87 array( 88 "state" => "my_state", 89 "code" => "my_code", 90 ) 91 ); 92 93 $this->assertEquals("my_access_token", $tokenResponse->getAccessToken()); 94 } 95 }