views:

68

answers:

2

Hi there!

In my Facebook application, I have one Facebook wrapper class to encapsulates some call to Facebook API. I want to to write a unit test for this wrapper class, but since it depends on a so called "access token", which we should get from Facebook dynamically, I'm not sure if it's possible to write one.

But apparently the Facebook SDK itself has a PHPUnit test class. After studying the test code for a while, I know that involves a creation of dummy cookie-based session key.

private static $VALID_EXPIRED_SESSION = array(
    'access_token' => '254752073152|2.I_eTFkcTKSzX5no3jI4r1Q__.3600.1273359600-1677846385|uI7GwrmBUed8seZZ05JbdzGFUpk.',
    'expires' => '1273359600',
    'secret' => '0d9F7pxWjM_QakY_51VZqw__',
    'session_key' => '2.I_eTFkcTKSzX5no3jI4r1Q__.3600.1273359600-1677846385',
    'sig' => '9f6ae89510b30dddb3f864f3caf32fb3',
    'uid' => '1677846385'
);

.
.
.

$cookieName = 'fbs_' . self::APP_ID;
$session = self::$VALID_EXPIRED_SESSION;
$_COOKIE[$cookieName] = '"' . http_build_query($session) . '"';

What I don't understand is, how do I get the "access_token", "sig", "session_key" etc? As far as I'm concerned, it should be dynamically exchanged from Facebook and involves user action (logging in).

A: 

I shouldn't answer my own question just to increase my question's popularity, but I really need an answer.

Andree
A: 

I'm not familiar with facebook development at all. However, there is a general pattern called "dependency injection" you might want to look in to.

If you have some object in the class you are testing that needs to be faked, you write your code so that you can pass in the object that needs to be faked. http://en.wikipedia.org/wiki/Dependency_injection

Does this help at all?

Rice Flour Cookies
I've heard about something similiar before, called "mock objects". So in my case, I need to create a fake Facebook SDK, that insteads of querying the Facebook server, it reads from local data?
Andree
Mock objects can be very useful. There are some good libraries for easily generating them. I'm familiar with several libraries for creating mock objects in .net. I suggest you do a Google search for mock objects and dependency injection; see if you can find some examples that are similar to what you're doing.
Rice Flour Cookies