Permalink
Browse files

Switch from using JHttpTransport to JOauthHttp (extended from JHttp) …

…to make the patch method available.
  • Loading branch information...
1 parent 6a77274 commit 48ee09560e0392e13024e6da5deaedf1fd3fe555 @aaronschmitz aaronschmitz committed with LouisLandry Aug 4, 2012
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage Oauth
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * HTTP client class for connecting to an Oauth instance.
+ *
+ * @package Joomla.Platform
+ * @subpackage Oauth
+ * @since 12.2
+ */
+class JOauthHttp extends JHttp
+{
+ /**
+ * Method to send the PATCH command to the server.
+ *
+ * @param string $url Path to the resource.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
+ * @param array $headers An array of name-value pairs to include in the header of the request.
+ *
+ * @return JHttpResponse
+ *
+ * @since 12.2
+ */
+ public function patch($url, $data, array $headers = null)
+ {
+ return $this->transport->request('PATCH', new JUri($url), $data, $headers);
+ }
+}
@@ -114,7 +114,7 @@ public function isAuth()
if (!$token || !array_key_exists('access_token', $token))
{
return false;
- }
+ }
elseif (array_key_exists('expires_in', $token) && $token['created'] + $token['expires_in'] < time() + 20)
{
return false;
@@ -0,0 +1,86 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage Client
+ *
+ * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+require_once JPATH_PLATFORM . '/joomla/oauth/http.php';
+require_once JPATH_PLATFORM . '/joomla/http/transport/stream.php';
+
+/**
+ * Test class for JOauth.
+ *
+ * @package Joomla.UnitTest
+ * @subpackage Oauth
+ * @since 12.2
+ */
+class JOauthHttpTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @var JRegistry Options for the Oauth object.
+ * @since 12.2
+ */
+ protected $options;
+
+ /**
+ * @var JHttpTransportStream Mock client object.
+ * @since 12.2
+ */
+ protected $transport;
+
+ /**
+ * @var JOauthHttp Object under test.
+ * @since 12.2
+ */
+ protected $object;
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ * @return void
+ */
+ protected function setUp()
+ {
+ $this->options = new JRegistry;
+ $this->transport = $this->getMock('JHttpTransportStream', array('request'), array($this->options), 'CustomTransport', false);
+
+ $this->object = new JOauthHttp($this->options, $this->transport);
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ * @return void
+ */
+ protected function tearDown()
+ {
+ }
+
+ /**
+ * Tests the patch method
+ *
+ * @group JOauth
+ * @return void
+ */
+ public function testPatch()
+ {
+ $uri = new JUri('https://example.com/gettest');
+
+ $this->transport->expects($this->once())
+ ->method('request')
+ ->with('PATCH', $uri, array())
+ ->will($this->returnValue('requestResponse'));
+
+ $this->assertThat(
+ $this->object->patch('https://example.com/gettest', array()),
+ $this->equalTo('requestResponse')
+ );
+ }
+}
@@ -7,19 +7,19 @@
* @license GNU General Public License version 2 or later; see LICENSE
*/
-require_once JPATH_PLATFORM . '/joomla/oauth/v2client.php';
+require_once JPATH_PLATFORM . '/joomla/oauth/2client.php';
/**
- * Test class for JOauthV2client.
+ * Test class for JOauth2client.
*
* @package Joomla.UnitTest
* @subpackage Oauth
* @since 12.2
*/
-class JOauthV2clientTest extends PHPUnit_Framework_TestCase
+class JOauth2clientTest extends PHPUnit_Framework_TestCase
{
/**
- * @var JRegistry Options for the JOauthV2client object.
+ * @var JRegistry Options for the JOauth2client object.
*/
protected $options;
@@ -34,7 +34,7 @@ class JOauthV2clientTest extends PHPUnit_Framework_TestCase
protected $input;
/**
- * @var JOauthV2client Object under test.
+ * @var JOauth2client Object under test.
*/
protected $object;
@@ -50,7 +50,7 @@ protected function setUp()
$this->options = new JRegistry;
$this->http = $this->getMock('JOauthHttp', array('head', 'get', 'delete', 'trace', 'post', 'put', 'patch'), array($this->options));
$this->input = new JInput;
- $this->object = new JOauthV2client($this->options, $this->http, $this->input);
+ $this->object = new JOauth2client($this->options, $this->http, $this->input);
}
/**
@@ -95,7 +95,7 @@ public function testAuth()
$this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
$this->input->set('code', '4/wEr_dK8SDkjfpwmc98KejfiwJP-f4wm.kdowmnr82jvmeisjw94mKFIJE48mcEM');
- $this->http->expects($this->once())->method('post')->will($this->returnCallback('postOauthCallback'));
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('postCallback'));
$result = $this->object->auth();
$this->assertEquals('accessvalue', $result['access_token']);
$this->assertEquals('refreshvalue', $result['refresh_token']);
@@ -104,31 +104,6 @@ public function testAuth()
}
/**
- * Tests the isauth method
- *
- * @group JOauth
- * @return void
- */
- public function testIsAuth()
- {
- $this->assertEquals(false, $this->object->isAuth());
-
- $token['access_token'] = 'accessvalue';
- $token['refresh_token'] = 'refreshvalue';
- $token['created'] = time();
- $token['expires_in'] = 3600;
- $this->object->setToken($token);
-
- $this->assertEquals(true, $this->object->isAuth());
-
- $token['created'] = time() - 4000;
- $token['expires_in'] = 3600;
- $this->object->setToken($token);
-
- $this->assertEquals(false, $this->object->isAuth());
- }
-
- /**
* Tests the auth method
*
* @group JOauth
@@ -166,12 +141,12 @@ public function testQuery()
$token['expires_in'] = 3600;
$this->object->setToken($token);
- $this->http->expects($this->once())->method('get')->will($this->returnCallback('getOauthCallback'));
+ $this->http->expects($this->once())->method('get')->will($this->returnCallback('getCallback'));
$result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'get');
$this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
$this->assertEquals(200, $result->code);
- $this->http->expects($this->once())->method('post')->will($this->returnCallback('postOauthCallback'));
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('postCallback'));
$result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'post');
$this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
$this->assertEquals(200, $result->code);
@@ -270,7 +245,7 @@ public function testRefreshToken()
$this->object->setOption('userefresh', true);
$this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600, 'refresh_token' => ' RANDOM STRING OF DATA'));
- $this->http->expects($this->once())->method('post')->will($this->returnCallback('postOauthCallback'));
+ $this->http->expects($this->once())->method('post')->will($this->returnCallback('postCallback'));
$result = $this->object->refreshToken();
$this->assertEquals('accessvalue', $result['access_token']);
$this->assertEquals('refreshvalue', $result['refresh_token']);
@@ -291,7 +266,7 @@ public function testRefreshToken()
*
* @since 12.2
*/
-function postOauthCallback($url, $data, array $headers = null, $timeout = null)
+function postCallback($url, $data, array $headers = null, $timeout = null)
{
if (isset($data['grant_type']))
{
@@ -319,7 +294,7 @@ function postOauthCallback($url, $data, array $headers = null, $timeout = null)
*
* @since 12.2
*/
-function getOauthCallback($url, array $headers = null, $timeout = null)
+function getCallback($url, array $headers = null, $timeout = null)
{
$response->code = 200;
$response->headers = array('Content-Type' => 'text/html');

0 comments on commit 48ee095

Please sign in to comment.