Skip to content

Add traffic #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Github\Api\Repository\Labels;
use Github\Api\Repository\Stargazers;
use Github\Api\Repository\Statuses;
use Github\Api\Repository\Traffic;

/**
* Searching repositories, getting repository information
Expand Down Expand Up @@ -549,4 +550,9 @@ public function projects()
{
return new Projects($this->client);
}

public function traffic()
{
return new Traffic($this->client);
}
}
63 changes: 63 additions & 0 deletions lib/Github/Api/Repository/Traffic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace Github\Api\Repository;

use Github\Api\AbstractApi;
use Github\Exception\MissingArgumentException;

/**
* @link https://developer.github.com/v3/repos/traffic/
* @author Miguel Piedrafita <soy@miguelpiedrafita.com>
*/
class Traffic extends AbstractApi
{
/**
* @link https://developer.github.com/v3/repos/traffic/#list-referrers
*
* @param string $owner
* @param string $repository
*
* @return array
*/
public function referers($owner, $repository)
{
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/popular/referrers');
}
/**
* @link https://developer.github.com/v3/repos/traffic/#list-paths
*
* @param string $owner
* @param string $repository
*
* @return array
*/
public function paths($owner, $repository)
{
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/popular/paths');
}
/**
* @link https://developer.github.com/v3/repos/traffic/#views
*
* @param string $owner
* @param string $repository
* @param string|day $per
*
* @return array
*/
public function views($owner, $repository, $per = 'day')
{
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/views?per='.rawurlencode($per));
}
/**
* @link https://developer.github.com/v3/repos/traffic/#clones
*
* @param string $owner
* @param string $repository
* @param string|day $per
*
* @return array
*/
public function clones($owner, $repository, $per = 'day')
{
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/clones?per='.rawurlencode($per));
}
}
79 changes: 79 additions & 0 deletions test/Github/Tests/Api/Repository/TrafficTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use Github\Tests\Api\TestCase;

class TrafficTest extends TestCase
{

/**
* @test
*/
public function shouldgetReferers()
{
$expectedValue = json_encode(["referrer" => "github.com","count" => 112,"uniques" => 15]);

$api = $this->getApiMock();

$api->expects($this->once())
->method('get')
->with('/repos/knplabs/php-github-api/traffic/popular/referrers')
->will($this->returnValue($expectedValue));

$result = $api->referers('knplabs', 'php-github-api');

$this->assertEquals($expectedValue, $result);
}

public function shouldgetPaths()
{
$expectedValue = json_encode(["path" => "/knplabs/php-github-api","title" => "KnpLabs/php-github-api: A simple PHP GitHub API client, Object Oriented, tested and documented. For 5.5+.","count" => 203,"uniques" => 54]);

$api = $this->getApiMock();

$api->expects($this->once())
->method('get')
->with('/repos/knplabs/php-github-api/traffic/popular/paths')
->will($this->returnValue($expectedValue));

$result = $api->paths('knplabs', 'php-github-api');

$this->assertEquals($expectedValue, $result);
}

public function shouldgetViews()
{
$expectedValue = json_encode(["count" => 813,"uniques" => 61,"views" => [["timestamp" => "2017-03-12T00:00:00Z","count" => 40,"uniques" => 3]]]);

$api = $this->getApiMock();

$api->expects($this->once())
->method('get')
->with('/repos/knplabs/php-github-api/traffic/views?per=day')
->will($this->returnValue($expectedValue));

$result = $api->views('knplabs', 'php-github-api');

$this->assertEquals($expectedValue, $result);
}

public function shouldgetClones()
{
$expectedValue = json_encode(["count" => 813,"uniques" => 61,"clones" => [["timestamp" => "2017-03-12T00:00:00Z","count" => 14,"uniques" => 8]]]);

$api = $this->getApiMock();

$api->expects($this->once())
->method('get')
->with('/repos/knplabs/php-github-api/traffic/clones?per=day')
->will($this->returnValue($expectedValue));

$result = $api->clones('knplabs', 'php-github-api');

$this->assertEquals($expectedValue, $result);
}

protected function getApiClass()
{
return \Github\Api\Repository\Traffic::class;
}
}