Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add JGithubMeta object to interact with Meta API endpoint

  • Loading branch information...
commit 20ec48ee5eb7c2d2cfba10f2eed5a290af449f32 1 parent 9a88cff
Michael Babker mbabker authored
16 libraries/joomla/github/github.php
View
@@ -23,6 +23,7 @@
* @property-read JGithubAccount $account GitHub API object for account references.
* @property-read JGithubHooks $hooks GitHub API object for hooks.
* @property-read JGithubUsers $users GitHub API object for users.
+ * @property-read JGithubMeta $meta GitHub API object for meta.
*
* @package Joomla.Platform
* @subpackage GitHub
@@ -109,6 +110,12 @@ class JGithub
protected $users;
/**
+ * @var JGithubMeta GitHub API object for meta.
+ * @since 13.1
+ */
+ protected $meta;
+
+ /**
* Constructor.
*
* @param JRegistry $options GitHub options object.
@@ -234,6 +241,15 @@ public function __get($name)
}
return $this->users;
}
+
+ if ($name == 'meta')
+ {
+ if ($this->meta == null)
+ {
+ $this->meta = new JGithubMeta($this->options, $this->client);
+ }
+ return $this->meta;
+ }
}
/**
59 libraries/joomla/github/meta.php
View
@@ -0,0 +1,59 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage GitHub
+ *
+ * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * GitHub API Meta class.
+ *
+ * @package Joomla.Platform
+ * @subpackage GitHub
+ * @since 13.1
+ */
+class JGithubMeta extends JGithubObject
+{
+ /**
+ * Method to get the authorized IP addresses for services
+ *
+ * @return array Authorized IP addresses
+ *
+ * @since 13.1
+ * @throws DomainException
+ */
+ 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;
+ }
+}
123 tests/suites/unit/joomla/github/JGithubMetaTest.php
View
@@ -0,0 +1,123 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Github
+ *
+ * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+/**
+ * Test class for JGithubMeta.
+ *
+ * @package Joomla.UnitTest
+ * @subpackage Github
+ * @since 13.1
+ */
+class JGithubMetaTest extends TestCase
+{
+ /**
+ * @var JRegistry Options for the GitHub object.
+ * @since 13.1
+ */
+ protected $options;
+
+ /**
+ * @var JGithubHttp Mock client object.
+ * @since 13.1
+ */
+ protected $client;
+
+ /**
+ * @var JHttpResponse Mock response object.
+ * @since 13.1
+ */
+ protected $response;
+
+ /**
+ * @var JGithubMeta Object under test.
+ * @since 13.1
+ */
+ protected $object;
+
+ /**
+ * @var string Sample JSON string.
+ * @since 13.1
+ */
+ 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 13.1
+ */
+ 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 13.1
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->options = new JRegistry;
+ $this->client = $this->getMock('JGithubHttp', array('get', 'post', 'delete', 'patch', 'put'));
+ $this->response = $this->getMock('JHttpResponse');
+
+ $this->object = new JGithubMeta($this->options, $this->client);
+ }
+
+ /**
+ * Tests the getMeta method
+ *
+ * @return void
+ *
+ * @since 13.1
+ */
+ 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 13.1
+ *
+ * @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();
+ }
+}
24 tests/suites/unit/joomla/github/JGithubTest.php
View
@@ -1,23 +1,20 @@
<?php
/**
* @package Joomla.UnitTest
- * @subpackage Client
+ * @subpackage Github
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
-require_once JPATH_PLATFORM . '/joomla/github/github.php';
-
/**
* Test class for JGithub.
*
* @package Joomla.UnitTest
* @subpackage Github
- *
* @since 11.1
*/
-class JGithubTest extends PHPUnit_Framework_TestCase
+class JGithubTest extends TestCase
{
/**
* @var JRegistry Options for the GitHub object.
@@ -32,7 +29,7 @@ class JGithubTest extends PHPUnit_Framework_TestCase
protected $client;
/**
- * @var JGithubIssues Object under test.
+ * @var JGithub Object under test.
* @since 11.4
*/
protected $object;
@@ -218,6 +215,21 @@ public function test__GetHooks()
}
/**
+ * Tests the magic __get method - meta
+ *
+ * @since 13.1
+ *
+ * @return void
+ */
+ public function test__GetMeta()
+ {
+ $this->assertThat(
+ $this->object->meta,
+ $this->isInstanceOf('JGithubMeta')
+ );
+ }
+
+ /**
* Tests the magic __get method - failure
*
* @since 11.3
Please sign in to comment.
Something went wrong with that request. Please try again.