Skip to content

Commit

Permalink
添加公众号检查回调地址网络情况接口 (w7corp#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
her-cat authored and overtrue committed Sep 16, 2019
1 parent b9690e9 commit c641763
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/OfficialAccount/Base/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace EasyWeChat\OfficialAccount\Base;

use EasyWeChat\Kernel\BaseClient;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;

/**
* Class Client.
Expand Down Expand Up @@ -48,4 +49,37 @@ public function getValidIps()
{
return $this->httpGet('cgi-bin/getcallbackip');
}

/**
* Check the callback address network.
*
* @param string $action
* @param string $operator
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function checkCallbackUrl(string $action = 'all', string $operator = 'DEFAULT')
{
if (!in_array($action, ['dns', 'ping', 'all'])) {
throw new InvalidArgumentException('The action must be dns, ping, all.');
}

$operator = strtoupper($operator);

if (!in_array($operator, ['CHINANET', 'UNICOM', 'CAP', 'DEFAULT'])) {
throw new InvalidArgumentException('The operator must be CHINANET, UNICOM, CAP, DEFAULT.');
}

$params = [
'action' => $action,
'check_operator' => $operator,
];

return $this->httpPostJson('cgi-bin/callback/check', $params);
}
}
27 changes: 27 additions & 0 deletions tests/OfficialAccount/Base/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace EasyWeChat\Tests\OfficialAccount\Base;

use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\OfficialAccount\Base\Client;
use EasyWeChat\Tests\TestCase;
Expand All @@ -36,4 +37,30 @@ public function testGetValidIps()

$this->assertSame('mock-result', $client->getValidIps());
}

public function testCheckCallbackUrl()
{
$client = $this->mockApiClient(Client::class);

$client->expects()->httpPostJson('cgi-bin/callback/check', [
'action' => 'all',
'check_operator' => 'DEFAULT',
])->andReturn('mock-result');

$this->assertSame('mock-result', $client->checkCallbackUrl());

try {
$client->checkCallbackUrl('invalid-action');
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidArgumentException::class, $e);
$this->assertSame('The action must be dns, ping, all.', $e->getMessage());
}

try {
$client->checkCallbackUrl('all', 'invalid-operator');
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidArgumentException::class, $e);
$this->assertSame('The operator must be CHINANET, UNICOM, CAP, DEFAULT.', $e->getMessage());
}
}
}

0 comments on commit c641763

Please sign in to comment.