Skip to content

Commit

Permalink
添加小程序一次性订阅消息发送支持 (#1704)
Browse files Browse the repository at this point in the history
* 添加小程序一次性订阅消息发送支持

* fix tests and data check

* phpcs requirement

* Application $subscribe_message property

* combine formatMessage & formatData and more testCase

* rollback phpunit8 code
  • Loading branch information
Hugo Zhang authored and overtrue committed Oct 19, 2019
1 parent a4259a5 commit 64458fa
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/MiniProgram/Application.php
Expand Up @@ -38,6 +38,7 @@
* @property \EasyWeChat\BasicService\Media\Client $media
* @property \EasyWeChat\BasicService\ContentSecurity\Client $content_security
* @property \EasyWeChat\MiniProgram\Mall\ForwardsMall $mall
* @property \EasyWeChat\MiniProgram\SubscribeMessage\Client $subscribe_message
*/
class Application extends ServiceContainer
{
Expand All @@ -61,6 +62,7 @@ class Application extends ServiceContainer
OCR\ServiceProvider::class,
Soter\ServiceProvider::class,
Mall\ServiceProvider::class,
SubscribeMessage\ServiceProvider::class,
// Base services
BasicService\Media\ServiceProvider::class,
BasicService\ContentSecurity\ServiceProvider::class,
Expand Down
112 changes: 112 additions & 0 deletions src/MiniProgram/SubscribeMessage/Client.php
@@ -0,0 +1,112 @@
<?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.
*/

namespace EasyWeChat\MiniProgram\SubscribeMessage;

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

/**
* Class Client.
*
* @author hugo <rabbitzhang52@gmail.com>
*/
class Client extends BaseClient
{
/**
* {@inheritdoc}.
*/
protected $message = [
'touser' => '',
'template_id' => '',
'page' => '',
'data' => [],
];

/**
* {@inheritdoc}.
*/
protected $required = ['touser', 'template_id', 'data'];

/**
* Send a template message.
*
* @param array $data
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function send(array $data = [])
{
$params = $this->formatMessage($data);

$this->restoreMessage();

return $this->httpPostJson('cgi-bin/message/subscribe/send', $params);
}

/**
* @param array $data
*
* @return array
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
protected function formatMessage(array $data = [])
{
$params = array_merge($this->message, $data);

foreach ($params as $key => $value) {
if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) {
throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
}

$params[$key] = empty($value) ? $this->message[$key] : $value;
}

foreach ($params['data'] as $key => $value) {
if (is_array($value)) {
if (isset($value['value'])) {
$params['data'][$key] = ['value' => $value['value']];

continue;
}

if (count($value) >= 1) {
$value = [
'value' => $value[0],
// 'color' => $value[1],// color unsupported
];
}
} else {
$value = [
'value' => strval($value),
];
}

$params['data'][$key] = $value;
}

return $params;
}

/**
* Restore message.
*/
protected function restoreMessage()
{
$this->message = (new ReflectionClass(static::class))->getDefaultProperties()['message'];
}
}
28 changes: 28 additions & 0 deletions src/MiniProgram/SubscribeMessage/ServiceProvider.php
@@ -0,0 +1,28 @@
<?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.
*/

namespace EasyWeChat\MiniProgram\SubscribeMessage;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['subscribe_message'] = function ($app) {
return new Client($app);
};
}
}
117 changes: 117 additions & 0 deletions tests/MiniProgram/SubscribeMessage/ClientTest.php
@@ -0,0 +1,117 @@
<?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.
*/

namespace EasyWeChat\Tests\MiniProgram\SubscribeMessage;

use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\MiniProgram\SubscribeMessage\Client;
use EasyWeChat\Tests\TestCase;

class ClientTest extends TestCase
{
public function testSend()
{
$client = $this->mockApiClient(Client::class)->makePartial();

// without touser
try {
$client->send();
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidArgumentException::class, $e);
$this->assertSame('Attribute "touser" can not be empty!', $e->getMessage());
}

// without template_id
try {
$client->send(['touser' => 'mock-openid']);
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidArgumentException::class, $e);
$this->assertSame('Attribute "template_id" can not be empty!', $e->getMessage());
}

// without data
try {
$client->send(['touser' => 'mock-openid', 'template_id' => 'mock-template_id']);
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidArgumentException::class, $e);
$this->assertSame('Attribute "data" can not be empty!', $e->getMessage());
}

// format message
$this->assertSame([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
], $client->formatMessage([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => 'thing1.DATA'],
]));

$this->assertSame([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
], $client->formatMessage([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['thing1.DATA']],
]));

$this->assertSame([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
], $client->formatMessage([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['thing1.DATA', 'ignore value']],
]));

$this->assertSame([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
], $client->formatMessage([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
]));

$this->assertSame([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
], $client->formatMessage([
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA', 'color' => 'ignore value']],
]));

$client->expects()->httpPostJson('cgi-bin/message/subscribe/send', [
'touser' => 'mock-openid',
'template_id' => 'mock-template_id',
'page' => '',
'data' => ['thing1' => ['value' => 'thing1.DATA']],
])->andReturn('mock-result');
$this->assertSame('mock-result', $client->send(['touser' => 'mock-openid', 'template_id' => 'mock-template_id', 'data' => ['thing1' => 'thing1.DATA']]));
}
}

0 comments on commit 64458fa

Please sign in to comment.