Skip to content

Commit

Permalink
添加小程序订阅消息模板相关支持 (w7corp#1745)
Browse files Browse the repository at this point in the history
* 添加小程序订阅消息模板相关支持

* 添加小程序订阅消息模板单元测试

* Delete extra blank lines

* Fix styleci errors
  • Loading branch information
her-cat authored and overtrue committed Nov 27, 2019
1 parent baa93cb commit 853e077
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/MiniProgram/SubscribeMessage/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,100 @@ protected function restoreMessage()
{
$this->message = (new ReflectionClass(static::class))->getDefaultProperties()['message'];
}

/**
* Combine templates and add them to your personal template library under your account.
*
* @param string $tid
* @param array $kidList
* @param string|null $sceneDesc
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function addTemplate(string $tid, array $kidList, string $sceneDesc = null)
{
$sceneDesc = $sceneDesc ?? '';
$data = \compact('tid', 'kidList', 'sceneDesc');

return $this->httpPost('wxaapi/newtmpl/addtemplate', $data);
}

/**
* Delete personal template under account.
*
* @param string $id
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function deleteTemplate(string $id)
{
return $this->httpPost('wxaapi/newtmpl/deltemplate', ['priTmplId' => $id]);
}

/**
* Get keyword list under template title.
*
* @param string $tid
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getTemplateKeywords(string $tid)
{
return $this->httpGet('wxaapi/newtmpl/getpubtemplatekeywords', compact('tid'));
}

/**
* Get the title of the public template under the category to which the account belongs.
*
* @param array $ids
* @param int $start
* @param int $limit
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getTemplateTitles(array $ids, int $start = 0, int $limit = 30)
{
$ids = \implode(',', $ids);
$query = \compact('ids', 'start', 'limit');

return $this->httpGet('wxaapi/newtmpl/getpubtemplatetitles', $query);
}

/**
* Get list of personal templates under the current account.
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getTemplates()
{
return $this->httpGet('wxaapi/newtmpl/gettemplate');
}

/**
* Get the category of the applet account.
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getCategory()
{
return $this->httpGet('wxaapi/newtmpl/getcategory');
}
}
66 changes: 66 additions & 0 deletions tests/MiniProgram/SubscribeMessage/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,70 @@ public function testSend()
])->andReturn('mock-result');
$this->assertSame('mock-result', $client->send(['touser' => 'mock-openid', 'template_id' => 'mock-template_id', 'data' => ['thing1' => 'thing1.DATA']]));
}

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

$client->expects()->httpPost('wxaapi/newtmpl/addtemplate', [
'tid' => 'mock-tid',
'kidList' => ['mock-kid'],
'sceneDesc' => 'mock-scene',
])->andReturn('mock-result');

$this->assertSame('mock-result', $client->addTemplate('mock-tid', ['mock-kid'], 'mock-scene'));
}

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

$client->expects()->httpPost('wxaapi/newtmpl/deltemplate', [
'priTmplId' => 'mock-template-id',
])->andReturn('mock-result');

$this->assertSame('mock-result', $client->deleteTemplate('mock-template-id'));
}

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

$client->expects()->httpGet('wxaapi/newtmpl/getpubtemplatekeywords', [
'tid' => 'mock-tid',
])->andReturn('mock-result');

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

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

$client->expects()->httpGet('wxaapi/newtmpl/getpubtemplatetitles', [
'ids' => 'mock-tid1,mock-tid2',
'start' => 0,
'limit' => 30,
])->andReturn('mock-result');

$this->assertSame('mock-result', $client->getTemplateTitles(['mock-tid1', 'mock-tid2']));
}

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

$client->expects()->httpGet('wxaapi/newtmpl/gettemplate')->andReturn('mock-result');

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

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

$client->expects()->httpGet('wxaapi/newtmpl/getcategory')->andReturn('mock-result');

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

0 comments on commit 853e077

Please sign in to comment.