Permalink
Browse files

adding the files back from the previous commit with the new case.

  • Loading branch information...
1 parent 7879a1d commit 1ace7b542f1f0ac065d1ad98af07716764a611dd @gwoo committed Oct 11, 2011
Showing with 201 additions and 0 deletions.
  1. +132 −0 tests/cases/extensions/adapter/data/source/http/GitHubTest.php
  2. +69 −0 tests/mocks/MockGitHubSocket.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace li3_github\tests\cases\extensions\adapter\data\source\http;
+
+use lithium\data\Connections;
+use lithium\data\model\Query;
+use li3_github\models\Issues;
+use li3_github\models\Repos;
+use li3_github\models\Orgs;
+use li3_github\models\Users;
+
+class GitHubTest extends \lithium\test\Unit {
+
+ protected $_models = array(
+ 'issues' => 'li3_github\models\Issues',
+ 'repos' => 'li3_github\models\Repos'
+ );
+
+ public function setUp() {
+ Connections::add('test-gh', array(
+ 'type' => 'http',
+ 'adapter' => 'GitHub',
+ 'login' => '',
+ 'password' => '',
+ 'socket' => 'li3_github\tests\mocks\MockGitHubSocket'
+ ));
+ Issues::config(array('connection' => 'test-gh'));
+ Repos::config(array('connection' => 'test-gh'));
+ Users::config(array('connection' => 'test-gh'));
+ Orgs::config(array('connection' => 'test-gh'));
+ }
+
+ public function testBasicGet() {
+ $gh = Connections::get('test-gh');
+ $headers = array('Content-Type' => 'application/json');
+ $expected = 'User';
+ $results = json_decode(
+ $gh->connection->get('users/octocat', array(), compact('headers'))
+ );
+ $this->assertEqual($expected, $results->type);
+ }
+
+ public function testIssuesRead() {
+ $gh = Connections::get('test-gh');
+ $query = new Query(array('model' => $this->_models['issues']));
+ $results = $gh->read($query);
+ $expected = 'octocat';
+ $result = $results->first();
+ $this->assertEqual($expected, $result->user->login);
+ }
+
+ public function testRepoIssues() {
+ $issues = Repos::issues(array(
+ 'conditions' => array(
+ 'user' => 'octocat', 'repo' => 'Hello-World'
+ )
+ ));
+ $expected = 'octocat';
+ $result = $issues->first();
+ $this->assertEqual($expected, $result->user->login);
+ }
+
+ public function testRepoIssuesCreate() {
+ $data = array(
+ 'title' => 'New Bug',
+ 'body' => 'this is a new bug',
+ );
+ $result = Repos::create($data, array(
+ 'type' => 'issues',
+ 'user' => 'apiheadbanger',
+ 'repo' => 'demo',
+ ));
+ $this->assertTrue($result->save());
+ }
+
+ public function testRepoIssuesWithSortCreatedAsc() {
+ $issues = Repos::issues(array(
+ 'conditions' => array(
+ 'user' => 'octocat', 'repo' => 'Hello-World',
+ 'sort' => 'created', 'direction' => 'asc'
+ )
+ ));
+ $expected = '1347';
+ $result = $issues->first();
+ $this->assertEqual($expected, $result->number);
+ }
+
+ public function testRepoIssuesWithSortCreatedDesc() {
+ $issues = Repos::issues(array(
+ 'conditions' => array(
+ 'user' => 'octocat', 'repo' => 'Hello-World',
+ 'sort' => 'created', 'direction' => 'desc'
+ )
+ ));
+ $result = $issues->first();
+ $this->assertTrue($result->number > 2);
+ }
+
+ public function testUsersRepos() {
+ $repos = Users::repos(array(
+ 'conditions' => array(
+ 'user' => 'octocat'
+ )
+ ));
+
+ $result = $repos->first();
+ $this->assertEqual($result->name, 'Hello-World');
+ }
+
+ public function testOrgsRepos() {
+ $repos = Orgs::repos(array(
+ 'conditions' => array(
+ 'org' => 'octocat'
+ )
+ ));
+ $result = $repos->first();
+ $this->assertEqual($result->name, 'Hello-World');
+ }
+
+ public function testUserOrgs() {
+ $orgs = Users::orgs(array(
+ 'conditions' => array(
+ 'user' => 'octocat'
+ )
+ ));
+
+ $result = $orgs->first();
+ $this->assertEqual($result->login, 'github');
+ $this->assertEqual($result->id, 1);
+ $this->assertEqual($result->url, 'https://api.github.com/orgs/1');
+ }
+}
@@ -0,0 +1,69 @@
+<?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, '/users/octocat/repos') || strpos($url, '/orgs/octocat/repos')) {
+ $json = '/responses/users/repos.json';
+ }
+ if (strpos($url, '/users/octocat/orgs')) {
+ $json = '/responses/users/orgs.json';
+ }
+ if (strpos($url, '/issues')) {
+ $json = '/responses/issues.json';
+ }
+ return file_get_contents(__DIR__ . $json);
+ }
+}
+
+?>

0 comments on commit 1ace7b5

Please sign in to comment.