ClientTest::testGenerateRequestTokenFailure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
4
 * @license    GNU General Public License version 2 or later; see LICENSE
5
 */
6
7
namespace Joomla\OAuth1\Tests;
8
9
use Joomla\Application\AbstractWebApplication;
10
use Joomla\Input\Input;
11
use Joomla\OAuth1\Client;
12
use Joomla\Registry\Registry;
13
use Joomla\Test\TestHelper;
14
use PHPUnit\Framework\TestCase;
15
16
require_once __DIR__ . '/stubs/ClientInspector.php';
17
18
/**
19
 * Test class for OAuth1 Client.
20
 *
21
 * @since  1.0
22
 */
23
class ClientTest extends TestCase
24
{
25
	/**
26
	 * @var    Input  input for the Client object.
27
	 * @since  1.0
28
	 */
29
	protected $input;
30
31
	/**
32
	 * @var    Registry  Options for the Client object.
33
	 * @since  1.0
34
	 */
35
	protected $options;
36
37
	/**
38
	 * @var    object  Mock http object.
39
	 * @since  1.0
40
	 */
41
	protected $client;
42
43
	/**
44
	 * An instance of the object to test.
45
	 *
46
	 * @var    ClientInspector
47
	 * @since  1.0
48
	 */
49
	protected $object;
50
51
	/**
52
	 * @var   AbstractWebApplication|\PHPUnit_Framework_MockObject_MockObject  The application object to send HTTP headers for redirects.
53
	 */
54
	protected $application;
55
56
	/**
57
	 * @var    string  Sample JSON string.
58
	 * @since  1.0
59
	 */
60
	protected $sampleString = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
61
62
	/**
63
	 * @var    string  Sample JSON error message.
64
	 * @since  1.0
65
	 */
66
	protected $errorString = '{"errorCode":401, "message": "Generic error"}';
67
68
	/**
69
	 * Sets up the fixture, for example, opens a network connection.
70
	 * This method is called before a test is executed.
71
	 *
72
	 * @return void
73
	 */
74
	protected function setUp()
75
	{
76
		$_SERVER['HTTP_HOST'] = 'example.com';
77
		$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0';
78
		$_SERVER['REQUEST_URI'] = '/index.php';
79
		$_SERVER['SCRIPT_NAME'] = '/index.php';
80
81
		$key = "TEST_KEY";
82
		$secret = "TEST_SECRET";
83
		$my_url = "TEST_URL";
0 ignored issues
show
Unused Code introduced by
$my_url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
85
		$this->options = new Registry;
86
		$this->client = $this->getMockBuilder('Joomla\\Http\\Http')->getMock();
87
		$this->input = new Input;
88
		$this->application = $this->getMockForAbstractClass('Joomla\\Application\\AbstractWebApplication');
89
90
		$mockSession = $this->getMockBuilder('Joomla\\Session\\Session')
91
			->disableOriginalConstructor()
92
			->getMock();
93
94
		$this->application->setSession($mockSession);
95
96
		$this->options->set('consumer_key', $key);
97
		$this->options->set('consumer_secret', $secret);
98
		$this->object = new ClientInspector($this->options, $this->client, $this->input, $this->application);
0 ignored issues
show
Documentation introduced by
$this->options is of type object<Joomla\Registry\Registry>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Joomla\Http\Http>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->application is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Joomla\Application\AbstractWebApplication>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
	}
100
101
	/**
102
	* Provides test data.
103
	*
104
	* @return array
105
	*
106
	* @since 1.0
107
	*/
108
	public function seedAuthenticate()
109
	{
110
		// Token, fail and oauth version.
111
		return array(
112
			array(array('key' => 'valid', 'secret' => 'valid'), false, '1.0'),
113
			array(null, false, '1.0'),
114
			array(null, false, '1.0a'),
115
			array(null, true, '1.0a')
116
			);
117
	}
118
119
	/**
120
	 * Tests the authenticate method
121
	 *
122
	 * @param   array    $token    The passed token.
123
	 * @param   boolean  $fail     Mark if should fail or not.
124
	 * @param   string   $version  Specify oauth version 1.0 or 1.0a.
125
	 *
126
	 * @return  void
127
	 *
128
	 * @dataProvider seedAuthenticate
129
	 * @since   1.0
130
	 */
131
	public function testAuthenticate($token, $fail, $version)
132
	{
133
		// Already got some credentials stored?
134
		if (!\is_null($token))
135
		{
136
			$this->object->setToken($token);
137
			$result = $this->object->authenticate();
138
			$this->assertEquals($result, $token);
139
		}
140
		else
141
		{
142
			$this->object->setOption('requestTokenURL', 'https://example.com/request_token');
143
			$this->object->setOption('authoriseURL', 'https://example.com/authorize');
144
			$this->object->setOption('accessTokenURL', 'https://example.com/access_token');
145
146
			// Request token.
147
			$returnData = new \stdClass;
148
			$returnData->code = 200;
149
			$returnData->body = 'oauth_token=token&oauth_token_secret=secret&oauth_callback_confirmed=true';
150
151
			$this->client->expects($this->at(0))
152
				->method('post')
153
				->with($this->object->getOption('requestTokenURL'))
154
				->will($this->returnValue($returnData));
155
156
			$input = TestHelper::getValue($this->object, 'input');
157
			$input->set('oauth_verifier', null);
158
			TestHelper::setValue($this->object, 'input', $input);
159
160
			if (strcmp($version, '1.0a') === 0)
161
			{
162
				$this->object->setOption('callback', 'TEST_URL');
163
			}
164
165
			$this->object->authenticate();
166
167
			$token = $this->object->getToken();
168
			$this->assertEquals($token['key'], 'token');
169
			$this->assertEquals($token['secret'], 'secret');
170
171
			// Access token.
172
			$input = TestHelper::getValue($this->object, 'input');
173
174
			if (strcmp($version, '1.0a') === 0)
175
			{
176
				TestHelper::setValue($this->object, 'version', $version);
177
				$data = array('oauth_verifier' => 'verifier', 'oauth_token' => 'token');
178
			}
179
			else
180
			{
181
				TestHelper::setValue($this->object, 'version', $version);
182
				$data = array('oauth_token' => 'token');
183
			}
184
185
			TestHelper::setValue($input, 'data', $data);
186
187
			// Get mock session
188
			$mockSession = $this->getMockBuilder('Joomla\\Session\\Session')
189
				->disableOriginalConstructor()
190
				->getMock();
191
192
			if ($fail)
193
			{
194
				$mockSession->expects($this->at(0))
195
							->method('get')
196
							->with('oauth_token.key', null)
197
							->will($this->returnValue('bad'));
198
199
				$mockSession->expects($this->at(1))
200
							->method('get')
201
							->with('oauth_token.secret', null)
202
							->will($this->returnValue('session'));
203
204
				$this->application->setSession($mockSession);
0 ignored issues
show
Documentation introduced by
$mockSession is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Joomla\Session\Session>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method setSession does only exist in Joomla\Application\AbstractWebApplication, but not in PHPUnit_Framework_MockObject_MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
205
206
				// expectException was added in PHPUnit 5.2 and setExpectedException removed in 6.0
207
				if (method_exists($this, 'expectException'))
208
				{
209
					$this->expectException('DomainException');
210
				}
211
				else
212
				{
213
					$this->setExpectedException('DomainException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not exist on Joomla\OAuth1\Tests\ClientTest. Did you maybe mean setExpectedExceptionFromAnnotation()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
214
				}
215
216
				$result = $this->object->authenticate();
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
217
			}
218
219
			$mockSession->expects($this->at(0))
220
						->method('get')
221
						->with('oauth_token.key', null)
222
						->will($this->returnValue('token'));
223
224
			$mockSession->expects($this->at(1))
225
						->method('get')
226
						->with('oauth_token.secret', null)
227
						->will($this->returnValue('secret'));
228
229
			$this->application->setSession($mockSession);
0 ignored issues
show
Documentation introduced by
$mockSession is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Joomla\Session\Session>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
230
231
			$returnData = new \stdClass;
232
			$returnData->code = 200;
233
			$returnData->body = 'oauth_token=token_key&oauth_token_secret=token_secret';
234
235
			$this->client->expects($this->at(0))
236
				->method('post')
237
				->with($this->object->getOption('accessTokenURL'))
238
				->will($this->returnValue($returnData));
239
240
			$result = $this->object->authenticate();
241
242
			$this->assertEquals($result['key'], 'token_key');
243
			$this->assertEquals($result['secret'], 'token_secret');
244
		}
245
	}
246
247
	/**
248
	 * Tests the generateRequestToken method - failure
249
	 *
250
	 * @return  void
251
	 *
252
	 * @since   1.0
253
	 * @expectedException  \DomainException
254
	 */
255
	public function testGenerateRequestTokenFailure()
256
	{
257
		$this->object->setOption('requestTokenURL', 'https://example.com/request_token');
258
259
		$returnData = new \stdClass;
260
		$returnData->code = 200;
261
		$returnData->body = 'oauth_token=token&oauth_token_secret=secret&oauth_callback_confirmed=false';
262
263
		$this->client->expects($this->at(0))
264
			->method('post')
265
			->with($this->object->getOption('requestTokenURL'))
266
			->will($this->returnValue($returnData));
267
268
		TestHelper::invoke($this->object, 'generateRequestToken');
269
	}
270
271
	/**
272
	* Provides test data.
273
	*
274
	* @return array
275
	*
276
	* @since  1.0
277
	*/
278
	public function seedOauthRequest()
279
	{
280
		// Method
281
		return array(
282
			array('GET'),
283
			array('PUT'),
284
			array('DELETE')
285
			);
286
	}
287
288
	/**
289
	 * Tests the oauthRequest method
290
	 *
291
	 * @param   string  $method  The request method.
292
	 *
293
	 * @dataProvider seedOauthRequest
294
	 * @return  void
295
	 *
296
	 * @since   1.0
297
	 */
298
	public function testOauthRequest($method)
299
	{
300
		$returnData = new \stdClass;
301
		$returnData->code = 200;
302
		$returnData->body = $this->sampleString;
303
304
		if (strcmp($method, 'PUT') === 0)
305
		{
306
			$data = array('key1' => 'value1', 'key2' => 'value2');
307
			$this->client->expects($this->at(0))
308
				->method($method, $data)
309
				->with('www.example.com')
310
				->will($this->returnValue($returnData));
311
312
			$this->assertThat(
313
				$this->object->oauthRequest('www.example.com', $method, array('oauth_token' => '1235'), $data, array('Content-Type' => 'multipart/form-data')),
314
				$this->equalTo($returnData)
315
				);
316
		}
317
		else
318
		{
319
			$this->client->expects($this->at(0))
320
				->method($method)
321
				->with('www.example.com')
322
				->will($this->returnValue($returnData));
323
324
			$this->assertThat(
325
				$this->object->oauthRequest('www.example.com', $method, array('oauth_token' => '1235'), array(), array('Content-Type' => 'multipart/form-data')),
326
				$this->equalTo($returnData)
327
				);
328
		}
329
	}
330
331
	/**
332
	 * Tests the safeEncode
333
	 *
334
	 * @return  void
335
	 *
336
	 * @since   1.0
337
	 */
338
	public function testSafeEncodeEmpty()
339
	{
340
		$this->assertThat(
341
			$this->object->safeEncode(null),
342
			$this->equalTo('')
343
			);
344
	}
345
}
346