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).
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?