Skip to content

Commit dcfd458

Browse files
authored
Merge pull request #39 from bufferapp/fix/support-metric-type-batch
Support metric_type option which is necessary for the views metric
2 parents d5158bc + 2b96551 commit dcfd458

2 files changed

Lines changed: 109 additions & 7 deletions

File tree

Facebook/Facebook.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,22 @@ public function getPagePostInsightsMetricData($pageId, $postId, $insightsMetrics
230230
/*
231231
* Get page batch posts insights data for the given posts and metrics.
232232
*/
233-
public function getPageBatchPostsInsightsMetricData($postIds, $insightsMetrics)
233+
public function getPageBatchPostsInsightsMetricData($postIds, $insightsMetrics, $metric_type = null)
234234
{
235235
$result = [];
236236
$batchRequests = [];
237237
$insightsMetricsString = join(",", $insightsMetrics);
238+
$params = [
239+
"metric" => $insightsMetricsString,
240+
"period" => "lifetime",
241+
"until" => strtotime("now"),
242+
];
243+
244+
if (!is_null($metric_type)) {
245+
$params["metric_type"] = $metric_type;
246+
}
238247
foreach ($postIds as $postId) {
239-
$request = $this->createRequest("GET", "/{$postId}/insights", [
240-
"metric" => $insightsMetricsString,
241-
"period" => "lifetime",
242-
"until" => strtotime("now"),
243-
]);
248+
$request = $this->createRequest("GET", "/{$postId}/insights", $params);
244249
$batchRequests[$postId] = $request;
245250
}
246251

tests/FacebookTest.php

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function testGetPageInsightsAudienceDataEmptyResponse()
431431
$responseMock = m::mock('\Facebook\FacebookResponse')
432432
->shouldReceive('getDecodedBody')
433433
->once()
434-
->andReturn($decodedAudienceData)
434+
->andReturn($decodedAudienceData)
435435
->getMock();
436436
$facebookMock = m::mock('\Facebook\Facebook');
437437
$facebookMock->shouldReceive('sendRequest')->once()->andReturn($responseMock);
@@ -708,6 +708,103 @@ public function testGetPageBatchPostsInsightsMetricsData()
708708
$this->assertEquals($graphData['33333_444444']['post_consumptions'], 42);
709709
}
710710

711+
public function testGetPageBatchPostsInsightsMetricDataWithTotalValues()
712+
{
713+
$facebook = new Facebook();
714+
715+
$decodedResponseData1 = [
716+
'data' => [
717+
[
718+
'name' => 'post_impressions',
719+
'period' => 'lifetime',
720+
'total_value' => 150,
721+
'values' => [['value' => 150]]
722+
],
723+
[
724+
'name' => 'post_fan_reach',
725+
'period' => 'lifetime',
726+
'total_value' => 300,
727+
'values' => [['value' => 300]]
728+
],
729+
[
730+
'name' => 'post_consumptions',
731+
'period' => 'lifetime',
732+
'total_value' => 75,
733+
'values' => [['value' => 75]]
734+
],
735+
]
736+
];
737+
738+
$decodedResponseData2 = [
739+
'data' => [
740+
[
741+
'name' => 'post_impressions',
742+
'period' => 'lifetime',
743+
'total_value' => 250,
744+
'values' => [['value' => 250]]
745+
],
746+
[
747+
'name' => 'post_fan_reach',
748+
'period' => 'lifetime',
749+
'total_value' => 500,
750+
'values' => [['value' => 500]]
751+
],
752+
[
753+
'name' => 'post_consumptions',
754+
'period' => 'lifetime',
755+
'total_value' => 125,
756+
'values' => [['value' => 125]]
757+
],
758+
]
759+
];
760+
761+
$responseMock1 = m::mock('\Facebook\FacebookResponse')
762+
->shouldReceive('getDecodedBody')
763+
->once()
764+
->andReturn($decodedResponseData1)
765+
->getMock();
766+
$responseMock2 = m::mock('\Facebook\FacebookResponse')
767+
->shouldReceive('getDecodedBody')
768+
->once()
769+
->andReturn($decodedResponseData2)
770+
->getMock();
771+
772+
$getIteratorMock = new ArrayIterator([
773+
"11111_22222" => $responseMock1,
774+
"33333_444444" => $responseMock2
775+
]);
776+
777+
$responseBatchMock = m::mock('\Facebook\FacebookBatchResponse')
778+
->shouldReceive('getIterator')
779+
->once()
780+
->andReturn($getIteratorMock)
781+
->getMock();
782+
783+
$requestMock1 = m::mock('\Facebook\FacebookRequest');
784+
$requestMock2 = m::mock('\Facebook\FacebookRequest');
785+
786+
$postIds = ['11111_22222', '33333_444444'];
787+
$facebookMock = m::mock('\Facebook\Facebook');
788+
$facebook->setFacebookLibrary($facebookMock);
789+
790+
$facebookMock->shouldReceive('request')->twice()->andReturn($requestMock1, $requestMock2);
791+
$facebookMock->shouldReceive('sendBatchRequest')->once()->andReturn($responseBatchMock);
792+
793+
$graphData = $facebook->getPageBatchPostsInsightsMetricData(
794+
$postIds,
795+
['post_impressions', 'post_fan_reach', 'post_consumptions'],
796+
'total_value'
797+
);
798+
799+
$this->assertEquals($graphData['11111_22222']['post_impressions'], 150);
800+
$this->assertEquals($graphData['11111_22222']['post_fan_reach'], 300);
801+
$this->assertEquals($graphData['11111_22222']['post_consumptions'], 75);
802+
803+
$this->assertEquals($graphData['33333_444444']['post_impressions'], 250);
804+
$this->assertEquals($graphData['33333_444444']['post_fan_reach'], 500);
805+
$this->assertEquals($graphData['33333_444444']['post_consumptions'], 125);
806+
}
807+
711808
public function testGetPagePostInsightsMetricDataReturnsEmptyOnNullResponse()
712809
{
713810
$facebook = new Facebook();

0 commit comments

Comments
 (0)