Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MYOB Service #551

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Included service implementations
- Mailchimp
- Microsoft
- Mondo
- MYOB
- Nest
- Netatmo
- Parrot Flower Power
Expand Down
4 changes: 4 additions & 0 deletions examples/init.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
'key' => '',
'secret' => '',
),
'myob' => array(
'key' => '',
'secret' => '',
),
'nest' => array(
'key' => '',
'secret' => '',
Expand Down
45 changes: 45 additions & 0 deletions examples/myob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Example of retrieving an authentication token of the MYOB service
*
* PHP version 5.4
*
* @author Jervy Escoto <[email protected]>
* @copyright Copyright (c) 2012 The authors
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/

use OAuth\Common\Storage\Session;
use OAuth\Common\Consumer\Credentials;

/**
* Bootstrap the example
*/
require_once __DIR__ . '/bootstrap.php';

// Session storage
$storage = new Session();

// Setup the credentials for the requests
$credentials = new Credentials(
$servicesCredentials['myob']['key'],
$servicesCredentials['myob']['secret'],
$currentUri->getAbsoluteUri()
);

// Instantiate MYOB service using the credentials, http client, storage mechanism for the token and profile scope
/** @var OAuth\OAuth2\Service\Myob $myobService */
$myobService = $serviceFactory->createService('myob', $credentials, $storage,
array(\OAuth\OAuth2\Service\Myob::SCOPE_COMPANYFILE));

if (!empty($_GET['code'])) {
// This was a callback request from MYOB, get the token
$token = $myobService->requestAccessToken($_GET['code']);

} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
$url = $myobService->getAuthorizationUri();
header('Location: ' . $url);
} else {
$url = $currentUri->getRelativeUri() . '?go=go';
echo "<a href='$url'>Login with MYOB!</a>";
}
97 changes: 97 additions & 0 deletions src/OAuth/OAuth2/Service/Myob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace OAuth\OAuth2\Service;

use OAuth\OAuth2\Token\StdOAuth2Token;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Http\Exception\TokenResponseException;

/**
* MYOB Service
* @link http://developer.myob.com/api/accountright/api-overview/authentication/
*/
class Myob extends AbstractService
{
/**
* @var string The CompanyFile scope
*/
const SCOPE_COMPANYFILE = "CompanyFile";

/**
* @inheritdoc
*/
public function getAuthorizationEndpoint()
{
return new Uri("https://secure.myob.com/oauth2/account/authorize");
}

/**
* @inheritdoc
*/
public function getAccessTokenEndpoint()
{
return new Uri("https://secure.myob.com/oauth2/v1/authorize");
}

/**
* @inheritdoc
*/
protected function parseAccessTokenResponse($responseBody)
{
$data = json_decode($responseBody, true);

if (null === $data || !is_array($data)) {
throw new TokenResponseException('Unable to parse response.');
} elseif (isset($data['error_description'])) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"');
} elseif (isset($data['error'])) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}

$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);
$token->setLifeTime($data['expires_in']);

if (isset($data['refresh_token'])) {
$token->setRefreshToken($data['refresh_token']);
unset($data['refresh_token']);
}

unset($data['access_token']);
unset($data['expires_in']);

$token->setExtraParams($data);

return $token;
}

/**
* @inheritdoc
*/
public function requestAccessToken($code, $state = null)
{
if (null !== $state) {
$this->validateAuthorizationState($state);
}

$bodyParams = array(
'code' => $code,
'client_id' => $this->credentials->getConsumerId(),
'client_secret' => $this->credentials->getConsumerSecret(),
'redirect_uri' => $this->credentials->getCallbackUrl(),
'grant_type' => 'authorization_code',
'scope' => implode(' ', $this->scopes),
);

$responseBody = $this->httpClient->retrieveResponse(
$this->getAccessTokenEndpoint(),
$bodyParams,
$this->getExtraOAuthHeaders()
);

$token = $this->parseAccessTokenResponse($responseBody);
$this->storage->storeAccessToken($this->service(), $token);

return $token;
}
}
190 changes: 190 additions & 0 deletions tests/Unit/OAuth2/Service/MyobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace OAuthTest\Unit\OAuth2\Service;

use OAuth\Common\Token\TokenInterface;
use OAuth\OAuth2\Service\Myob;

class MyobTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers OAuth\OAuth2\Service\Myob::__construct
*/
public function testConstructCorrectInterfaceWithoutCustomUri()
{
$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
*/
public function testConstructCorrectInstanceWithoutCustomUri()
{
$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
*/
public function testConstructCorrectInstanceWithCustomUri()
{
$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
array(),
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::getAuthorizationEndpoint
*/
public function testGetAuthorizationEndpoint()
{
$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertSame('https://secure.myob.com/oauth2/account/authorize',
$service->getAuthorizationEndpoint()->getAbsoluteUri());
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::getAccessTokenEndpoint
*/
public function testGetAccessTokenEndpoint()
{
$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertSame('https://secure.myob.com/oauth2/v1/authorize',
$service->getAccessTokenEndpoint()->getAbsoluteUri());
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::getAuthorizationMethod
*/
public function testGetAuthorizationMethod()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2));

$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));

$storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
$storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));

$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$storage
);

$headers = $service->request('https://pieterhordijk.com/my/awesome/path');

$this->assertTrue(array_key_exists('Authorization', $headers));
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));

$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');

$service->requestAccessToken('foo');
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error_description=some_error'));

$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');

$service->requestAccessToken('foo');
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseThrowsExceptionOnError()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));

$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');

$service->requestAccessToken('foo');
}

/**
* @covers OAuth\OAuth2\Service\Myob::__construct
* @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
*/
public function testParseAccessTokenResponseValidWithoutRefreshToken()
{
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));

$service = new Myob(
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
$client,
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
);

$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
}
}