Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit c9d0671

Browse files
author
Fosco Marotto
committed
Merge pull request #119 from SammyK/test-cleanup
Added test user garbage collection to test suite
2 parents 5733f55 + e002ad7 commit c9d0671

15 files changed

+69
-123
lines changed

src/Facebook/FacebookRequest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class FacebookRequest
8686
*/
8787
private static $httpClientHandler;
8888

89+
/**
90+
* @var int The number of calls that have been made to Graph.
91+
*/
92+
public static $requestCount = 0;
93+
8994
/**
9095
* getSession - Returns the associated FacebookSession.
9196
*
@@ -242,6 +247,8 @@ public function execute()
242247
// Don't catch to allow it to bubble up.
243248
$result = $connection->send($url, $this->method, $params);
244249

250+
static::$requestCount++;
251+
245252
$etagHit = 304 == $connection->getResponseHttpStatusCode();
246253

247254
$headers = $connection->getResponseHeaders();

tests/FacebookCanvasLoginHelperTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
class FacebookCanvasLoginHelperTest extends PHPUnit_Framework_TestCase
77
{
88

9-
public static function setUpBeforeClass()
10-
{
11-
FacebookTestHelper::setUpBeforeClass();
12-
}
13-
14-
public static function tearDownAfterClass()
15-
{
16-
17-
}
18-
199
public function testGetSessionFromCanvasGET() {
2010
$helper = new FacebookCanvasLoginHelper();
2111
$signedRequest = FacebookSessionTest::makeSignedRequest(array(

tests/FacebookJavaScriptLoginHelperTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
class FacebookJavaScriptLoginHelperTest extends PHPUnit_Framework_TestCase
77
{
88

9-
public static function setUpBeforeClass()
10-
{
11-
FacebookTestHelper::setUpBeforeClass();
12-
}
13-
14-
public static function tearDownAfterClass()
15-
{
16-
17-
}
18-
199
public function testGetSessionFromCookie() {
2010
$helper = new FacebookJavaScriptLoginHelper(
2111
FacebookTestCredentials::$appId
@@ -28,4 +18,5 @@ public function testGetSessionFromCookie() {
2818
$this->assertTrue($session instanceof FacebookSession);
2919
$this->assertTrue($session->getToken() == 'token');
3020
}
21+
3122
}

tests/FacebookPageTabHelperTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
class FacebookPageTabHelperTest extends PHPUnit_Framework_TestCase
77
{
88

9-
public static function setUpBeforeClass()
10-
{
11-
FacebookTestHelper::setUpBeforeClass();
12-
}
13-
14-
public static function tearDownAfterClass()
15-
{
16-
17-
}
18-
199
public function testGetSessionFromPageTabGET() {
2010
$signedRequest = FacebookSessionTest::makeSignedRequest(array(
2111
'oauth_token' => 'token',
@@ -72,4 +62,4 @@ public function testLoggedOutPageTab() {
7262
$this->assertEquals(42, $helper->getPageId());
7363
}
7464

75-
}
65+
}

tests/FacebookRedirectLoginHelperTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ class FacebookRedirectLoginHelperTest extends PHPUnit_Framework_TestCase
88

99
const REDIRECT_URL = 'http://invalid.zzz';
1010

11-
public static function setUpBeforeClass()
12-
{
13-
FacebookTestHelper::setUpBeforeClass();
14-
}
15-
1611
public function testLoginURL()
1712
{
1813
$helper = new FacebookRedirectLoginHelper(
@@ -63,4 +58,4 @@ public function testLogoutURL()
6358
}
6459
}
6560

66-
}
61+
}

tests/FacebookRequestExceptionTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
class FacebookRequestExceptionTest extends PHPUnit_Framework_TestCase
1313
{
1414

15-
public static function setUpBeforeClass()
16-
{
17-
FacebookTestHelper::setUpBeforeClass();
18-
}
19-
2015
public function testAuthorizationExceptions()
2116
{
2217
$params = array(
@@ -268,4 +263,4 @@ public function testInvalidCredentialsException()
268263
$bogusSession->validate('invalid-app-id', 'invalid-app-secret');
269264
}
270265

271-
}
266+
}

tests/FacebookRequestTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
class FacebookRequestTest extends PHPUnit_Framework_TestCase
77
{
88

9-
public static function setUpBeforeClass()
10-
{
11-
FacebookTestHelper::setUpBeforeClass();
12-
}
13-
149
public function testGetsTheLoggedInUsersProfile()
1510
{
1611
$response = (

tests/FacebookSessionTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
class FacebookSessionTest extends PHPUnit_Framework_TestCase
77
{
88

9-
public static function setUpBeforeClass()
10-
{
11-
FacebookTestHelper::setUpBeforeClass();
12-
}
13-
149
public function testSessionToken()
1510
{
1611
$session = new FacebookSession(FacebookTestHelper::getAppToken());

tests/FacebookTestHelper.php

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,13 @@
44
use Facebook\FacebookRequest;
55
use Facebook\FacebookSDKException;
66

7-
// Uncomment two lines to force functional test curl implementation
8-
//use Facebook\HttpClients\FacebookCurlHttpClient;
9-
//FacebookRequest::setHttpClientHandler(new FacebookCurlHttpClient());
10-
11-
// Uncomment two lines to force functional test stream wrapper implementation
12-
//use Facebook\HttpClients\FacebookStreamHttpClient;
13-
//FacebookRequest::setHttpClientHandler(new FacebookStreamHttpClient());
14-
15-
// Uncomment two lines to force functional test Guzzle implementation
16-
//use Facebook\HttpClients\FacebookGuzzleHttpClient;
17-
//FacebookRequest::setHttpClientHandler(new FacebookGuzzleHttpClient());
18-
197
class FacebookTestHelper
208
{
219

2210
public static $testSession;
11+
protected static $testUserId;
2312

24-
public static function setUpBeforeClass()
13+
public static function initialize()
2514
{
2615
if (!strlen(FacebookTestCredentials::$appId) ||
2716
!strlen(FacebookTestCredentials::$appSecret)) {
@@ -32,47 +21,53 @@ public static function setUpBeforeClass()
3221
FacebookSession::setDefaultApplication(
3322
FacebookTestCredentials::$appId, FacebookTestCredentials::$appSecret
3423
);
35-
if (!(static::$testSession instanceof FacebookSession)) {
24+
if (!static::$testSession instanceof FacebookSession) {
3625
static::$testSession = static::createTestSession();
3726
}
3827
}
3928

40-
public static function setUp()
29+
public static function createTestSession()
4130
{
42-
31+
$accessToken = static::createTestUserAndGetAccessToken();
32+
return new FacebookSession($accessToken);
4333
}
4434

45-
public static function tearDownAfterClass()
35+
public static function createTestUserAndGetAccessToken()
4636
{
37+
$testUserPath = '/' . FacebookTestCredentials::$appId . '/accounts/test-users';
38+
$params = array(
39+
'installed' => true,
40+
'name' => 'Foo Phpunit User',
41+
'locale' => 'en_US',
42+
'permissions' => 'read_stream,user_photos',
43+
);
4744

48-
}
45+
$request = new FacebookRequest(static::getAppSession(), 'POST', $testUserPath, $params);
46+
$response = $request->execute()->getGraphObject();
4947

50-
public static function tearDown()
51-
{
48+
static::$testUserId = $response->getProperty('id');
5249

50+
return $response->getProperty('access_token');
5351
}
5452

55-
public static function createTestSession()
53+
public static function getAppSession()
5654
{
57-
$testUserPath = '/' . FacebookTestCredentials::$appId . '/accounts/test-users';
58-
$params = array(
59-
'installed' => true,
60-
'name' => 'PHPUnitTestUser',
61-
'locale' => 'en_US',
62-
'permissions' => 'read_stream, user_photos',
63-
'method' => 'post'
64-
);
65-
$response = (new FacebookRequest(
66-
new FacebookSession(FacebookTestHelper::getAppToken()),
67-
'GET',
68-
$testUserPath,
69-
$params))->execute()->getGraphObject();
70-
return new FacebookSession($response->getProperty('access_token'));
55+
return new FacebookSession(static::getAppToken());
7156
}
7257

7358
public static function getAppToken()
7459
{
7560
return FacebookTestCredentials::$appId . '|' . FacebookTestCredentials::$appSecret;
7661
}
7762

78-
}
63+
public static function deleteTestUser()
64+
{
65+
if (!static::$testUserId) {
66+
return;
67+
}
68+
$testUserPath = '/' . static::$testUserId;
69+
$request = new FacebookRequest(static::getAppSession(), 'DELETE', $testUserPath);
70+
$request->execute();
71+
}
72+
73+
}

tests/GraphAlbumTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55

66
class GraphAlbumTest extends PHPUnit_Framework_TestCase
77
{
8+
89
const ALBUM_DESCRIPTION = "Album Description";
910
const ALBUM_NAME = "Album Name";
1011

11-
public static function setUpBeforeClass()
12-
{
13-
FacebookTestHelper::setUpBeforeClass();
14-
}
15-
1612
public function testMeReturnsGraphAlbum()
1713
{
1814
$response = (
@@ -53,4 +49,5 @@ public function testMeReturnsGraphAlbum()
5349
$this->assertTrue($response->getCreatedTime() instanceof DateTime);
5450
$this->assertTrue($response->getUpdatedTime() instanceof DateTime);
5551
}
56-
}
52+
53+
}

0 commit comments

Comments
 (0)