View source
<?php
require_once 'seq_share_count.php';
require_once 'http.php';
class ShareaholicSeqShareCountsTest extends PHPUnit_Framework_TestCase {
public function setUp() {
$this->url = 'https://www.google.com';
$this->services = array_keys(ShareaholicSeqShareCount::get_services_config());
$this->options = array();
$this->share_count = new ShareaholicSeqShareCount($this->url, $this->services, $this->options);
$this->response = array(
'response' => array(
'code' => 200,
),
);
}
public function tearDown() {
}
public function testHasHttpError() {
$response = NULL;
$this
->assertTrue($this->share_count
->has_http_error($response), 'should return true for null response');
$response = array();
$this
->assertTrue($this->share_count
->has_http_error($response), 'should return true for empty response');
$response = array(
'response' => array(
'code' => 200,
),
'body' => '',
);
$this
->assertFalse($this->share_count
->has_http_error($response), 'should return false');
}
public function testGetClientIp() {
$expected = '12345';
$_SERVER['HTTP_CLIENT_IP'] = $expected;
$result = $this->share_count
->get_client_ip();
$this
->assertEquals($expected, $result);
$expected = '23456';
$_SERVER['HTTP_CLIENT_IP'] = '';
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
$result = $this->share_count
->get_client_ip();
$this
->assertEquals($expected, $result);
$expected = '34567';
$_SERVER['HTTP_CLIENT_IP'] = '';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '';
$_SERVER['REMOTE_ADDR'] = $expected;
$result = $this->share_count
->get_client_ip();
$this
->assertEquals($expected, $result);
}
public function testFacebookCountCallback() {
$json = '{ "id": "https://blog.shareaholic.com", "shares": 16 }';
$this->response['body'] = $json;
$facebook_count = $this->share_count
->facebook_count_callback($this->response);
$this
->assertEquals(16, $facebook_count, 'It should get the correct fb count');
}
public function testLinkedinCountCallback() {
$json = '{"count":8,"fCnt":"8","fCntPlusOne":"9","url":"https:\\/\\/blog.shareaholic.com\\/"}';
$this->response['body'] = $json;
$linkedin_count = $this->share_count
->linkedin_count_callback($this->response);
$this
->assertEquals(8, $linkedin_count, 'It should get the correct linkedin count');
}
public function testGoogleplusCountCallback() {
$json = '[{"id": "p", "result": {"kind": "pos#plusones", "id": "https://blog.shareaholic.com/", "isSetByViewer": false, "metadata": {"type": "URL", "globalCounts": {"count": 10.0}}}}]';
$this->response['body'] = $json;
$google_plus_count = $this->share_count
->google_plus_count_callback($this->response);
$this
->assertEquals(10, $google_plus_count, 'It should get the correct google_plus count');
$json = '{"test": "test"}';
$this->response['body'] = $json;
$google_plus_count = $this->share_count
->google_plus_count_callback($this->response);
$this
->assertEquals(0, $google_plus_count, 'It should return zero google plus count for unexpected JSON');
$json = 'hello';
$this->response['body'] = $json;
$google_plus_count = $this->share_count
->google_plus_count_callback($this->response);
$this
->assertEquals(0, $google_plus_count, 'It should return zero google plus count for non JSON');
}
public function testPinterestCountCallback() {
$body = 'f({"count": 1, "url": "https://blog.shareaholic.com"})';
$this->response['body'] = $body;
$count = $this->share_count
->pinterest_count_callback($this->response);
$this
->assertEquals(1, $count, 'It should get the correct pinterest count');
}
public function testBufferCountCallback() {
$body = '{"shares":3}';
$this->response['body'] = $body;
$count = $this->share_count
->buffer_count_callback($this->response);
$this
->assertEquals(3, $count, 'It should get the correct buffer count');
}
public function testStumbleuponCountCallback() {
$body = '{"result":{"url":"https:\\/\\/blog.shareaholic.com\\/","in_index":true,"publicid":"1Qat7p","views":1,"title":"Blog \\/ Shareaholic (@shareaholic)","thumbnail":"http:\\/\\/cdn.stumble-upon.com\\/mthumb\\/672\\/157433672.jpg","thumbnail_b":"http:\\/\\/cdn.stumble-upon.com\\/bthumb\\/672\\/157433672.jpg","submit_link":"http:\\/\\/www.stumbleupon.com\\/submit\\/?url=https:\\/\\/blog.shareaholic.com\\/","badge_link":"http:\\/\\/www.stumbleupon.com\\/badge\\/?url=https:\\/\\/blog.shareaholic.com\\/","info_link":"http:\\/\\/www.stumbleupon.com\\/url\\/https%253A\\/\\/blog.shareaholic.com\\/"},"timestamp":1394771877,"success":true}';
$this->response['body'] = $body;
$count = $this->share_count
->stumbleupon_count_callback($this->response);
$this
->assertEquals(1, $count, 'It should get the correct stumbleupon count');
}
public function testRedditCountCallback() {
$body = '{"kind": "Listing", "data": {"modhash": "", "children": [{"kind": "t3", "data": {"domain": "reddit.com", "banned_by": null, "likes": null, "clicked": false, "stickied": false, "score": 1, "downs": 0, "url": "http://reddit.com", "ups": 1, "num_comments": 0, "distinguished": null}}], "after": null, "before": null}}';
$this->response['body'] = $body;
$count = $this->share_count
->reddit_count_callback($this->response);
$this
->assertEquals(1, $count, 'It should get the correct reddit count');
}
public function testVkCountCallback() {
$body = 'VK.Share.count(0, 3781);';
$this->response['body'] = $body;
$count = $this->share_count
->vk_count_callback($this->response);
$this
->assertEquals(3781, $count, 'It should get the correct vk count');
}
public function testOdnoklassnikiCountCallback() {
$body = "ODKL.updateCount('odklcnt0','1');";
$this->response['body'] = $body;
$count = $this->share_count
->odnoklassniki_count_callback($this->response);
$this
->assertEquals(1, $count, 'It should get the correct odnoklassniki count');
}
public function testYummlyCountCallback() {
$body = '{"count":760}';
$this->response['body'] = $body;
$count = $this->share_count
->yummly_count_callback($this->response);
$this
->assertEquals(760, $count, 'It should get the correct yummly count');
}
public function testFancyCountCallback() {
$body = '__FIB.collectCount({"url": "http://www.google.com", "count": 26, "thing_url": "http://fancy.com/things/263001623", "showcount": 1});';
$this->response['body'] = $body;
$count = $this->share_count
->fancy_count_callback($this->response);
$this
->assertEquals(26, $count, 'It should get the correct fancy count');
}
public function testGooglePlusPrepareRequest() {
$config = ShareaholicSeqShareCount::get_services_config();
$this->share_count
->google_plus_prepare_request($this->url, $config);
$this
->assertNotNull($config['google_plus']['body'], 'The post body for google plus should not be null');
$mockIp = 'mockIp';
$_SERVER['REMOTE_ADDR'] = $mockIp;
$this->share_count
->google_plus_prepare_request($this->url, $config);
$this
->assertEquals($mockIp, $config['google_plus']['body'][0]['params']['userIp']);
}
public function testGetCount() {
$response = $this->share_count
->get_counts();
$this
->assertNotNull($response, 'The response array should not be null');
foreach ($this->services as $service) {
$this
->assertNotNull($response['data'][$service], 'The ' . $service . ' count should not be null');
}
}
public function testMissingServices() {
$share_count = new ShareaholicSeqShareCount('https://dev.losloslos.com/', $this->services, $this->options);
$response = $share_count
->get_counts();
$this
->assertNotNull($response, 'The response array should not be null');
$this
->assertNull($response['data']['facebook'], 'The facebook count should be null');
}
public function testIsUrlEncoded() {
$url = 'http://eatnabout.com/2014/03/29/bestie/#more-10144';
$encoded_url = 'http%3A%2F%2Featnabout.com%2F2014%2F03%2F29%2Fbestie%2F%23more-10144';
$this
->assertTrue($this->share_count
->is_url_encoded($encoded_url), 'It should return true when the url is encoded');
$this
->assertFalse($this->share_count
->is_url_encoded($url), 'It should return false when the url is not encoded');
}
public function testRawResponseObject() {
$response = $this->share_count
->get_counts();
$this
->assertNotNull($this->share_count->raw_response, 'The raw response object should not be null');
foreach ($this->services as $service) {
$this
->assertNotNull($this->share_count->raw_response[$service], 'The raw response for ' . $service . ' should not be null');
}
}
}