Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Add unit tests to all Adsense methods. Fix a few bugs in Adsense class.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronschmitz authored and LouisLandry committed Oct 12, 2012
1 parent f5668ce commit 04ac820
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions libraries/joomla/google/data/adsense.php
Expand Up @@ -55,6 +55,7 @@ public function __construct(JRegistry $options = null, JGoogleAuth $auth = null)
*/
protected function listGetData($url, $maxpages = 1, $token = null)
{
$qurl = $url;
if (strpos($url, '&'))
{
$qurl .= '&pageToken=' . $token;
Expand All @@ -64,7 +65,9 @@ protected function listGetData($url, $maxpages = 1, $token = null)
$qurl .= 'pageToken=' . $token;
}
$jdata = $this->auth->query($qurl);
if ($data = json_decode($jdata->body, true) && array_key_exists('items', $data))
$data = json_decode($jdata->body, true);

if ($data && array_key_exists('items', $data))
{
if ($maxpages != 1 && array_key_exists('nextPageToken', $data))
{
Expand Down Expand Up @@ -126,7 +129,7 @@ public function listAccounts($options = array(), $maxpages = 1)
{
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts?' . explode($options, '&');
$url = 'https://www.googleapis.com/adsense/v1.1/accounts?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
Expand All @@ -153,7 +156,7 @@ public function listClients($accountID, $options = array(), $maxpages = 1)
{
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients?' . explode($options, '&');
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
Expand Down Expand Up @@ -215,7 +218,7 @@ public function listUnitChannels($accountID, $adclientID, $adunitID, $options =
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID;
$url .= '/adclients/' . $adclientID . '/adunits/' . $adunitID . '/customchannels?' . explode($options, '&');
$url .= '/adclients/' . $adclientID . '/adunits/' . $adunitID . '/customchannels?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
Expand Down Expand Up @@ -276,7 +279,7 @@ public function listChannels($accountID, $adclientID, $options = array(), $maxpa
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients/' . $adclientID;
$url .= '/customchannels?' . explode($options, '&');
$url .= '/customchannels?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
Expand Down Expand Up @@ -306,7 +309,7 @@ public function listChannelUnits($accountID, $adclientID, $channelID, $options =
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients/' . $adclientID;
$url .= '/customchannels/' . $channelID . '/adunits?' . explode($options, '&');
$url .= '/customchannels/' . $channelID . '/adunits?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
Expand All @@ -318,22 +321,24 @@ public function listChannelUnits($accountID, $adclientID, $channelID, $options =
/**
* Method to generate a report from Google AdSense
*
* @param string $accountID ID of account
* @param array $options Search settings
* @param int $maxpages Maximum number of pages of accounts to return
* @param string $accountID ID of account
* @param string $adclientID ID of client
* @param array $options Search settings
* @param int $maxpages Maximum number of pages of accounts to return
*
* @return mixed Data from Google
*
* @since 1234
* @throws UnexpectedValueException
*/
public function listUrlChannels($accountID, $options = array(), $maxpages = 1)
public function listUrlChannels($accountID, $adclientID, $options = array(), $maxpages = 1)
{
if ($this->authenticated())
{
$next = array_key_exists('nextPageToken', $options) ? $options['nextPage'] : null;
unset($options['nextPageToken']);
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/adclients/' . $adclientID . '/urlchannels?' . explode($options, '&');
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID;
$url .= '/adclients/' . $adclientID . '/urlchannels?' . implode('&', $options);
return $this->listGetData($url, $maxpages, $next);
}
else
Expand All @@ -356,7 +361,7 @@ public function listUrlChannels($accountID, $options = array(), $maxpages = 1)
* @since 1234
* @throws UnexpectedValueException
*/
public function generateReport($accountID, $start, $end, $options = array(), $maxpages = 1)
public function generateReport($accountID, $start, $end = false, $options = array(), $maxpages = 1)
{
if ($this->authenticated())
{
Expand All @@ -378,7 +383,11 @@ public function generateReport($accountID, $start, $end, $options = array(), $ma
throw new InvalidArgumentException('Invalid start time.');
}

if (is_int($end))
if (!$end)
{
$endobj = new DateTime;
}
elseif (is_int($end))
{
$endobj = new DateTime;
$endobj->setTimestamp($end);
Expand All @@ -402,7 +411,7 @@ public function generateReport($accountID, $start, $end, $options = array(), $ma
$begin = array_key_exists('startIndex', $options) ? $options['startIndex'] : 0;
unset($options['startIndex']);

$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/reports?' . explode($options, '&');
$url = 'https://www.googleapis.com/adsense/v1.1/accounts/' . $accountID . '/reports?' . implode('&', $options);
if (strpos($url, '&'))
{
$url .= '&';
Expand All @@ -413,7 +422,9 @@ public function generateReport($accountID, $start, $end, $options = array(), $ma
do
{
$jdata = $this->auth->query($url . 'startIndex=' . count($data['rows']));
if ($newdata = json_decode($jdata->body, true) && array_key_exists('items', $newdata))
$newdata = json_decode($jdata->body, true);

if ($newdata && array_key_exists('rows', $newdata))
{
$newdata['rows'] = array_merge($data['rows'], $newdata['rows']);
$data = $newdata;
Expand Down

0 comments on commit 04ac820

Please sign in to comment.