public function testErrorStatusCanBeCheckedWhenAnErrorResponseIsReturned() { $app = new FacebookApp('123', 'foo_secret'); $graphResponse = '{"error":{"message":"Foo error.","type":"OAuthException","code":190,"error_subcode":463}}'; $response = new FacebookResponse($app, 401, [], $graphResponse); $exception = $response->getThrownException(); $this->assertTrue($response->isError(), 'Expected Response to return an error.'); $this->assertInstanceOf('Facebook\\Exceptions\\FacebookResponseException', $exception); }
public function testUserPropertiesWillGetCastAsGraphUserObjects() { $dataFromGraph = ['id' => '123', 'name' => 'Foo User', 'significant_other' => ['id' => '1337', 'name' => 'Bar User']]; $this->responseMock->shouldReceive('getDecodedBody')->once()->andReturn($dataFromGraph); $factory = new GraphObjectFactory($this->responseMock); $graphObject = $factory->makeGraphUser(); $significantOther = $graphObject->getSignificantOther(); $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphUser', $significantOther); }
public function testPlacePropertyWillGetCastAsGraphPageObject() { $dataFromGraph = ['id' => '123', 'name' => 'Foo Album', 'place' => ['id' => '1', 'name' => 'For Bar Place']]; $this->responseMock->shouldReceive('getDecodedBody')->once()->andReturn($dataFromGraph); $factory = new GraphObjectFactory($this->responseMock); $graphObject = $factory->makeGraphAlbum(); $place = $graphObject->getPlace(); $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphPage', $place); }
public function testLocationPropertyWillGetCastAsGraphLocationObject() { $dataFromGraph = ['id' => '123', 'name' => 'Foo Page', 'location' => ['city' => 'Washington', 'country' => 'United States', 'latitude' => 38.881634205431, 'longitude' => -77.029121075722, 'state' => 'DC']]; $this->responseMock->shouldReceive('getDecodedBody')->once()->andReturn($dataFromGraph); $factory = new GraphObjectFactory($this->responseMock); $graphObject = $factory->makeGraphPage(); $location = $graphObject->getLocation(); $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphLocation', $location); }
public function testDatesGetCastToDateTime() { $dataFromGraph = ['expires_at' => 123, 'issued_at' => 1337]; $this->responseMock->shouldReceive('getDecodedBody')->once()->andReturn($dataFromGraph); $factory = new GraphObjectFactory($this->responseMock); $graphObject = $factory->makeGraphSessionInfo(); $expires = $graphObject->getExpiresAt(); $issuedAt = $graphObject->getIssuedAt(); $this->assertInstanceOf('DateTime', $expires); $this->assertInstanceOf('DateTime', $issuedAt); }
public function testACollectionWillBeCastRecursively() { $someUser = ['id' => '123', 'name' => 'Foo McBar']; $likesCollection = ['data' => [['id' => '1', 'name' => 'Sammy Kaye Powers', 'is_sexy' => true], ['id' => '2', 'name' => 'Yassine Guedidi', 'is_sexy' => true], ['id' => '3', 'name' => 'Fosco Marotto', 'is_sexy' => true], ['id' => '4', 'name' => 'Foo McUgly', 'is_sexy' => false]], 'paging' => ['next' => 'http://facebook/next_likes', 'previous' => 'http://facebook/prev_likes']]; $commentsCollection = ['data' => [['id' => '42_1', 'from' => $someUser, 'message' => 'Foo comment.', 'created_time' => '2014-07-15T03:54:34+0000', 'likes' => $likesCollection], ['id' => '42_2', 'from' => $someUser, 'message' => 'Bar comment.', 'created_time' => '2014-07-15T04:11:24+0000', 'likes' => $likesCollection]], 'paging' => ['next' => 'http://facebook/next_comments', 'previous' => 'http://facebook/prev_comments']]; $dataFromGraph = ['data' => [['id' => '1337_1', 'from' => $someUser, 'story' => 'Some great foo story.', 'likes' => $likesCollection, 'comments' => $commentsCollection], ['id' => '1337_2', 'from' => $someUser, 'to' => ['data' => [$someUser]], 'message' => 'Some great bar message.', 'likes' => $likesCollection, 'comments' => $commentsCollection]], 'paging' => ['next' => 'http://facebook/next', 'previous' => 'http://facebook/prev']]; $this->responseMock->shouldReceive('getDecodedBody')->once()->andReturn($dataFromGraph); $factory = new GraphObjectFactory($this->responseMock); $graphObject = $factory->makeGraphList(); $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphList', $graphObject); // Story $storyObject = $graphObject[0]; $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphObject', $storyObject['from']); $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphList', $storyObject['likes']); $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphList', $storyObject['comments']); // Story Comments $storyComments = $storyObject['comments']; $firstStoryComment = $storyComments[0]; $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphObject', $firstStoryComment['from']); // Message $messageObject = $graphObject[1]; $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphList', $messageObject['to']); $toUsers = $messageObject['to']; $this->assertInstanceOf('\\Facebook\\GraphNodes\\GraphObject', $toUsers[0]); }
/** * Creates a new Response entity. * * @param FacebookResponse $response */ public function __construct(FacebookResponse $response) { $app = $response->getApp(); $httpStatusCode = $response->getHttpStatusCode(); $headers = $response->getHeaders(); $body = $response->getBody(); $accessToken = $response->getAccessToken(); parent::__construct($app, $httpStatusCode, $headers, $body, $accessToken); $responses = $response->getDecodedBody(); $this->setResponses($responses); }
/** * Init this Graph object. * * @param FacebookResponse $response The response entity from Graph. */ public function __construct(FacebookResponse $response) { $this->decodedBody = $response->getDecodedBody(); }
/** * Makes the request to Graph and returns the result. * * @param FacebookRequest $request * * @return FacebookResponse * * @throws FacebookSDKException */ public function sendRequest(FacebookRequest $request) { if (get_class($request) === 'FacebookRequest') { $request->validateAccessToken(); } $url = $this->getBaseGraphUrl() . $request->getUrl(); $method = $request->getMethod(); $params = $request->getPostParams(); $headers = $request->getHeaders(); // Should throw `FacebookSDKException` exception on HTTP client error. // Don't catch to allow it to bubble up. $response = $this->httpClientHandler->send($url, $method, $params, $headers); static::$requestCount++; $httpResponseCode = $this->httpClientHandler->getResponseHttpStatusCode(); $httpResponseHeaders = $this->httpClientHandler->getResponseHeaders(); $accessToken = $request->getAccessToken(); $app = $request->getApp(); $returnResponse = new FacebookResponse($app, $httpResponseCode, $httpResponseHeaders, $response, $accessToken); if ($returnResponse->isError()) { throw $returnResponse->getThrownException(); } return $returnResponse; }