diff --git a/composer.json b/composer.json index e990f6817..1c4d69521 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "ext-openssl": "*", "pimple/pimple": "~3.0", "monolog/monolog": "^1.22", - "overtrue/socialite": ">=1.0.24", + "overtrue/socialite": ">=1.0.25", "doctrine/cache": "~1.6", "guzzlehttp/guzzle": "~6.2.1", "symfony/http-foundation": "~2.6|~2.7|~2.8|~3.0", diff --git a/src/Foundation/Application.php b/src/Foundation/Application.php index 87b4ebec2..08adf0ed3 100644 --- a/src/Foundation/Application.php +++ b/src/Foundation/Application.php @@ -296,4 +296,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}()"); + } } diff --git a/src/Foundation/ServiceProviders/FundamentalServiceProvider.php b/src/Foundation/ServiceProviders/FundamentalServiceProvider.php new file mode 100644 index 000000000..a7dc2e34c --- /dev/null +++ b/src/Foundation/ServiceProviders/FundamentalServiceProvider.php @@ -0,0 +1,43 @@ + + * + * 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 + * + * 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']); + }; + } +} diff --git a/src/Fundamental/API.php b/src/Fundamental/API.php new file mode 100644 index 000000000..41718e22e --- /dev/null +++ b/src/Fundamental/API.php @@ -0,0 +1,52 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * Fundamental API. + * + * @author mingyoung + * @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]); + } +} diff --git a/src/OfficialAccount/TemplateMessage/TemplateMessage.php b/src/OfficialAccount/TemplateMessage/TemplateMessage.php index 9edbeb6d1..46dbf7abd 100644 --- a/src/OfficialAccount/TemplateMessage/TemplateMessage.php +++ b/src/OfficialAccount/TemplateMessage/TemplateMessage.php @@ -82,6 +82,20 @@ public function __construct(AccessToken $accessToken) $this->messageBackup = $this->message; } + /** + * Set default color. + * + * @param string $color example: #0f0f0f + * + * @return $this + */ + public function defaultColor($color) + { + $this->defaultColor = $color; + + return $this; + } + /** * Set industry. * diff --git a/src/Support/Log.php b/src/Support/Log.php index fc52a17ec..e77de07c3 100644 --- a/src/Support/Log.php +++ b/src/Support/Log.php @@ -113,7 +113,7 @@ private static function createDefaultLogger() { $log = new Logger('EasyWeChat'); - if (defined('PHPUNIT_RUNNING')) { + if (defined('PHPUNIT_RUNNING') || php_sapi_name() === 'cli') { $log->pushHandler(new NullHandler()); } else { $log->pushHandler(new ErrorLogHandler()); diff --git a/tests/Fundamental/FundamentalAPITest.php b/tests/Fundamental/FundamentalAPITest.php new file mode 100644 index 000000000..e94cc9e62 --- /dev/null +++ b/tests/Fundamental/FundamentalAPITest.php @@ -0,0 +1,54 @@ + + * + * 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; + } +}