Permalink
Browse files

Port joomla/joomla-platform#1831

  • Loading branch information...
1 parent 86ce751 commit 7d5de65acaca7015b8975a7eb7a763d91fdff959 @mbabker mbabker committed Mar 3, 2013
@@ -22,6 +22,7 @@
* @property-read Statuses $statuses GitHub API object for commits.
* @property-read Account $account GitHub API object for account references.
* @property-read Hooks $hooks GitHub API object for hooks.
+ * @property-read Meta $meta GitHub API object for meta.
*
* @package Joomla\Framework
* @since 1.0
@@ -101,6 +102,12 @@ class Github
protected $hooks;
/**
+ * @var Meta GitHub API object for meta.
+ * @since 1.0
+ */
+ protected $meta;
+
+ /**
* Constructor.
*
* @param Registry $options GitHub options object.
@@ -0,0 +1,55 @@
+<?php
+/**
+ * @package Joomla\Framework
+ * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+namespace Joomla\Github;
+
+/**
+ * GitHub API Meta class for the Joomla Framework.
+ *
+ * @package Joomla\Framework
+ * @since 1.0
+ */
+class Meta extends Object
+{
+ /**
+ * Method to get the authorized IP addresses for services
+ *
+ * @return array Authorized IP addresses
+ *
+ * @since 1.0
+ */
+ public function getMeta()
+ {
+ // Build the request path.
+ $path = '/meta';
+
+ $githubIps = $this->processResponse($this->client->get($this->fetchUrl($path)), 200);
+
+ /*
+ * The response body returns the IP addresses in CIDR format
+ * Decode the response body and strip the subnet mask information prior to
+ * returning the data to the user. We're assuming quite a bit here that all
+ * masks will be /32 as they are as of the time of development.
+ */
+
+ $authorizedIps = array();
+
+ foreach ($githubIps as $key => $serviceIps)
+ {
+ // The first level contains an array of IPs based on the service
+ $authorizedIps[$key] = array();
+
+ foreach ($serviceIps as $serviceIp)
+ {
+ // The second level is each individual IP address, strip the mask here
+ $authorizedIps[$key][] = substr($serviceIp, 0, -3);
+ }
+ }
+
+ return $authorizedIps;
+ }
+}
@@ -7,26 +7,27 @@
namespace Joomla\Github;
+use Joomla\Http\Response;
use Joomla\Uri\Uri;
use Joomla\Registry\Registry;
/**
* GitHub API object class for the Joomla Platform.
*
* @package Joomla\Framework
- * @since 11.3
+ * @since 1.0
*/
abstract class Object
{
/**
* @var Registry Options for the GitHub object.
- * @since 11.3
+ * @since 1.0
*/
protected $options;
/**
* @var Http The HTTP client object to use in sending HTTP requests.
- * @since 11.3
+ * @since 1.0
*/
protected $client;
@@ -36,7 +37,7 @@
* @param Registry $options GitHub options object.
* @param Http $client The HTTP client object.
*
- * @since 11.3
+ * @since 1.0
*/
public function __construct(Registry $options = null, Http $client = null)
{
@@ -55,11 +56,11 @@ public function __construct(Registry $options = null, Http $client = null)
*
* @return string The request URL.
*
- * @since 11.3
+ * @since 1.0
*/
protected function fetchUrl($path, $page = 0, $limit = 0)
{
- // Get a new JUri object fousing the api url and given path.
+ // Get a new Uri object fousing the api url and given path.
$uri = new Uri($this->options->get('api.url') . $path);
if ($this->options->get('api.username', false))
@@ -86,4 +87,28 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
return (string) $uri;
}
+
+ /**
+ * Process the response and decode it.
+ *
+ * @param Response $response The response.
+ * @param integer $expectedCode The expected "good" code.
+ *
+ * @return mixed
+ *
+ * @since 1.0
+ * @throws \DomainException
+ */
+ protected function processResponse(Response $response, $expectedCode = 200)
+ {
+ // Validate the response code.
+ if ($response->code != $expectedCode)
+ {
+ // Decode the error response and throw an exception.
+ $error = json_decode($response->body);
+ throw new \DomainException($error->message, $response->code);
+ }
+
+ return json_decode($response->body);
+ }
}
@@ -216,6 +216,21 @@ public function test__GetHooks()
}
/**
+ * Tests the magic __get method - meta
+ *
+ * @since 1.0
+ *
+ * @return void
+ */
+ public function test__GetMeta()
+ {
+ $this->assertThat(
+ $this->object->meta,
+ $this->isInstanceOf('Joomla\Github\Meta')
+ );
+ }
+
+ /**
* Tests the magic __get method - failure
*
* @since 11.3
@@ -0,0 +1,125 @@
+<?php
+/**
+ * @package Joomla\Framework\Test
+ * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+namespace Joomla\Github\Tests;
+
+use Joomla\Github\Meta;
+use Joomla\Registry\Registry;
+
+/**
+ * Test class for Joomla\Github\Meta.
+ *
+ * @package Joomla\Framework\Test
+ * @since 1.0
+ */
+class MetaTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var Registry Options for the GitHub object.
+ * @since 1.0
+ */
+ protected $options;
+
+ /**
+ * @var \Joomla\Github\Http Mock client object.
+ * @since 1.0
+ */
+ protected $client;
+
+ /**
+ * @var \Joomla\Http\Response Mock response object.
+ * @since 1.0
+ */
+ protected $response;
+
+ /**
+ * @var Meta Object under test.
+ * @since 1.0
+ */
+ protected $object;
+
+ /**
+ * @var string Sample JSON string.
+ * @since 1.0
+ */
+ protected $sampleString = '{"hooks":["127.0.0.1/32","192.168.1.1/32"],"git":["127.0.0.1/32"]}';
+
+ /**
+ * @var string Sample JSON error message.
+ * @since 1.0
+ */
+ protected $errorString = '{"message": "Generic Error"}';
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * @return void
+ *
+ * @since 1.0
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->options = new Registry;
+ $this->client = $this->getMock('\\Joomla\\Github\\Http', array('get', 'post', 'delete', 'patch', 'put'));
+ $this->response = $this->getMock('\\Joomla\\Http\\Response');
+
+ $this->object = new Meta($this->options, $this->client);
+ }
+
+ /**
+ * Tests the getMeta method
+ *
+ * @return void
+ *
+ * @since 1.0
+ */
+ public function testGetMeta()
+ {
+ $this->response->code = 200;
+ $this->response->body = $this->sampleString;
+
+ $decodedResponse = array(
+ 'hooks' => array('127.0.0.1', '192.168.1.1'),
+ 'git' => array('127.0.0.1')
+ );
+
+ $this->client->expects($this->once())
+ ->method('get')
+ ->with('/meta')
+ ->will($this->returnValue($this->response));
+
+ $this->assertThat(
+ $this->object->getMeta(),
+ $this->equalTo($decodedResponse)
+ );
+ }
+
+ /**
+ * Tests the getMeta method - failure
+ *
+ * @return void
+ *
+ * @since 1.0
+ *
+ * @expectedException DomainException
+ */
+ public function testGetMetaFailure()
+ {
+ $this->response->code = 500;
+ $this->response->body = $this->errorString;
+
+ $this->client->expects($this->once())
+ ->method('get')
+ ->with('/meta')
+ ->will($this->returnValue($this->response));
+
+ $this->object->getMeta();
+ }
+}

0 comments on commit 7d5de65

Please sign in to comment.