Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding Orgs and MockGithubSocket.

  • Loading branch information...
commit d9cc31f96b002d8de96ff14dc56ae1e25d9171cc 1 parent 6a784f0
@gwoo authored
View
11 extensions/adapter/data/source/http/GitHub.php
@@ -14,7 +14,12 @@ class GitHub extends \lithium\data\source\Http {
protected $_strings = array(
'issues' => '/issues',
- 'repos' => '/repos/{:user}/{:repo}/{:type}/{:id}'
+ 'repos' => '/repos/{:user}/{:repo}/{:type}/{:id}',
+ 'orgs' => '/orgs/{:org}/{:type}'
+ );
+
+ protected $_params = array(
+ 'user', 'repo', 'type', 'id', 'org'
);
/**
@@ -62,7 +67,9 @@ public function __construct(array $config = array()) {
public function read($query, array $options = array()) {
extract($query->export($this));
$path = $this->_path($source, $conditions);
- unset($conditions['type'], $conditions['user'], $conditions['repo']);
+ foreach ($this->_params as $param) {
+ unset($conditions[$param]);
+ }
$result = $this->connection->get($path, $conditions);
$data = json_decode($result, true);
View
14 models/Orgs.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace li3_github\models;
+
+class Orgs extends \lithium\data\Model {
+
+ protected $_meta = array('connection' => 'github');
+}
+
+Orgs::finder('repos', function($self, $params, $chain) {
+ $params['options']['conditions']['type'] = 'repos';
+ $data = $chain->next($self, $params, $chain);
+ return $data;
+});
View
24 tests/cases/extensions/adapter/data/source/http/GithubTest.php
@@ -18,8 +18,9 @@ public function setUp() {
Connections::add('test-gh', array(
'type' => 'Http',
'adapter' => 'Github',
- 'login' => 'apiheadbanger',
- 'password' => 'testing1',
+ 'login' => '',
+ 'password' => '',
+ 'socket' => 'li3_github\tests\mocks\MockGithubSocket'
));
Issues::config(array('connection' => 'test-gh'));
Repos::config(array('connection' => 'test-gh'));
@@ -30,7 +31,7 @@ public function testBasicGet() {
$headers = array('Content-Type' => 'application/json');
$expected = 'User';
$results = json_decode(
- $gh->connection->get('users/whatever', array(), compact('headers'))
+ $gh->connection->get('users/octocat', array(), compact('headers'))
);
$this->assertEqual($expected, $results->type);
}
@@ -39,17 +40,18 @@ public function testIssuesRead() {
$gh = Connections::get('test-gh');
$query = new Query(array('model' => $this->_models['issues']));
$results = $gh->read($query);
- $expected = array();
- $this->assertEqual($expected, $results);
+ $expected = 'octocat';
+ $result = $results->first();
+ $this->assertEqual($expected, $result->user->login);
}
public function testRepoIssues() {
$issues = Repos::issues(array(
'conditions' => array(
- 'user' => 'apiheadbanger', 'repo' => 'demo'
+ 'user' => 'octocat', 'repo' => 'Hello-World'
)
));
- $expected = 'apiheadbanger';
+ $expected = 'octocat';
$result = $issues->first();
$this->assertEqual($expected, $result->user->login);
}
@@ -70,19 +72,19 @@ public function testRepoIssuesCreate() {
public function testRepoIssuesWithSortCreatedAsc() {
$issues = Repos::issues(array(
'conditions' => array(
- 'user' => 'apiheadbanger', 'repo' => 'demo',
+ 'user' => 'octocat', 'repo' => 'Hello-World',
'sort' => 'created', 'direction' => 'asc'
)
));
- $expected = '1';
+ $expected = '1347';
$result = $issues->first();
$this->assertEqual($expected, $result->number);
}
-
+
public function testRepoIssuesWithSortCreatedDesc() {
$issues = Repos::issues(array(
'conditions' => array(
- 'user' => 'apiheadbanger', 'repo' => 'demo',
+ 'user' => 'octocat', 'repo' => 'Hello-World',
'sort' => 'created', 'direction' => 'desc'
)
));
View
63 tests/mocks/MockGithubSocket.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace li3_github\tests\mocks;
+
+class MockGithubSocket extends \lithium\net\Socket {
+
+ protected $_data = null;
+
+ public function open(array $options = array()) {
+ parent::open($options);
+ return true;
+ }
+
+ public function close() {
+ return true;
+ }
+
+ public function eof() {
+ return true;
+ }
+
+ public function read() {
+ return join("\r\n", array(
+ 'HTTP/1.1 200 OK',
+ 'Header: Value',
+ 'Connection: close',
+ 'Content-Type: text/html;charset=UTF-8',
+ '',
+ $this->_data
+ ));
+
+ // Status: 200 OK
+ // Link: <https://api.github.com/resource?page=2>; rel="next",
+ // <https://api.github.com/resource?page=5>; rel="last"
+ // X-RateLimit-Limit: 5000
+ // X-RateLimit-Remaining: 4999
+ }
+
+ public function write($data) {
+ $url = $data->to('url');
+ return $this->_data = $this->_response($url);
+ }
+
+ public function timeout($time) {
+ return true;
+ }
+
+ public function encoding($charset) {
+ return true;
+ }
+
+ private function _response($url) {
+ if (strpos($url, '/users/octocat')) {
+ $json = '/responses/users/octocat.json';
+ }
+ if (strpos($url, '/issues')) {
+ $json = '/responses/issues.json';
+ }
+ return file_get_contents(__DIR__ . $json);
+ }
+}
+
+?>
View
55 tests/mocks/responses/issues.json
@@ -0,0 +1,55 @@
+[
+ {
+ "url": "https://api.github.com/repos/octocat/Hello-World/issues/1",
+ "html_url": "https://github.com/octocat/Hello-World/issues/1",
+ "number": 1347,
+ "state": "open",
+ "title": "Found a bug",
+ "body": "I'm having a problem with this.",
+ "user": {
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "url": "https://api.github.com/users/octocat"
+ },
+ "labels": [
+ {
+ "url": "https://api.github.com/repos/octocat/Hello-World/labels/bug",
+ "name": "bug",
+ "color": "f29513"
+ }
+ ],
+ "assignee": {
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "url": "https://api.github.com/users/octocat"
+ },
+ "milestone": {
+ "url": "https://api.github.com/repos/octocat/Hello-World/milestones/1",
+ "number": 1,
+ "state": "open",
+ "title": "v1.0",
+ "description": "",
+ "creator": {
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "url": "https://api.github.com/users/octocat"
+ },
+ "open_issues": 4,
+ "closed_issues": 8,
+ "created_at": "2011-04-10T20:09:31Z",
+ "due_on": null
+ },
+ "comments": 0,
+ "pull_request": {
+ "html_url": "https://github.com/octocat/Hello-World/issues/1",
+ "diff_url": "https://github.com/octocat/Hello-World/issues/1.diff",
+ "patch_url": "https://github.com/octocat/Hello-World/issues/1.patch"
+ },
+ "closed_at": null,
+ "created_at": "2011-04-22T13:33:48Z",
+ "updated_at": "2011-04-22T13:33:48Z"
+ }
+]
View
20 tests/mocks/responses/users/octocat.json
@@ -0,0 +1,20 @@
+{
+ "login": "octocat",
+ "id": 1,
+ "avatar_url": "https://github.com/images/error/octocat_happy.gif",
+ "url": "https://api.github.com/users/octocat",
+ "name": "monalisa octocat",
+ "company": "GitHub",
+ "blog": "https://github.com/blog",
+ "location": "San Francisco",
+ "email": "octocat@github.com",
+ "hireable": false,
+ "bio": "There once was...",
+ "public_repos": 2,
+ "public_gists": 1,
+ "followers": 20,
+ "following": 0,
+ "html_url": "https://github.com/octocat",
+ "created_at": "2008-01-14T04:33:35Z",
+ "type": "User"
+}

1 comment on commit d9cc31f

@Tomen

hey @gwoo =)

Did you see that i began work on li3_github too? It would be nice if we would bundle our efforts. Take a look at my repo: https://github.com/Tomen/li3_github
I'll be merging your new commit now.

greetings

Please sign in to comment.
Something went wrong with that request. Please try again.