summaryrefslogtreecommitdiff
path: root/test/lib
diff options
context:
space:
mode:
authorBrent Shaffer <bshafs@gmail.com>2012-12-20 20:09:08 +0400
committerBrent Shaffer <bshafs@gmail.com>2012-12-20 20:09:08 +0400
commit8cb2360d9d76db082f6db9fbaccf05d1e2dcbb5b (patch)
tree5fbe976d7e3a50539a81fc96533579aaefec3677 /test/lib
parent5e4d75e3b155c449ebe1d4a734568780d3e57d69 (diff)
parent81ceb467b9b6745574a9ece4b3b2bbebf1e7c9b9 (diff)
downloadoauth2-server-php-0.2.tar.xz
Merge branch 'develop'v0.2
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/OAuth2/Request/TestRequest.php46
-rw-r--r--test/lib/OAuth2/Storage/Bootstrap.php94
2 files changed, 140 insertions, 0 deletions
diff --git a/test/lib/OAuth2/Request/TestRequest.php b/test/lib/OAuth2/Request/TestRequest.php
new file mode 100644
index 0000000..623d139
--- /dev/null
+++ b/test/lib/OAuth2/Request/TestRequest.php
@@ -0,0 +1,46 @@
+<?php
+
+/**
+*
+*/
+class OAuth2_Request_TestRequest implements OAuth2_RequestInterface
+{
+ private $query, $post, $server;
+
+ public function __construct()
+ {
+ $this->query = $_GET;
+ $this->post = $_POST;
+ $this->server = $_SERVER;
+ }
+
+ public function query($name, $default = null)
+ {
+ return isset($this->query[$name]) ? $this->query[$name] : $default;
+ }
+
+ public function request($name, $default = null)
+ {
+ return isset($this->post[$name]) ? $this->post[$name] : $default;
+ }
+
+ public function server($name, $default = null)
+ {
+ return isset($this->server[$name]) ? $this->server[$name] : $default;
+ }
+
+ public function headers($name, $default = null)
+ {
+ return $default;
+ }
+
+ public function getAllQueryParameters()
+ {
+ return $this->query;
+ }
+
+ public function setQuery(array $query)
+ {
+ $this->query = $query;
+ }
+} \ No newline at end of file
diff --git a/test/lib/OAuth2/Storage/Bootstrap.php b/test/lib/OAuth2/Storage/Bootstrap.php
new file mode 100644
index 0000000..4a214b3
--- /dev/null
+++ b/test/lib/OAuth2/Storage/Bootstrap.php
@@ -0,0 +1,94 @@
+<?php
+
+class OAuth2_Storage_Bootstrap
+{
+ protected static $instance;
+ private $mysql;
+ private $sqlite;
+
+ public static function getInstance()
+ {
+ if (!self::$instance) {
+ self::$instance = new self();
+ }
+ return self::$instance;
+ }
+
+ public function getSqlitePdo()
+ {
+ if (!$this->sqlite) {
+ $this->removeSqliteDb();
+ $pdo = new PDO(sprintf('sqlite://%s', $this->getSqliteDir()));
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $this->createSqliteDb($pdo);
+
+ $this->sqlite = new OAuth2_Storage_Pdo($pdo);
+ }
+ return $this->sqlite;
+ }
+
+ public function getMemoryStorage()
+ {
+ return new OAuth2_Storage_Memory(json_decode(file_get_contents(dirname(__FILE__).'/../../../config/storage.json'), true));
+ }
+
+ public function getMysqlPdo()
+ {
+ if (!$this->mysql) {
+ $pdo = new PDO('mysql:host=localhost;', 'root');
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $this->removeMysqlDb($pdo);
+ $this->createMysqlDb($pdo);
+
+ $this->mysql = new OAuth2_Storage_Pdo($pdo);
+ }
+ return $this->mysql;
+ }
+
+ private function createSqliteDb(PDO $pdo)
+ {
+ $pdo->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT)');
+ $pdo->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires TIMESTAMP, scope TEXT)');
+ $pdo->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires TIMESTAMP, scope TEXT)');
+ $pdo->exec('CREATE TABLE oauth_users (username TEXT, password TEXT, first_name TEXT, last_name TEXT)');
+
+ // test data
+ $pdo->exec('INSERT INTO oauth_clients (client_id, client_secret) VALUES ("oauth_test_client", "testpass")');
+ $pdo->exec('INSERT INTO oauth_access_tokens (access_token, client_id) VALUES ("testtoken", "Some Client")');
+ $pdo->exec('INSERT INTO oauth_authorization_codes (authorization_code, client_id) VALUES ("testcode", "Some Client")');
+ $pdo->exec('INSERT INTO oauth_users (username, password) VALUES ("testuser", "password")');
+ }
+
+ private function removeSqliteDb()
+ {
+ if (file_exists($this->getSqliteDir())) {
+ unlink($this->getSqliteDir());
+ }
+ }
+
+ private function createMysqlDb(PDO $pdo)
+ {
+ $pdo->exec('CREATE DATABASE oauth2_server_php');
+ $pdo->exec('USE oauth2_server_php');
+ $pdo->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT)');
+ $pdo->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires DATETIME, scope TEXT)');
+ $pdo->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires DATETIME, scope TEXT)');
+ $pdo->exec('CREATE TABLE oauth_users (username TEXT, password TEXT, first_name TEXT, last_name TEXT)');
+
+ // test data
+ $pdo->exec('INSERT INTO oauth_clients (client_id, client_secret) VALUES ("oauth_test_client", "testpass")');
+ $pdo->exec('INSERT INTO oauth_access_tokens (access_token, client_id) VALUES ("testtoken", "Some Client")');
+ $pdo->exec('INSERT INTO oauth_authorization_codes (authorization_code, client_id) VALUES ("testcode", "Some Client")');
+ $pdo->exec('INSERT INTO oauth_users (username, password) VALUES ("testuser", "password")');
+ }
+
+ public function removeMysqlDb(PDO $pdo)
+ {
+ $pdo->exec('DROP DATABASE IF EXISTS oauth2_server_php');
+ }
+
+ private function getSqliteDir()
+ {
+ return dirname(__FILE__).'/../../../config/test.sqlite';
+ }
+} \ No newline at end of file