Skip to content

Commit 9f0cf3a

Browse files
m1guelpfNyholm
authored andcommitted
Add traffic (#548)
* Create Traffic.php * Add Clones * Apply fixes from StyleCI [ci skip] [skip ci] * Add Traffic to Repo * Add Traffic * Apply fixes from StyleCI [ci skip] [skip ci] * list() -> referers() * Test skeleton * Apply fixes from StyleCI [ci skip] [skip ci] * Add per * Remove // * Fix Syntax Error * Add * Syntax * Add Tests
1 parent 4eb1e2e commit 9f0cf3a

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

lib/Github/Api/Repo.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Github\Api\Repository\Labels;
1717
use Github\Api\Repository\Stargazers;
1818
use Github\Api\Repository\Statuses;
19+
use Github\Api\Repository\Traffic;
1920

2021
/**
2122
* Searching repositories, getting repository information
@@ -562,4 +563,9 @@ public function projects()
562563
{
563564
return new Projects($this->client);
564565
}
566+
567+
public function traffic()
568+
{
569+
return new Traffic($this->client);
570+
}
565571
}

lib/Github/Api/Repository/Traffic.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace Github\Api\Repository;
3+
4+
use Github\Api\AbstractApi;
5+
use Github\Exception\MissingArgumentException;
6+
7+
/**
8+
* @link https://developer.github.com/v3/repos/traffic/
9+
* @author Miguel Piedrafita <soy@miguelpiedrafita.com>
10+
*/
11+
class Traffic extends AbstractApi
12+
{
13+
/**
14+
* @link https://developer.github.com/v3/repos/traffic/#list-referrers
15+
*
16+
* @param string $owner
17+
* @param string $repository
18+
*
19+
* @return array
20+
*/
21+
public function referers($owner, $repository)
22+
{
23+
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/popular/referrers');
24+
}
25+
/**
26+
* @link https://developer.github.com/v3/repos/traffic/#list-paths
27+
*
28+
* @param string $owner
29+
* @param string $repository
30+
*
31+
* @return array
32+
*/
33+
public function paths($owner, $repository)
34+
{
35+
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/popular/paths');
36+
}
37+
/**
38+
* @link https://developer.github.com/v3/repos/traffic/#views
39+
*
40+
* @param string $owner
41+
* @param string $repository
42+
* @param string|day $per
43+
*
44+
* @return array
45+
*/
46+
public function views($owner, $repository, $per = 'day')
47+
{
48+
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/views?per='.rawurlencode($per));
49+
}
50+
/**
51+
* @link https://developer.github.com/v3/repos/traffic/#clones
52+
*
53+
* @param string $owner
54+
* @param string $repository
55+
* @param string|day $per
56+
*
57+
* @return array
58+
*/
59+
public function clones($owner, $repository, $per = 'day')
60+
{
61+
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/traffic/clones?per='.rawurlencode($per));
62+
}
63+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Github\Tests\Api\TestCase;
4+
5+
class TrafficTest extends TestCase
6+
{
7+
8+
/**
9+
* @test
10+
*/
11+
public function shouldgetReferers()
12+
{
13+
$expectedValue = json_encode(["referrer" => "github.com","count" => 112,"uniques" => 15]);
14+
15+
$api = $this->getApiMock();
16+
17+
$api->expects($this->once())
18+
->method('get')
19+
->with('/repos/knplabs/php-github-api/traffic/popular/referrers')
20+
->will($this->returnValue($expectedValue));
21+
22+
$result = $api->referers('knplabs', 'php-github-api');
23+
24+
$this->assertEquals($expectedValue, $result);
25+
}
26+
27+
public function shouldgetPaths()
28+
{
29+
$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]);
30+
31+
$api = $this->getApiMock();
32+
33+
$api->expects($this->once())
34+
->method('get')
35+
->with('/repos/knplabs/php-github-api/traffic/popular/paths')
36+
->will($this->returnValue($expectedValue));
37+
38+
$result = $api->paths('knplabs', 'php-github-api');
39+
40+
$this->assertEquals($expectedValue, $result);
41+
}
42+
43+
public function shouldgetViews()
44+
{
45+
$expectedValue = json_encode(["count" => 813,"uniques" => 61,"views" => [["timestamp" => "2017-03-12T00:00:00Z","count" => 40,"uniques" => 3]]]);
46+
47+
$api = $this->getApiMock();
48+
49+
$api->expects($this->once())
50+
->method('get')
51+
->with('/repos/knplabs/php-github-api/traffic/views?per=day')
52+
->will($this->returnValue($expectedValue));
53+
54+
$result = $api->views('knplabs', 'php-github-api');
55+
56+
$this->assertEquals($expectedValue, $result);
57+
}
58+
59+
public function shouldgetClones()
60+
{
61+
$expectedValue = json_encode(["count" => 813,"uniques" => 61,"clones" => [["timestamp" => "2017-03-12T00:00:00Z","count" => 14,"uniques" => 8]]]);
62+
63+
$api = $this->getApiMock();
64+
65+
$api->expects($this->once())
66+
->method('get')
67+
->with('/repos/knplabs/php-github-api/traffic/clones?per=day')
68+
->will($this->returnValue($expectedValue));
69+
70+
$result = $api->clones('knplabs', 'php-github-api');
71+
72+
$this->assertEquals($expectedValue, $result);
73+
}
74+
75+
protected function getApiClass()
76+
{
77+
return \Github\Api\Repository\Traffic::class;
78+
}
79+
}

0 commit comments

Comments
 (0)