Skip to content

Commit

Permalink
Fix test.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jan 18, 2018
1 parent d23dc79 commit 9dc2ea8
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/BasicService/Jssdk/Client.php
Expand Up @@ -27,7 +27,7 @@ class Client extends BaseClient
/**
* @var string
*/
protected $baseUri = 'https://api.weixin.qq.com/cgi-bin/';
protected $ticketEndpoint = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';

/**
* Current URI.
Expand Down Expand Up @@ -93,7 +93,7 @@ public function getTicket(bool $refresh = false, string $type = 'jsapi'): array
}

$result = $this->castResponseToType(
$this->requestRaw('ticket/getticket', 'GET', ['query' => ['type' => $type]]),
$this->requestRaw($this->ticketEndpoint, 'GET', ['query' => ['type' => $type]]),
'array'
);

Expand Down
14 changes: 14 additions & 0 deletions src/Kernel/AccessToken.php
Expand Up @@ -84,6 +84,8 @@ public function __construct(Container $app)
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function getRefreshedToken(): array
{
Expand All @@ -97,6 +99,8 @@ public function getRefreshedToken(): array
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function getToken(bool $refresh = false): array
{
Expand Down Expand Up @@ -137,6 +141,8 @@ public function setToken(string $token, int $lifetime = 7200): AccessTokenInterf
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function refresh(): AccessTokenInterface
{
Expand All @@ -153,6 +159,7 @@ public function refresh(): AccessTokenInterface
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function requestToken(array $credentials, $toArray = false)
{
Expand All @@ -172,6 +179,11 @@ public function requestToken(array $credentials, $toArray = false)
* @param array $requestOptions
*
* @return \Psr\Http\Message\RequestInterface
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function applyToRequest(RequestInterface $request, array $requestOptions = []): RequestInterface
{
Expand Down Expand Up @@ -215,6 +227,8 @@ protected function getCacheKey()
*
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
* @throws \Psr\SimpleCache\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
protected function getQuery(): array
{
Expand Down
6 changes: 6 additions & 0 deletions src/OfficialAccount/Card/JssdkClient.php
Expand Up @@ -27,6 +27,9 @@ class JssdkClient extends Jssdk
* @param string $type
*
* @return array
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getTicket(bool $refresh = false, string $type = 'wx_card'): array
{
Expand Down Expand Up @@ -54,6 +57,9 @@ public function assign(array $cards)
* @param array $extension
*
* @return array
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function attachExtension($cardId, array $extension = [])
{
Expand Down
6 changes: 6 additions & 0 deletions src/Work/Agent/Client.php
Expand Up @@ -26,6 +26,8 @@ class Client extends BaseClient
* @param int $agentId
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function get(int $agentId)
{
Expand All @@ -43,6 +45,8 @@ public function get(int $agentId)
* @param array $attributes
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function set(int $agentId, array $attributes)
{
Expand All @@ -53,6 +57,8 @@ public function set(int $agentId, array $attributes)
* Get agent list.
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function list()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Work/Base/Client.php
Expand Up @@ -24,6 +24,8 @@ class Client extends BaseClient
* Get callback ip.
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function getCallbackIp()
{
Expand Down
8 changes: 8 additions & 0 deletions src/Work/Department/Client.php
Expand Up @@ -26,6 +26,8 @@ class Client extends BaseClient
* @param array $data
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function create(array $data)
{
Expand All @@ -39,6 +41,8 @@ public function create(array $data)
* @param array $data
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function update(int $id, array $data)
{
Expand All @@ -51,6 +55,8 @@ public function update(int $id, array $data)
* @param int $id
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function delete($id)
{
Expand All @@ -63,6 +69,8 @@ public function delete($id)
* @param int|null $id
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function list($id = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Work/Jssdk/Client.php
Expand Up @@ -20,7 +20,7 @@
*/
class Client extends BaseClient
{
const API_GET_TICKET = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket';
protected $ticketEndpoint = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket';

/**
* @return string
Expand Down
4 changes: 2 additions & 2 deletions tests/BasicService/Jssdk/ClientTest.php
Expand Up @@ -80,15 +80,15 @@ public function testGetTicket()
$cache->expects()->has($cacheKey)->andReturn(false);
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500)->once();
$client->expects()->requestRaw('ticket/getticket', 'GET', ['query' => ['type' => 'jsapi']])->andReturn($response)->once();
$client->expects()->requestRaw('https://api.weixin.qq.com/cgi-bin/ticket/getticket', 'GET', ['query' => ['type' => 'jsapi']])->andReturn($response)->once();

$this->assertSame($ticket, $client->getTicket());

// with refresh and cached
$cache->expects()->has('mock-cache-key')->never();
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500)->once();
$client->expects()->requestRaw('ticket/getticket', 'GET', ['query' => ['type' => 'jsapi']])->andReturn($response)->once();
$client->expects()->requestRaw('https://api.weixin.qq.com/cgi-bin/ticket/getticket', 'GET', ['query' => ['type' => 'jsapi']])->andReturn($response)->once();

$this->assertSame($ticket, $client->getTicket(true));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/OfficialAccount/Card/JssdkClientTest.php
Expand Up @@ -35,7 +35,7 @@ public function testGetTicket()
$cache->expects()->has($cacheKey)->andReturn(false);
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500)->once();
$client->expects()->requestRaw('ticket/getticket', 'GET', ['query' => ['type' => 'wx_card']])->andReturn($response)->once();
$client->expects()->requestRaw('https://api.weixin.qq.com/cgi-bin/ticket/getticket', 'GET', ['query' => ['type' => 'wx_card']])->andReturn($response)->once();

$this->assertSame($ticket, $client->getTicket());
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Work/Jssdk/ClientTest.php
Expand Up @@ -11,6 +11,7 @@

namespace EasyWeChat\Tests\Work\Jssdk;

use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\Tests\TestCase;
use EasyWeChat\Work\Application;
use EasyWeChat\Work\Jssdk\Client;
Expand All @@ -23,4 +24,43 @@ public function testGetAppid()
$client->expects()->getAppId()->passthru();
$this->assertSame('wx123', $client->getAppId());
}

public function testGetTicket()
{
$app = new ServiceContainer([
'corp_id' => '123456',
]);
$client = $this->mockApiClient(Client::class, ['getCache'], $app);
$cache = \Mockery::mock(CacheInterface::class);
$ticket = [
'ticket' => 'mock-ticket',
'expires_in' => 7200,
];
$cacheKey = 'easywechat.basic_service.jssdk.ticket.jsapi.123456';
$client->allows()->getCache()->andReturn($cache);

// no refresh and cached
$cache->expects()->has($cacheKey)->andReturn(true);
$cache->expects()->get($cacheKey)->andReturn($ticket);

$this->assertSame($ticket, $client->getTicket());

// no refresh and no cached
$response = new \EasyWeChat\Kernel\Http\Response(200, [], json_encode($ticket));

$cache->expects()->has($cacheKey)->andReturn(false);
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500)->once();
$client->expects()->requestRaw('https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket', 'GET', ['query' => ['type' => 'jsapi']])->andReturn($response)->once();

$this->assertSame($ticket, $client->getTicket());

// with refresh and cached
$cache->expects()->has('mock-cache-key')->never();
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500)->once();
$client->expects()->requestRaw('https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket', 'GET', ['query' => ['type' => 'jsapi']])->andReturn($response)->once();

$this->assertSame($ticket, $client->getTicket(true));
}
}

0 comments on commit 9dc2ea8

Please sign in to comment.