8000 initial commit with basic unit tests · oauth-io/sdk-php@0684c7a · GitHub
[go: up one dir, main page]

Skip to content

Commit 0684c7a

Browse files
committed
initial commit with basic unit tests
0 parents  commit 0684c7a

File tree

10 files changed

+278
-0
lines changed

10 files changed

+278
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/*
2+
coverage/*
3+
.DS_Store*
4+
composer.lock

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "OAuth_io/OAuth",
3+
"description": "OAuth that just works !",
4+
"license": "Apache2",
5+
"version": "0.0.1",
6+
"authors": [
7+
{
8+
"name": "Antoine Jackson",
9+
"email": "team@oauth.io"
10+
}
11+
],
12+
"minimum-stability": "dev",
13+
"require": {
14+
"php": ">=5.3.0"
15+
},
16+
"autoload": {
17+
"psr-0": {
18+
"OAuth_io": "src/"
19+
}
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "4.0.*"
23+
}
24+
}

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0 8000 " encoding="UTF-8"?>
2+
<phpunit colors="true">
3+
<testsuites>
4+
<testsuite name="Application Test Suite">
5+
<directory>./tests/</directory>
6+
7+
</testsuite>
8+
</testsuites>
9+
<filter>
10+
<blacklist>
11+
<directory>./vendor</directory>
12+
</blacklist>
13+
</filter>
14+
</phpunit>

src/OAuth_io/CurlHttpRequest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
interface CurlHttpRequest
3+
{
4+
public function init($url);
5+
public function setOption($name, $value);
6+
public function execute();
7+
public function getInfo($name);
8+
public function close();
9+
}

src/OAuth_io/CurlRequest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
class CurlRequest implements CurlHttpRequest
3+
{
4+
private $handle = null;
5+
6+
public function init() {
7+
$this->handle = curl_init();
8+
}
9+
10+
public function setOption($name, $value) {
11+
curl_setopt($this->handle, $name, $value);
12+
}
13+
14+
public function execute() {
15+
return curl_exec($this->handle);
16+
}
17+
18+
public function getInfo($name) {
19+
return curl_getinfo($this->handle, $name);
20+
}
21+
22+
public function close() {
23+
curl_close($this->handle);
24+
}
25+
}

src/OAuth_io/OAuth.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace OAuth_io;
4+
5+
class OAuth {
6+
public function getVersion() {
7+
return "0.0.1";
8+
}
9+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use OAuth_io\OAuth;
6+
7+
class AuthenticateTest extends PHPUnit_Framework_TestCase {
8+
9+
protected $oauth;
10+
protected $token;
11+
12+
protected function setUp() {
13+
$this->oauth = new OAuth();
14+
$this->oauth->initialize('somekey', 'somesecret');
15+
$this->token = $this->oauth->generateToken();
16+
}
17+
18+
public function testAuthenticateMethodExists() {
19+
$this->assertTrue(method_exists('OAuth', 'authenticate'));
20+
}
21+
22+
public function testAuthenticateMethodCallsOauthioWithCredentialsAndCode() {
23+
$http = $this->getMockBuilder('OAuth_io\CurlHttpRequest')
24+
->getMock();
25+
26+
$http->expects($this->once())
27+
->method('setOption')
28+
->with(CURLOPT_URL, 'https://oauth.io/auth/token');
29+
30+
$http->expects($this->once())
31+
->method('setOption')
32+
->with(CURLOPT_POST, 3);
33+
34+
$fields = array(
35+
'code' => 'somecode',
36+
'key' => 'somekey',
37+
'secret' => 'somesecret'
38+
);
39+
40+
$http->expects($this->once())
41+
->method('setOption')
42+
->with(CURLOPT_POSTFIELDS, http_build_query($fields));
43+
44+
$response = array(
45+
'access_token' => 'someaccesstoken',
46+
'state' => $this->token
47+
);
48+
49+
$http->expects($this->once())
50+
->method('execute')
51+
->will($this->returnValue(json_encode($response)));
52+
53+
$result = $this->oauth->authenticate('somecode', $http);
54+
55+
$this->assertTrue(is_array($result));
56+
$this->assertEquals($result['access_token'], 'someaccesstoken');
57+
$this->assertEquals($result['state'], $this->token);
58+
}
59+
}

tests/OAuthTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
6+
use OAuth_io\OAuth;
7+
8+
9+
10+
class OAuthTest extends PHPUnit_Framework_TestCase {
11+
12+
protected $oauth;
13+
14+
protected function setUp() {
15+
$this->oauth = new OAuth();
16+
}
17+
18+
public function testGetVersionReturnsVersion() {
19+
$version = json_decode(file_get_contents('./composer.json'), true);
20< F987 span class="diff-text-marker">+
$version = $version["version"];
21+
22+
$this->assertEquals($this->oauth->getVersion(), $version);
23+
}
24+
25+
public function testInitializeSetsKeyAndSecret() {
26+
if (method_exists('OAuth', 'initialize')) {
27+
$this->oauth->initialize('somekey', 'somesecret');
28+
29+
$this->assertEquals($this->oauth->getAppKey(), 'somekey');
30+
$this->assertEquals($this->oauth->getAppSecret(), 'somesecret');
31+
} else {
32+
$this->fail('OAuth::initialize() does not exist');
33+
}
34+
}
35+
36+
public function testOAuthdUrlIsOAuthIOByDefault() {
37+
if (method_exists('OAuth', 'initialize') && method('OAuth', 'setOAuthdUrl')) {
38+
39+
$this->assertEquals($this->oauth->getOAuthdUrl(), 'https://oaut.io');
40+
41+
} else {
42+
$this->fail('methods are missing');
43+
}
44+
}
45+
46+
public function testSetOAuthdUrlSetsUrlInObject() {
47+
if (method_exists('OAuth', 'initialize') && method('OAuth', 'setOAuthdUrl')) {
48+
$this->oauth->initialize('somekey', 'somesecret');
49+
$this->oauth->setOAuthdUrl('https://oauthd.local');
50+
51+
$this->assertEquals($this->oauth->getOAuthdUrl(), 'https://oauthd.local');
52+
53+
} else {
54+
$this->fail('methods are missing');
55+
}
56+
}
57+
}

tests/RequestsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use OAuth_io/OAuth;
6+
7+
class RequestsTest extendes PHPUnit_Framework_TestCase {
8+
protected $oauth;
9+
protected $result;
10+
11+
protected function setUp() {
12+
$http = $this->getMockBuilder('OAuth_io\CurlHttpRequest')
13+
->getMock();
14+
15+
$http->expects($this->once())
16+
->method('execute')
17+
->will($this->returnValue(json_encode($response)));
18+
19+
$this->oauth = new OAuth();
20+
$this->oauth->initialize('somekey', 'somesecret');
21+
$this->result = $this->oauth->authenticate('somecode', $http);
22+
}
23+
24+
public function testRequest() {
25+
26+
}
27+
}

tests/TokenGenerationTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use OAuth_io\OAuth;
6+
7+
class TokenGenerationTest extends PHPUnit_Framework_TestCase {
8+
protected $oauth;
9+
10+
protected function setUp() {
11+
$this->oauth = new OAuth();
12+
}
13+
14+
public function testTokenGeneratorExists() {
15+
$this->assertTrue(method_exists('OAuth', 'generateToken'));
16+
}
17+
18+
public function testTokenGeneratorResultFormat() {
19+
if (method_exists('OAuth', 'generateToken')) {
20+
$session = array();
21+
22+
$token1 = $this->oauth->generateToken($session);
23+
$token2 = $this->oauth->generateToken($session);
24+
25+
$this->assertTrue(is_string($token1) && is_string(token2));
26+
$this->assertTrue($token1 !== $token2);
27+
} else {
28+
$this->fail('$this->oauth->generateToken does not exist');
29+
}
30+
}
31+
32+
public function testTokenGeneratorSessionStorage() {
33+
if (method_exists('OAuth', 'generateToken')) {
34+
$session = array();
35+
36+
$token1 = $this->oauth->generateToken($session);
37+
38+
$this->assertTrue(isset($session["oauthio"]["tokens"][0]));
39+
$this->assertEquals($session["oauthio"]["tokens"][0], $token1);
40+
41+
$token2 = $this->oauth->generateToken($session);
42+
43+
$this->assertTrue(isset($session["oauthio"]["tokens"][0]));
44+
$this->assertEquals($session["oauthio"]["tokens"][0], $token2);
45+
46+
$this->assertTrue(isset($session["oauthio"]["tokens"][1]));
47+
$this->assertEquals($session["oauthio"]["tokens"][1], $token1);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)
0