Skip to content

Commit

Permalink
Add api. (#700)
Browse files Browse the repository at this point in the history
* Add api.

* StyleCI.

* New line.
  • Loading branch information
mingyoung authored and overtrue committed May 16, 2017
1 parent 60388ee commit 62f2a91
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Foundation/Application.php
Expand Up @@ -78,6 +78,7 @@ class Application extends Container
* @var array
*/
protected $providers = [
ServiceProviders\FundamentalServiceProvider::class,
ServiceProviders\ServerServiceProvider::class,
ServiceProviders\UserServiceProvider::class,
ServiceProviders\JsServiceProvider::class,
Expand Down Expand Up @@ -267,4 +268,23 @@ private function initializeLogger()

Log::setLogger($logger);
}

/**
* Magic call.
*
* @param string $method
* @param array $args
*
* @return mixed
*
* @throws \Exception
*/
public function __call($method, $args)
{
if (is_callable([$this['fundamental.api'], $method])) {
return call_user_func_array([$this['fundamental.api'], $method], $args);
}

throw new \Exception("Call to undefined method {$method}()");
}
}
43 changes: 43 additions & 0 deletions src/Foundation/ServiceProviders/FundamentalServiceProvider.php
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* FundamentalServiceProvider.php.
*
* This file is part of the wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Foundation\ServiceProviders;

use EasyWeChat\Fundamental\API;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class FundamentalServiceProvider.
*/
class FundamentalServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $pimple)
{
$pimple['fundamental.api'] = function ($pimple) {
return new API($pimple['access_token']);
};
}
}
52 changes: 52 additions & 0 deletions src/Fundamental/API.php
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Fundamental API.
*
* @author mingyoung <mingyoungcheung@gmail.com>
* @copyright 2017
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/

namespace EasyWeChat\Fundamental;

use EasyWeChat\Core\AbstractAPI;

class API extends AbstractAPI
{
const API_CLEAR_QUOTA = 'https://api.weixin.qq.com/cgi-bin/clear_quota';
const API_CALLBACK_IP = 'https://api.weixin.qq.com/cgi-bin/getcallbackip';

/**
* Clear quota.
*
* @return \EasyWeChat\Support\Collection
*/
public function clearQuota()
{
$appid = $this->getAccessToken()->getAppId();

return $this->parseJSON('json', [self::API_CLEAR_QUOTA, compact('appid')]);
}

/**
* Get wechat callback ip.
*
* @return \EasyWeChat\Support\Collection
*/
public function getCallbackIp()
{
return $this->parseJSON('get', [self::API_CALLBACK_IP]);
}
}
54 changes: 54 additions & 0 deletions tests/Fundamental/FundamentalAPITest.php
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

use EasyWeChat\Fundamental\API;
use EasyWeChat\Tests\TestCase;

class FundamentalAPITest extends TestCase
{
/**
* Test clearQuota().
*/
public function testClearQuota()
{
$result = $this->make()->clearQuota();

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/clear_quota', $result['api']);
$this->assertSame(['appid' => 'i-am-app-id'], $result['params']);
}

/**
* Test getCallbackIp().
*/
public function testGetCallbackIp()
{
$result = $this->make()->getCallbackIp();

$this->assertEquals('https://api.weixin.qq.com/cgi-bin/getcallbackip', $result['api']);
}

private function make()
{
$accessToken = \Mockery::mock('EasyWeChat\Core\AccessToken', function ($mock) {
$mock->shouldReceive('getAppId')->andReturn('i-am-app-id');
});
$api = \Mockery::mock('EasyWeChat\Fundamental\API[parseJSON]', [$accessToken]);
$api->shouldReceive('parseJSON')->andReturnUsing(function ($api, $params) {
if (isset($params[1])) {
return ['api' => $params[0], 'params' => $params[1]];
}

return ['api' => $params[0]];
});

return $api;
}
}

0 comments on commit 62f2a91

Please sign in to comment.