Skip to content

Commit

Permalink
Add PHPUnit support (beaulebens#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
pento authored and Beau Lebens committed Feb 28, 2021
1 parent 335ffd1 commit 1dc72ba
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Travis CI Configuration File

# Tell Travis CI we're using PHP
language: php

# whitelist branches for the "push" build check
branches:
only:
- master

# Run Matrix for these PHP versions
php:
- "5.6"
- "7.0"
- "7.2"
- "7.3"
- "7.4"

before_script:
- composer --version
- composer install

script: composer test-includes
6 changes: 6 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"core": "WordPress/WordPress",
"plugins": [ "." ],
"port": "5555",
"testsPort": "5556"
}
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
"description": "An authentication framework for WordPress development. Provides a standardized connection and communication framework for talking to web services.",
"require": {
"php": ">=5.3"
},
"require-dev": {
"phpunit/phpunit": "^5|^6|^7"
},
"scripts": {
"test-includes": "phpunit includes/oauth-php"
}
}
2 changes: 1 addition & 1 deletion includes/oauth-php/OAuthTests.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit colors="true">
<phpunit colors="true" backupGlobals="true">
<testsuite name="OAuth">
<directory>tests</directory>
</testsuite>
Expand Down
2 changes: 1 addition & 1 deletion includes/oauth-php/tests/OAuthConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'common.php';

class OAuthConsumerTest extends PHPUnit_Framework_TestCase {
class OAuthConsumerTest extends PHPUnit\Framework\TestCase {
public function testConvertToString() {
$consumer = new OAuthConsumer('key', 'secret');
$this->assertEquals('OAuthConsumer[key=key,secret=secret]', (string) $consumer);
Expand Down
4 changes: 2 additions & 2 deletions includes/oauth-php/tests/OAuthRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

require_once dirname(__FILE__) . '/common.php';

class OAuthRequestTest extends PHPUnit_Framework_TestCase {
class OAuthRequestTest extends PHPUnit\Framework\TestCase {
public function testCanGetSingleParameter() {
// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
$request = new OAuthRequest('', '', array('test'=>'foo'));
Expand Down Expand Up @@ -239,7 +239,7 @@ public function testBuildHeader() {
}

public function testWontBuildHeaderWithArrayInput() {
$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
OAuthTestUtils::build_request('POST', 'http://example.com', 'oauth_foo=bar&oauth_foo=baz');
OAuthRequest::from_request()->to_header();
}
Expand Down
39 changes: 20 additions & 19 deletions includes/oauth-php/tests/OAuthServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Tests of OAuthUtil
*/
class OAuthServerTest extends PHPUnit_Framework_TestCase {
class OAuthServerTest extends PHPUnit\Framework\TestCase {
private $consumer;
private $request_token;
private $access_token;
Expand Down Expand Up @@ -45,14 +45,14 @@ public function testAcceptRequestWithoutVersion() {
$request->unset_parameter('oauth_version');
$request->sign_request( $this->hmac_sha1, $this->consumer, $this->access_token );

$this->server->verify_request( $request );
$this->assertInternalType( 'array', $this->server->verify_request( $request ) );
}

public function testRejectRequestSignedWithRequestToken() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->request_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->request_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request( $request );
}

Expand All @@ -72,11 +72,12 @@ public function testRejectRequestWithMissingParameters() {
foreach( $required_parameters AS $required ) {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
try {
$request->unset_parameter( $required );
$this->server->verify_request($request);
$this->fail('Allowed a request without `' . $required . '`');
} catch( OAuthException $e ) { /* expected */ }

$this->expectException('OAuthException');

$request->unset_parameter( $required );
$this->server->verify_request($request);
$this->fail('Allowed a request without `' . $required . '`');
}
}

Expand All @@ -87,7 +88,7 @@ public function testRejectPastTimestamp() {
$request->set_parameter( 'oauth_timestamp', $request->get_parameter('oauth_timestamp') - 10*60*60, false);
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request($request);
}

Expand All @@ -98,7 +99,7 @@ public function testRejectFutureTimestamp() {
$request->set_parameter( 'oauth_timestamp', $request->get_parameter('oauth_timestamp') + 10*60*60, false);
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request($request);
}

Expand All @@ -110,7 +111,7 @@ public function testRejectUsedNonce() {
$request->set_parameter( 'oauth_nonce', 'nonce', false);
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request($request);
}

Expand All @@ -121,7 +122,7 @@ public function testRejectInvalidSignature() {
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$request->set_parameter( 'oauth_signature', '__whatever__', false);

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request($request);
}

Expand All @@ -133,7 +134,7 @@ public function testRejectInvalidConsumer() {
$request = OAuthRequest::from_consumer_and_token( $unknown_consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $unknown_consumer, $this->access_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request( $request );
}

Expand All @@ -145,7 +146,7 @@ public function testRejectInvalidToken() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $unknown_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $unknown_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request( $request );
}

Expand All @@ -158,7 +159,7 @@ public function testRejectUnknownSignatureMethod() {
$server = new OAuthServer( new Mock_OAuthDataStore() );
$server->add_signature_method( $this->hmac_sha1 );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$server->verify_request( $request );
}

Expand All @@ -169,7 +170,7 @@ public function testRejectUnknownVersion() {
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$request->set_parameter('oauth_version', '1.0a', false);

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$this->server->verify_request( $request );
}

Expand All @@ -189,7 +190,7 @@ public function testRejectSignedRequestTokenRequest() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->request_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->request_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$token = $this->server->fetch_request_token($request);
}

Expand All @@ -209,7 +210,7 @@ public function testRejectUnsignedAccessTokenRequest() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, NULL, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, NULL );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$token = $this->server->fetch_access_token($request);
}

Expand All @@ -219,7 +220,7 @@ public function testRejectAccessTokenSignedAccessTokenRequest() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );

$this->setExpectedException('OAuthException');
$this->expectException('OAuthException');
$token = $this->server->fetch_access_token($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once 'common.php';
require_once 'Mock_OAuthBaseStringRequest.php';

class OAuthSignatureMethodHmacSha1Test extends PHPUnit_Framework_TestCase {
class OAuthSignatureMethodHmacSha1Test extends PHPUnit\Framework\TestCase {
private $method;

public function setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_once 'common.php';
require_once 'Mock_OAuthBaseStringRequest.php';

class OAuthSignatureMethodPlaintextTest extends PHPUnit_Framework_TestCase {
class OAuthSignatureMethodPlaintextTest extends PHPUnit\Framework\TestCase {
private $method;

public function setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_once 'Mock_OAuthBaseStringRequest.php';
require_once 'Mock_OAuthSignatureMethod_RSA_SHA1.php';

class OAuthSignatureMethodRsaSha1Test extends PHPUnit_Framework_TestCase {
class OAuthSignatureMethodRsaSha1Test extends PHPUnit\Framework\TestCase {
private $method;

public function setUp() {
Expand Down
2 changes: 1 addition & 1 deletion includes/oauth-php/tests/OAuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once 'common.php';

class OAuthTokenTest extends PHPUnit_Framework_TestCase {
class OAuthTokenTest extends PHPUnit\Framework\TestCase {
public function testSerialize() {
$token = new OAuthToken('token', 'secret');
$this->assertEquals('oauth_token=token&oauth_token_secret=secret', $token->to_string());
Expand Down
2 changes: 1 addition & 1 deletion includes/oauth-php/tests/OAuthUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Tests of OAuthUtil
*/
class OAuthUtilTest extends PHPUnit_Framework_TestCase {
class OAuthUtilTest extends PHPUnit\Framework\TestCase {
public function testUrlencode() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Parameter Encoding")
Expand Down
15 changes: 13 additions & 2 deletions includes/oauth-php/tests/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@
*/
class OAuthTestUtils {
private static function reset_request_vars() {
$_SERVER = array();
unset(
$_SERVER['HTTPS'],
$_SERVER['REQUEST_METHOD'],
$_SERVER['HTTP_HOST'],
$_SERVER['SERVER_NAME'],
$_SERVER['SERVER_PORT'],
$_SERVER['SCRIPT_NAME'],
$_SERVER['REQUEST_URI'],
$_SERVER['QUERY_STRING'],
$_SERVER['HTTP_CONTENT_TYPE'],
$_SERVER['HTTP_AUTHORIZATION']
);
$_POST = array();
$_GET = array();
}
Expand Down Expand Up @@ -49,7 +60,7 @@ public static function build_request( $method, $uri, $post_data = '', $auth_head

if( $method == 'POST' ) {
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$_POST = parse_str($post_data);
parse_str( $post_data, $_POST );
OAuthRequest::$POST_INPUT = 'data:application/x-www-form-urlencoded,'.$post_data;
}

Expand Down

0 comments on commit 1dc72ba

Please sign in to comment.