Skip to content

Commit

Permalink
add mockery
Browse files Browse the repository at this point in the history
  • Loading branch information
AlloVince committed Apr 17, 2015
1 parent 8d99bcd commit 0604539
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -17,6 +17,7 @@
"monolog/monolog": "1.13.*"
},
"require-dev": {
"mockery/mockery": "dev-master",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
Expand Down
4 changes: 4 additions & 0 deletions examples/config.php
Expand Up @@ -9,6 +9,10 @@
'key' => '',
'secret' => '',
],
'weibo' => [
'key' => '',
'secret' => '',
],
'hundsun' => [
'key' => '',
'secret' => ''
Expand Down
2 changes: 2 additions & 0 deletions examples/service.php
Expand Up @@ -17,3 +17,5 @@
'callback' => dirname('http://' . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"]) . '/access.php?provider=' . $provider
]);

$service->debug();
$service->setLogPath(__DIR__ . '/../tmp/access.log');
32 changes: 32 additions & 0 deletions src/EvaOAuth/OAuth2/Providers/Weibo.php
@@ -0,0 +1,32 @@
<?php
/**
* @author AlloVince
* @copyright Copyright (c) 2015 EvaEngine Team (https://github.com/EvaEngine)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Eva\EvaOAuth\OAuth2\Providers;

use Eva\EvaOAuth\OAuth2\ResourceServerInterface;

/**
* Class Weibo
* @package Eva\EvaOAuth\OAuth2\Providers
*/
class Weibo extends AbstractProvider
{
/**
* @var string
*/
protected $authorizeUrl = 'https://api.weibo.com/oauth2/authorize';

/**
* @var string
*/
protected $accessTokenUrl = 'https://api.weibo.com/oauth2/access_token';

/**
* @var string
*/
protected $accessTokenFormat = ResourceServerInterface::FORMAT_JSON;
}
29 changes: 29 additions & 0 deletions src/EvaOAuth/Service.php
Expand Up @@ -61,6 +61,7 @@ class Service
'twitter' => 'Eva\EvaOAuth\OAuth1\Providers\Twitter',
'douban' => 'Eva\EvaOAuth\OAuth2\Providers\Douban',
'tencent' => 'Eva\EvaOAuth\OAuth2\Providers\Tencent',
'weibo' => 'Eva\EvaOAuth\OAuth2\Providers\Weibo',
'hundsun' => 'Eva\EvaOAuth\OAuth2\Providers\Hundsun',
];

Expand All @@ -83,6 +84,22 @@ public static function registerProviders(array $classes)
}
}

/**
* @return array
*/
public static function getProviders()
{
return self::$providers;
}

/**
* @return OAuth1Provider|OAuth2Provider
*/
public function getProvider()
{
return $this->provider;
}

/**
* @return Consumer|Client
*/
Expand Down Expand Up @@ -144,6 +161,18 @@ public function setLogPath($logPath)
return $this;
}

/**
* Enable debug mode
* Guzzle will print all request and response on screen
* @return $this
*/
public function debug()
{
$adapter = $this->getAdapter();
$adapter::getHttpClient()->getEmitter()->attach(new LogSubscriber(og, Formatter::DEBUG));
return $this;
}

/**
* @param string $providerName
* @param array $options
Expand Down
73 changes: 73 additions & 0 deletions tests/EvaOAuthTest/ServiceTest.php
@@ -0,0 +1,73 @@
<?php

namespace Eva\EvaOAuthTest;

use Eva\EvaOAuth\Service;
use Eva\EvaOAuth\OAuth1\Consumer;
use Eva\EvaOAuth\OAuth2\Client;

class ServiceTest extends \PHPUnit_Framework_TestCase
{

public function setUp()
{
}

/**
* @expectedException Eva\EvaOAuth\Exception\BadMethodCallException
*/
public function testNotExistProvider()
{
new Service('foo_service', []);
}

public function testConstruct()
{
$service = new Service('douban', [
'key' => 'test_key',
'secret' => 'test_secret',
'callback' => 'test_callback',
]);
$this->assertInstanceOf('Eva\EvaOAuth\OAuth2\Client', $service->getAdapter());

$service = new Service('twitter', [
'key' => 'test_key',
'secret' => 'test_secret',
'callback' => 'test_callback',
]);
$this->assertInstanceOf('Eva\EvaOAuth\OAuth1\Consumer', $service->getAdapter());
}

/**
* @expectedException Eva\EvaOAuth\Exception\InvalidArgumentException
*/
public function testProviderInterface()
{
\Mockery::namedMock('FooProvider');
Service::registerProvider('foo', 'FooProvider');
$service = new Service('foo', [
'key' => 'test_key',
'secret' => 'test_secret',
'callback' => 'test_callback',
]);
}

public function testRegisterProvider()
{
\Mockery::namedMock('FooOAuth2Provider', 'Eva\EvaOAuth\OAuth2\Providers\AbstractProvider');
\Mockery::namedMock('BarOAuth1Provider', 'Eva\EvaOAuth\OAuth1\Providers\AbstractProvider');
Service::registerProviders([
'foo' => 'FooOAuth2Provider',
'bar' => 'BarOAuth2Provider',
]);
$this->assertArrayHasKey('foo', Service::getProviders());
$this->assertArrayHasKey('bar', Service::getProviders());

$service = new Service('foo', [
'key' => 'test_key',
'secret' => 'test_secret',
'callback' => 'test_callback',
]);
$this->assertInstanceOf('FooOAuth2Provider', $service->getProvider());
}
}

0 comments on commit 0604539

Please sign in to comment.