Skip to content
This repository
Browse code

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
@@ -23,6 +23,7 @@
23 23 * @property-read JGithubAccount $account GitHub API object for account references.
24 24 * @property-read JGithubHooks $hooks GitHub API object for hooks.
25 25 * @property-read JGithubUsers $users GitHub API object for users.
  26 + * @property-read JGithubMeta $meta GitHub API object for meta.
26 27 *
27 28 * @package Joomla.Platform
28 29 * @subpackage GitHub
@@ -109,6 +110,12 @@ class JGithub
109 110 protected $users;
110 111
111 112 /**
  113 + * @var JGithubMeta GitHub API object for meta.
  114 + * @since 13.1
  115 + */
  116 + protected $meta;
  117 +
  118 + /**
112 119 * Constructor.
113 120 *
114 121 * @param JRegistry $options GitHub options object.
@@ -234,6 +241,15 @@ public function __get($name)
234 241 }
235 242 return $this->users;
236 243 }
  244 +
  245 + if ($name == 'meta')
  246 + {
  247 + if ($this->meta == null)
  248 + {
  249 + $this->meta = new JGithubMeta($this->options, $this->client);
  250 + }
  251 + return $this->meta;
  252 + }
237 253 }
238 254
239 255 /**
59 libraries/joomla/github/meta.php
... ... @@ -0,0 +1,59 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.Platform
  4 + * @subpackage GitHub
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE
  8 + */
  9 +
  10 +defined('JPATH_PLATFORM') or die;
  11 +
  12 +/**
  13 + * GitHub API Meta class.
  14 + *
  15 + * @package Joomla.Platform
  16 + * @subpackage GitHub
  17 + * @since 13.1
  18 + */
  19 +class JGithubMeta extends JGithubObject
  20 +{
  21 + /**
  22 + * Method to get the authorized IP addresses for services
  23 + *
  24 + * @return array Authorized IP addresses
  25 + *
  26 + * @since 13.1
  27 + * @throws DomainException
  28 + */
  29 + public function getMeta()
  30 + {
  31 + // Build the request path.
  32 + $path = '/meta';
  33 +
  34 + $githubIps = $this->processResponse($this->client->get($this->fetchUrl($path)), 200);
  35 +
  36 + /*
  37 + * The response body returns the IP addresses in CIDR format
  38 + * Decode the response body and strip the subnet mask information prior to
  39 + * returning the data to the user. We're assuming quite a bit here that all
  40 + * masks will be /32 as they are as of the time of development.
  41 + */
  42 +
  43 + $authorizedIps = array();
  44 +
  45 + foreach ($githubIps as $key => $serviceIps)
  46 + {
  47 + // The first level contains an array of IPs based on the service
  48 + $authorizedIps[$key] = array();
  49 +
  50 + foreach ($serviceIps as $serviceIp)
  51 + {
  52 + // The second level is each individual IP address, strip the mask here
  53 + $authorizedIps[$key][] = substr($serviceIp, 0, -3);
  54 + }
  55 + }
  56 +
  57 + return $authorizedIps;
  58 + }
  59 +}
123 tests/suites/unit/joomla/github/JGithubMetaTest.php
... ... @@ -0,0 +1,123 @@
  1 +<?php
  2 +/**
  3 + * @package Joomla.UnitTest
  4 + * @subpackage Github
  5 + *
  6 + * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7 + * @license GNU General Public License version 2 or later; see LICENSE
  8 + */
  9 +
  10 +/**
  11 + * Test class for JGithubMeta.
  12 + *
  13 + * @package Joomla.UnitTest
  14 + * @subpackage Github
  15 + * @since 13.1
  16 + */
  17 +class JGithubMetaTest extends TestCase
  18 +{
  19 + /**
  20 + * @var JRegistry Options for the GitHub object.
  21 + * @since 13.1
  22 + */
  23 + protected $options;
  24 +
  25 + /**
  26 + * @var JGithubHttp Mock client object.
  27 + * @since 13.1
  28 + */
  29 + protected $client;
  30 +
  31 + /**
  32 + * @var JHttpResponse Mock response object.
  33 + * @since 13.1
  34 + */
  35 + protected $response;
  36 +
  37 + /**
  38 + * @var JGithubMeta Object under test.
  39 + * @since 13.1
  40 + */
  41 + protected $object;
  42 +
  43 + /**
  44 + * @var string Sample JSON string.
  45 + * @since 13.1
  46 + */
  47 + protected $sampleString = '{"hooks":["127.0.0.1/32","192.168.1.1/32"],"git":["127.0.0.1/32"]}';
  48 +
  49 + /**
  50 + * @var string Sample JSON error message.
  51 + * @since 13.1
  52 + */
  53 + protected $errorString = '{"message": "Generic Error"}';
  54 +
  55 + /**
  56 + * Sets up the fixture, for example, opens a network connection.
  57 + * This method is called before a test is executed.
  58 + *
  59 + * @return void
  60 + *
  61 + * @since 13.1
  62 + */
  63 + protected function setUp()
  64 + {
  65 + parent::setUp();
  66 +
  67 + $this->options = new JRegistry;
  68 + $this->client = $this->getMock('JGithubHttp', array('get', 'post', 'delete', 'patch', 'put'));
  69 + $this->response = $this->getMock('JHttpResponse');
  70 +
  71 + $this->object = new JGithubMeta($this->options, $this->client);
  72 + }
  73 +
  74 + /**
  75 + * Tests the getMeta method
  76 + *
  77 + * @return void
  78 + *
  79 + * @since 13.1
  80 + */
  81 + public function testGetMeta()
  82 + {
  83 + $this->response->code = 200;
  84 + $this->response->body = $this->sampleString;
  85 +
  86 + $decodedResponse = array(
  87 + 'hooks' => array('127.0.0.1', '192.168.1.1'),
  88 + 'git' => array('127.0.0.1')
  89 + );
  90 +
  91 + $this->client->expects($this->once())
  92 + ->method('get')
  93 + ->with('/meta')
  94 + ->will($this->returnValue($this->response));
  95 +
  96 + $this->assertThat(
  97 + $this->object->getMeta(),
  98 + $this->equalTo($decodedResponse)
  99 + );
  100 + }
  101 +
  102 + /**
  103 + * Tests the getMeta method - failure
  104 + *
  105 + * @return void
  106 + *
  107 + * @since 13.1
  108 + *
  109 + * @expectedException DomainException
  110 + */
  111 + public function testGetMetaFailure()
  112 + {
  113 + $this->response->code = 500;
  114 + $this->response->body = $this->errorString;
  115 +
  116 + $this->client->expects($this->once())
  117 + ->method('get')
  118 + ->with('/meta')
  119 + ->will($this->returnValue($this->response));
  120 +
  121 + $this->object->getMeta();
  122 + }
  123 +}
24 tests/suites/unit/joomla/github/JGithubTest.php
... ... @@ -1,23 +1,20 @@
1 1 <?php
2 2 /**
3 3 * @package Joomla.UnitTest
4   - * @subpackage Client
  4 + * @subpackage Github
5 5 *
6 6 * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7 7 * @license GNU General Public License version 2 or later; see LICENSE
8 8 */
9 9
10   -require_once JPATH_PLATFORM . '/joomla/github/github.php';
11   -
12 10 /**
13 11 * Test class for JGithub.
14 12 *
15 13 * @package Joomla.UnitTest
16 14 * @subpackage Github
17   - *
18 15 * @since 11.1
19 16 */
20   -class JGithubTest extends PHPUnit_Framework_TestCase
  17 +class JGithubTest extends TestCase
21 18 {
22 19 /**
23 20 * @var JRegistry Options for the GitHub object.
@@ -32,7 +29,7 @@ class JGithubTest extends PHPUnit_Framework_TestCase
32 29 protected $client;
33 30
34 31 /**
35   - * @var JGithubIssues Object under test.
  32 + * @var JGithub Object under test.
36 33 * @since 11.4
37 34 */
38 35 protected $object;
@@ -218,6 +215,21 @@ public function test__GetHooks()
218 215 }
219 216
220 217 /**
  218 + * Tests the magic __get method - meta
  219 + *
  220 + * @since 13.1
  221 + *
  222 + * @return void
  223 + */
  224 + public function test__GetMeta()
  225 + {
  226 + $this->assertThat(
  227 + $this->object->meta,
  228 + $this->isInstanceOf('JGithubMeta')
  229 + );
  230 + }
  231 +
  232 + /**
221 233 * Tests the magic __get method - failure
222 234 *
223 235 * @since 11.3

0 comments on commit 20ec48e

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