Skip to content

Commit 4228ae5

Browse files
committed
Changed to non-B/C approach
1 parent 697ca84 commit 4228ae5

4 files changed

Lines changed: 68 additions & 56 deletions

File tree

Tests/GithubObjectTest.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
namespace Joomla\Github\Tests;
88

9+
use Joomla\Github\AbstractGithubObject;
910
use Joomla\Github\Tests\Stub\GitHubTestCase;
1011
use Joomla\Github\Tests\Stub\ObjectMock;
12+
use Joomla\Github\Tests\Stub\TransportMock;
13+
use Joomla\Http\Http;
1114

1215
/**
1316
* Test class for Joomla\Github\Object.
@@ -17,11 +20,17 @@
1720
class GithubObjectTest extends GitHubTestCase
1821
{
1922
/**
20-
* @var ObjectMock Object under test.
23+
* @var AbstractGithubObject Object under test.
2124
* @since 1.0
2225
*/
2326
protected $object;
2427

28+
/**
29+
* @var Http The HTTP client
30+
* @since __DEPLOY_VERSION__
31+
*/
32+
protected $client;
33+
2534
/**
2635
* Sets up the fixture, for example, opens a network connection.
2736
* This method is called before a test is executed.
@@ -34,6 +43,7 @@ protected function setUp()
3443
{
3544
parent::setUp();
3645

46+
$this->client = new Http(array(), new TransportMock());
3747
$this->object = new ObjectMock($this->options, $this->client);
3848
}
3949

@@ -74,12 +84,8 @@ public function testFetchUrl($apiUrl, $path, $page, $limit, $expected)
7484

7585
self::assertEquals(
7686
$expected,
77-
$this->object->fetchUrl($path, $page, $limit)
78-
);
79-
80-
self::assertEquals(
81-
array(),
82-
$this->object->authHeader()
87+
$this->object->fetchUrl($path, $page, $limit),
88+
'URL is not as expected.'
8389
);
8490
}
8591

@@ -97,9 +103,10 @@ public function testFetchUrlBasicAuth()
97103
$this->options->set('api.username', 'MyTestUser');
98104
$this->options->set('api.password', 'MyTestPass');
99105

100-
$this->assertThat(
106+
self::assertEquals(
107+
'https://MyTestUser:MyTestPass@api.github.com/gists',
101108
$this->object->fetchUrl('/gists', 0, 0),
102-
$this->equalTo('https://MyTestUser:MyTestPass@api.github.com/gists')
109+
'URL is not as expected.'
103110
);
104111
}
105112

@@ -114,14 +121,18 @@ public function testFetchUrlToken()
114121

115122
$this->options->set('gh.token', 'MyTestToken');
116123

124+
$options = clone $this->options;
125+
117126
self::assertEquals(
118127
'https://api.github.com/gists',
119-
$this->object->fetchUrl('/gists', 0, 0)
128+
$this->object->fetchUrl('/gists', 0, 0),
129+
'URL is not as expected.'
120130
);
121-
131+
122132
self::assertEquals(
123-
array('Authorization: MyTestToken'),
124-
$this->object->authHeader()
133+
array('Authorization' => 'token MyTestToken'),
134+
$this->client->getOption('headers'),
135+
'Token should bhe propagated as a header.'
125136
);
126137
}
127138
}

Tests/Stub/ObjectMock.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,4 @@ public function fetchUrl($path, $page = 0, $limit = 0)
3232
{
3333
return parent::fetchUrl($path, $page, $limit);
3434
}
35-
36-
/**
37-
* Returns the Authorization header, if required.
38-
*
39-
* If the options passed to the constructor contain a value for `gh.token`,
40-
* an array with a suitable Authorization header is returned, an empty array
41-
* otherwise.
42-
*
43-
* @return array Authorization header if set in options.
44-
*
45-
* @since 1.8.0
46-
*/
47-
public function authHeader()
48-
{
49-
return parent::authHeader();
50-
}
51-
5235
}

Tests/Stub/TransportMock.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Joomla\Github\Tests\Stub;
4+
5+
class TransportMock implements \Joomla\Http\TransportInterface
6+
{
7+
public function request($method, \Joomla\Uri\UriInterface $uri, $data = null, array $headers = array(), $timeout = null, $userAgent = null)
8+
{
9+
// TODO: Implement request() method.
10+
}
11+
12+
public static function isSupported()
13+
{
14+
// TODO: Implement isSupported() method.
15+
}
16+
}

src/AbstractGithubObject.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Joomla\Github;
1010

1111
use Joomla\Http\Exception\UnexpectedResponseException;
12-
use Joomla\Http\Http as BaseHttp;
12+
use Joomla\Http\Http;
1313
use Joomla\Http\Response;
1414
use Joomla\Registry\Registry;
1515
use Joomla\Uri\Uri;
@@ -82,15 +82,27 @@ abstract class AbstractGithubObject
8282
* Constructor.
8383
*
8484
* @param Registry $options GitHub options object.
85-
* @param BaseHttp $client The HTTP client object.
85+
* @param Http $client The HTTP client object.
8686
*
8787
* @since 1.0
8888
*/
89-
public function __construct(Registry $options = null, BaseHttp $client = null)
89+
public function __construct(Registry $options = null, Http $client = null)
9090
{
9191
$this->options = $options ?: new Registry;
9292
$this->client = $client ?: new Http($this->options);
9393

94+
// Make sure the user agent string is defined.
95+
if (!isset($this->options['userAgent']))
96+
{
97+
$this->options['userAgent'] = 'JGitHub/2.0';
98+
}
99+
100+
// Set the default timeout to 120 seconds.
101+
if (!isset($this->options['timeout']))
102+
{
103+
$this->options['timeout'] = 120;
104+
}
105+
94106
$this->package = \get_class($this);
95107
$this->package = substr($this->package, strrpos($this->package, '\\') + 1);
96108
}
@@ -114,7 +126,18 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
114126
// Get a new Uri object focusing the api url and given path.
115127
$uri = new Uri($this->options->get('api.url') . $path);
116128

117-
if (!$this->options->get('gh.token', false))
129+
if ($this->options->get('gh.token', false))
130+
{
131+
// Use oAuth authentication
132+
$headers = $this->client->getOption('headers', array());
133+
134+
if (!isset($headers['Authorization']))
135+
{
136+
$headers['Authorization'] = 'token ' . $this->options->get('gh.token');
137+
$this->client->setOption('headers', $headers);
138+
}
139+
}
140+
else
118141
{
119142
// Use basic authentication
120143
if ($this->options->get('api.username', false))
@@ -143,27 +166,6 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
143166
return (string) $uri;
144167
}
145168

146-
/**
147-
* Returns the Authorization header, if required.
148-
*
149-
* If the options passed to the constructor contain a value for `gh.token`,
150-
* an array with a suitable Authorization header is returned, an empty array
151-
* otherwise.
152-
*
153-
* @return array Authorization header if set in options.
154-
*
155-
* @since 1.8.0
156-
*/
157-
protected function authHeader()
158-
{
159-
if ($this->options->get('gh.token', false))
160-
{
161-
return array('Authorization: ' . $this->options->get('gh.token'));
162-
}
163-
164-
return array();
165-
}
166-
167169
/**
168170
* Process the response and decode it.
169171
*
@@ -172,8 +174,8 @@ protected function authHeader()
172174
*
173175
* @return mixed
174176
*
175-
* @throws UnexpectedResponseException
176177
* @since 1.0
178+
* @throws UnexpectedResponseException
177179
*/
178180
protected function processResponse(Response $response, $expectedCode = 200)
179181
{

0 commit comments

Comments
 (0)