Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 添加小程序一次性订阅消息发送支持 * fix tests and data check * phpcs requirement * Application $subscribe_message property * combine formatMessage & formatData and more testCase * rollback phpunit8 code
- Loading branch information
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']])); | ||
} | ||
} |