Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90badc7

Browse files
author
Dave Rochwerger
committed
Separated out output tests into own test class. Created Test Suite
1 parent ee43326 commit 90badc7

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

tests/OAuth2OutputTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
require_once(__DIR__ . '/../lib/OAuth2.php');
3+
require_once(__DIR__ . '/../lib/IOAuth2Storage.php');
4+
require_once(__DIR__ . '/../lib/IOAuth2GrantCode.php');
5+
6+
/**
7+
* OAuth2 test cases that invovle capturing output.
8+
*/
9+
class OAuth2OutputTest extends PHPUnit_Extensions_OutputTestCase {
10+
11+
/**
12+
* @var OAuth2
13+
*/
14+
private $fixture;
15+
16+
/**
17+
* Tests OAuth2->grantAccessToken() with successful Auth code grant
18+
*
19+
*/
20+
public function testGrantAccessTokenWithGrantAuthCodeSuccess() {
21+
$inputData = array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'redirect_uri' => 'http://www.example.com/my/subdir', 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo');
22+
$storedToken = array('redirect_uri' => 'http://www.example.com', 'client_id' => 'my_little_app', 'expires' => time() + 60);
23+
24+
$mockStorage = $this->createBaseMock('IOAuth2GrantCode');
25+
$mockStorage->expects($this->any())
26+
->method('getAuthCode')
27+
->will($this->returnValue($storedToken));
28+
29+
// Successful token grant will return a JSON encoded token:
30+
$this->expectOutputRegex('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/');
31+
$this->fixture = new OAuth2($mockStorage);
32+
$this->fixture->grantAccessToken($inputData, array(), FALSE);
33+
}
34+
35+
// Utility methods
36+
37+
/**
38+
*
39+
* @param string $interfaceName
40+
*/
41+
protected function createBaseMock($interfaceName) {
42+
$mockStorage = $this->getMock($interfaceName);
43+
$mockStorage->expects($this->any())
44+
->method('checkClientCredentials')
45+
->will($this->returnValue(TRUE)); // Always return true for any combination of user/pass
46+
$mockStorage->expects($this->any())
47+
->method('checkRestrictedGrantType')
48+
->will($this->returnValue(TRUE)); // Always return true for any combination of user/pass
49+
50+
return $mockStorage;
51+
}
52+
53+
}

tests/OAuth2Suite.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
/**
44
* Static test suite.
55
*/
6+
67
class OAuth2Suite extends PHPUnit_Framework_TestSuite {
78

89
/**
910
* Constructs the test suite handler.
1011
*/
1112
public function __construct() {
1213
$this->setName ( 'OAuth2Suite' );
14+
15+
foreach (glob(__DIR__.'/*Test.php') as $filename) {
16+
require_once($filename);
17+
$class = basename($filename, '.php');
18+
// $this->addTest(new $class());
19+
$this->addTestSuite($class);
20+
}
1321
}
1422

1523
/**

tests/OAuth2Test.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* OAuth2 test case.
88
*/
9-
class OAuth2Test extends PHPUnit_Extensions_OutputTestCase {
9+
class OAuth2Test extends PHPUnit_Framework_TestCase {
1010

1111
/**
1212
* @var OAuth2
@@ -19,22 +19,6 @@ class OAuth2Test extends PHPUnit_Extensions_OutputTestCase {
1919
*/
2020
private $tokenId = 'my_token';
2121

22-
/**
23-
* Prepares the environment before running a test.
24-
*/
25-
protected function setUp() {
26-
parent::setUp ();
27-
// $this->fixture = new OAuth2(/* parameters */);
28-
}
29-
30-
/**
31-
* Cleans up the environment after running a test.
32-
*/
33-
protected function tearDown() {
34-
$this->fixture = null;
35-
parent::tearDown ();
36-
}
37-
3822
/**
3923
* Tests OAuth2->verifyAccessToken() with a missing token
4024
*/
@@ -143,7 +127,7 @@ public function testVerifyAccessTokenCheckScope($scopeRequired, $token, $expecte
143127
}
144128

145129
/**
146-
* Tests OAuth2->grantAccessToken()
130+
* Tests OAuth2->grantAccessToken() for missing data
147131
*
148132
* @dataProvider generateEmptyDataForGrant
149133
*/
@@ -158,7 +142,7 @@ public function testGrantAccessTokenMissingData($inputData, $authHeaders) {
158142
/**
159143
* Tests OAuth2->grantAccessToken()
160144
*
161-
* Tests the different ways a client credentials can be provided.
145+
* Tests the different ways client credentials can be provided.
162146
*/
163147
public function testGrantAccessTokenCheckClientCredentials() {
164148
$mockStorage = $this->getMock('IOAuth2Storage');
@@ -170,7 +154,7 @@ public function testGrantAccessTokenCheckClientCredentials() {
170154
$inputData = array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE);
171155
$authHeaders = array();
172156

173-
// First, confirm that an non client related error is thrown:
157+
// First, confirm that an non-client related error is thrown:
174158
try {
175159
$this->fixture->grantAccessToken($inputData, $authHeaders);
176160
$this->fail('The expected exception OAuth2ServerException was not thrown');
@@ -254,25 +238,6 @@ public function testGrantAccessTokenWithGrantAuthCodeNoToken() {
254238
}
255239
}
256240

257-
/**
258-
* Tests OAuth2->grantAccessToken() with successful Auth code grant
259-
*
260-
*/
261-
public function testGrantAccessTokenWithGrantAuthCodeSuccess() {
262-
$inputData = array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'redirect_uri' => 'http://www.example.com/my/subdir', 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo');
263-
$storedToken = array('redirect_uri' => 'http://www.example.com', 'client_id' => 'my_little_app', 'expires' => time() + 60);
264-
265-
$mockStorage = $this->createBaseMock('IOAuth2GrantCode');
266-
$mockStorage->expects($this->any())
267-
->method('getAuthCode')
268-
->will($this->returnValue($storedToken));
269-
270-
// Successful token grant will return a JSON encoded token:
271-
$this->expectOutputRegex('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/');
272-
$this->fixture = new OAuth2($mockStorage);
273-
$this->fixture->grantAccessToken($inputData, array(), FALSE);
274-
}
275-
276241
/**
277242
* Tests OAuth2->grantAccessToken() with checks the redirect URI
278243
*

0 commit comments

Comments
 (0)