diff --git a/src/Broadcast/Broadcast.php b/src/Broadcast/Broadcast.php new file mode 100755 index 000000000..88920f14c --- /dev/null +++ b/src/Broadcast/Broadcast.php @@ -0,0 +1,129 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * Broadcast.php. + * + * @author overtrue + * @copyright 2015 overtrue + * + * @link https://github.com/overtrue + * @link http://overtrue.me + */ + +namespace EasyWeChat\Broadcast; + +use EasyWeChat\Core\AbstractAPI; +use EasyWeChat\Core\Exceptions\HttpException; + +/** + * Class Broadcast. + */ +class Broadcast extends AbstractAPI +{ + const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall'; + const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send'; + const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete'; + const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview'; + const API_GET = 'http://api.weixin.qq.com/cgi-bin/message/mass/get'; + + const PREVIEW_BY_OPENID = 'touser'; + const PREVIEW_BY_WXH = 'towxname'; + + const MSG_TYPE_TEXT = 'text'; // 文本 + const MSG_TYPE_NEWS = 'news'; // 图文 + const MSG_TYPE_VOICE = 'voice'; // 语音 + const MSG_TYPE_IMAGE = 'image'; // 图片 + const MSG_TYPE_VIDEO = 'video'; // 视频 + const MSG_TYPE_CARD = 'card'; // 卡券 + + /** + * Send a message. + * + * @param string $msgType message type + * @param mixed $message message + * @param mixed $to + * + * @return mixed + */ + public function send($msgType, $message, $to = null) + { + $message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->build(); + + $api = is_array($to) ? self::API_SEND_BY_OPENID : self::API_SEND_BY_GROUP; + + return $this->post($api, $message); + } + + /** + * Preview a message. + * + * @param string $msgType message type + * @param mixed $message message + * @param string $to + * @param string $by + * + * @return mixed + */ + public function preview($msgType, $message, $to, $by = self::PREVIEW_BY_OPENID) + { + $message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->buildPreview($by); + + return $this->post(self::API_PREVIEW, $message); + } + + /** + * Delete a broadcast. + * + * @param string $msgId + * + * @return bool + */ + public function delete($msgId) + { + $options = [ + 'msg_id' => $msgId, + ]; + + return $this->post(self::API_DELETE, $options); + } + + /** + * Get a broadcast status. + * + * @param string $msgId + * + * @return array + */ + public function status($msgId) + { + $options = [ + 'msg_id' => $msgId, + ]; + + return $this->post(self::API_GET, $options); + } + + /** + * post request. + * + * @param string $url + * @param array|string $options + * + * @return array|bool + * + * @throws HttpException + */ + private function post($url, $options) + { + return $this->parseJSON('post', [$url, $options]); + } +} diff --git a/src/Broadcast/LICENSE.txt b/src/Broadcast/LICENSE.txt new file mode 100755 index 000000000..c5251b826 --- /dev/null +++ b/src/Broadcast/LICENSE.txt @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 overtrue + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/src/Broadcast/MessageBuilder.php b/src/Broadcast/MessageBuilder.php new file mode 100755 index 000000000..3f98c34dd --- /dev/null +++ b/src/Broadcast/MessageBuilder.php @@ -0,0 +1,252 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * MessageBuilder.php. + * + * @author overtrue + * @copyright 2015 overtrue + * + * @link https://github.com/overtrue + * @link http://overtrue.me + */ + +namespace EasyWeChat\Broadcast; + +use EasyWeChat\Core\Exceptions\InvalidArgumentException; +use EasyWeChat\Core\Exceptions\RuntimeException; + +/** + * Class MessageBuilder. + */ +class MessageBuilder +{ + /** + * Message target user or group. + * + * @var mixed + */ + protected $to; + + /** + * Message type. + * + * @var string + */ + protected $msgType; + + /** + * Message. + * + * @var mixed + */ + protected $message; + + /** + * Message types. + * + * @var array + */ + private $msgTypes = [ + Broadcast::MSG_TYPE_TEXT, + Broadcast::MSG_TYPE_NEWS, + Broadcast::MSG_TYPE_IMAGE, + Broadcast::MSG_TYPE_VIDEO, + Broadcast::MSG_TYPE_VOICE, + Broadcast::MSG_TYPE_CARD, + ]; + + /** + * Preview bys. + * + * @var array + */ + private $previewBys = [ + Broadcast::PREVIEW_BY_OPENID, + Broadcast::PREVIEW_BY_WXH, + ]; + + /** + * Set message type. + * + * @param string $msgType + * + * @return MessageBuilder + * + * @throws InvalidArgumentException + */ + public function msgType($msgType) + { + if (!in_array($msgType, $this->msgTypes)) { + throw new InvalidArgumentException('This message type not exist.'); + } + + $this->msgType = $msgType; + + return $this; + } + + /** + * Set message. + * + * @param string|array $message + * + * @return MessageBuilder + */ + public function message($message) + { + $this->message = $message; + + return $this; + } + + /** + * Set target user or group. + * + * @param mixed $to + * + * @return MessageBuilder + */ + public function to($to) + { + $this->to = $to; + + return $this; + } + + /** + * Build message. + * + * @return bool + * + * @throws RuntimeException + */ + public function build() + { + if (empty($this->msgType)) { + throw new RuntimeException('message type not exist.'); + } + + if (empty($this->message)) { + throw new RuntimeException('No message content to send.'); + } + + // 群发视频消息给用户列表时,视频消息格式需要另外处理,具体见文档 + if (isset($this->to) && is_array($this->to) && $this->msgType == Broadcast::MSG_TYPE_VIDEO) { + $this->msgType = 'video'; + } elseif ($this->msgType == Broadcast::MSG_TYPE_VIDEO) { + $this->msgType = 'mpvideo'; + } + + $content = (new Transformer($this->msgType, $this->message))->transform(); + + $group = isset($this->to) ? $this->to : null; + + $message = array_merge($this->buildGroup($group), $content); + + return $message; + } + + /** + * Build preview message. + * + * @param string $by + * + * @return array + * + * @throws RuntimeException + * @throws InvalidArgumentException + */ + public function buildPreview($by) + { + if (!in_array($by, $this->previewBys)) { + throw new InvalidArgumentException('This preview by not exist.'); + } + + if (empty($this->msgType)) { + throw new RuntimeException('Message type not exist.'); + } + + if (empty($this->message)) { + throw new RuntimeException('No message content to send.'); + } + + if (empty($this->to)) { + throw new RuntimeException('No to.'); + } + + $content = (new Transformer($this->msgType, $this->message))->transform(); + + $message = array_merge($this->buildTo($this->to, $by), $content); + + return $message; + } + + /** + * Build group. + * + * @param mixed $group + * + * @return array + */ + private function buildGroup($group) + { + if (is_null($group)) { + $group = [ + 'filter' => [ + 'is_to_all' => true, + ], + ]; + } elseif (is_array($group)) { + $group = [ + 'touser' => $group, + ]; + } else { + $group = [ + 'filter' => [ + 'is_to_all' => false, + 'group_id' => $group, + ], + ]; + } + + return $group; + } + + /** + * Build to. + * + * @param string $to + * @param string $by + * + * @return array + */ + private function buildTo($to, $by) + { + return [ + $by => $to, + ]; + } + + /** + * Return property. + * + * @param string $property + * + * @return mixed + */ + public function __get($property) + { + if (property_exists($this, $property)) { + return $this->$property; + } + } +} diff --git a/src/Broadcast/README.md b/src/Broadcast/README.md new file mode 100755 index 000000000..a68f40593 --- /dev/null +++ b/src/Broadcast/README.md @@ -0,0 +1,2 @@ +# broadcast +微信 SDK 群发模块 diff --git a/src/Broadcast/Transformer.php b/src/Broadcast/Transformer.php new file mode 100755 index 000000000..363554288 --- /dev/null +++ b/src/Broadcast/Transformer.php @@ -0,0 +1,196 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * Transformer.php. + * + * @author overtrue + * @copyright 2015 overtrue + * + * @link https://github.com/overtrue + * @link http://overtrue.me + */ + +namespace EasyWeChat\Broadcast; + +use EasyWeChat\Core\Exceptions\InvalidArgumentException; + +/** + * Class Transformer. + */ +class Transformer +{ + /** + * Message type. + * + * @var string + */ + protected $msgType; + + /** + * message. + * + * @var mixed + */ + protected $message; + + /** + * Transformer constructor. + * + * @param $msgType + * @param $message + */ + public function __construct($msgType, $message) + { + $this->msgType = $msgType; + + $this->message = $message; + } + + /** + * Transform message. + * + * @return array + */ + public function transform() + { + $handle = sprintf("transform%s", ucfirst($this->msgType)); + + return method_exists($this, $handle) ? $this->$handle($this->message) : []; + } + + /** + * Transform text message. + * + * @param string $message + * + * @return array + */ + public function transformText($message) + { + return [ + 'text' => [ + 'content' => $message, + ], + 'msgtype' => 'text', + ]; + } + + /** + * Transform news message. + * + * @param string $message + * + * @return array + */ + public function transformNews($message) + { + return [ + 'mpnews' => [ + 'media_id' => $message, + ], + 'msgtype' => 'mpnews', + ]; + } + + /** + * Transform image message. + * + * @param string $message + * + * @return array + */ + public function transformImage($message) + { + return [ + 'image' => [ + 'media_id' => $message, + ], + 'msgtype' => 'image', + ]; + } + + /** + * Transform video message. + * + * @param array $message + * + * @return array + * + * @throws InvalidArgumentException + */ + public function transformVideo(array $message) + { + if (3 != count($message)) { + throw new InvalidArgumentException('send message to openids, the message must be three arguments.'); + } + + return [ + 'video' => [ + 'media_id' => $message[0], + 'title' => $message[1], + 'description' => $message[2], + ], + 'msgtype' => 'video', + ]; + } + + /** + * Transform mpvideo message. + * + * @param string $message + * + * @return array + */ + public function transformMpvideo($message) + { + return [ + 'mpvideo' => [ + 'media_id' => $message, + ], + 'msgtype' => 'mpvideo', + ]; + } + + /** + * Transform voice message. + * + * @param string $message + * + * @return array + */ + public function transformVoice($message) + { + return [ + 'voice' => [ + 'media_id' => $message, + ], + 'msgtype' => 'voice', + ]; + } + + /** + * Transform card message. + * + * @param $message + * + * @return array + */ + public function transformCard($message) + { + return [ + 'wxcard' => [ + 'card_id' => $message, + ], + 'msgtype' => 'wxcard', + ]; + } +} diff --git a/src/Broadcast/composer.json b/src/Broadcast/composer.json new file mode 100755 index 000000000..422ebc425 --- /dev/null +++ b/src/Broadcast/composer.json @@ -0,0 +1,32 @@ +{ + "name": "easywechat/broadcast", + "description": "broadcast module for EasyWeChat SDK.", + "keywords": [ + "wechat", + "weixin", + "SDK", + "broadcast", + "easywechat" + ], + "license": "MIT", + "authors": [ + { + "name": "overtrue", + "email": "anzhengchao@gmail.com" + } + ], + "autoload": { + "psr-4": { + "EasyWeChat\\Broadcast\\": "src/" + } + }, + "minimum-stability": "dev", + "require-dev": { + "phpunit/phpunit": "4.8.*", + "mockery/mockery": "^1.0@dev" + }, + "require": { + "easywechat/core": "dev-master", + "easywechat/message": "dev-master" + } +} diff --git a/src/Foundation/Application.php b/src/Foundation/Application.php old mode 100644 new mode 100755 index 2347729e2..27f8de2c2 --- a/src/Foundation/Application.php +++ b/src/Foundation/Application.php @@ -61,6 +61,7 @@ class Application extends Container ServiceProviders\StatsServiceProvider::class, ServiceProviders\PaymentServiceProvider::class, ServiceProviders\POIServiceProvider::class, + ServiceProviders\BroadcastServiceProvider::class, ]; /** @@ -123,6 +124,8 @@ public function getExceptionHandler() * Add a provider. * * @param string $provider + * + * @return Application */ public function addProvider($provider) { diff --git a/src/Foundation/ServiceProviders/BroadcastServiceProvider.php b/src/Foundation/ServiceProviders/BroadcastServiceProvider.php new file mode 100755 index 000000000..6c57ad3c8 --- /dev/null +++ b/src/Foundation/ServiceProviders/BroadcastServiceProvider.php @@ -0,0 +1,48 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * BroadcastServiceProvider.php. + * + * This file is part of the wechat. + * + * (c) overtrue + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Foundation\ServiceProviders; + +use EasyWeChat\Broadcast\Broadcast; +use Pimple\Container; +use Pimple\ServiceProviderInterface; + +/** + * Class BroadcastServiceProvider. + */ +class BroadcastServiceProvider implements ServiceProviderInterface +{ + /** + * Registers services on the given container. + * + * This method should only be used to configure services and parameters. + * It should not get services. + * + * @param Container $pimple A container instance + */ + public function register(Container $pimple) + { + $pimple['broadcast'] = function ($pimple) { + return new Broadcast($pimple['access_token']); + }; + } +} diff --git a/tests/Broadcast/BroadcastBroadcastTest.php b/tests/Broadcast/BroadcastBroadcastTest.php new file mode 100755 index 000000000..47d0f9efc --- /dev/null +++ b/tests/Broadcast/BroadcastBroadcastTest.php @@ -0,0 +1,129 @@ + +* +* This source file is subject to the MIT license that is bundled +* with this source code in the file LICENSE. +*/ + +use EasyWeChat\Broadcast\Broadcast; + +class BroadcastBroadcastTest extends PHPUnit_Framework_TestCase +{ + public function getBroadcast() + { + $broadcast = Mockery::mock('EasyWeChat\Broadcast\Broadcast[parseJSON]', [Mockery::mock('EasyWeChat\Core\AccessToken')]); + $broadcast->shouldReceive('parseJSON')->andReturnUsing(function ($method, $params) { + return [ + 'api' => $params[0], + 'params' => empty($params[1]) ? null : $params[1], + 'quires' => empty($params[3]) ? null : $params[3], + ]; + }); + + return $broadcast; + } + + /** + * Test send(). + */ + public function testSend() + { + $broadcast = $this->getBroadcast(); + + $response = $broadcast->send(Broadcast::MSG_TYPE_TEXT, 'CONTENT'); + + $this->assertStringStartsWith(Broadcast::API_SEND_BY_GROUP, $response['api']); + $data = [ + 'filter' => [ + 'is_to_all' => true, + ], + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($data, $response['params']); + + $broadcast = $this->getBroadcast(); + + $response = $broadcast->send(Broadcast::MSG_TYPE_TEXT, 'CONTENT', ['OPENID1', 'OPENID2', 'OPENID3']); + + $this->assertStringStartsWith(Broadcast::API_SEND_BY_OPENID, $response['api']); + $data = [ + 'touser' => [ + 'OPENID1', + 'OPENID2', + 'OPENID3', + ], + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($data, $response['params']); + } + + /** + * Test preview(). + */ + public function testPreview() + { + $broadcast = $this->getBroadcast(); + + $response = $broadcast->preview(Broadcast::MSG_TYPE_TEXT, 'CONTENT', 'OPENID'); + + $this->assertStringStartsWith(Broadcast::API_PREVIEW, $response['api']); + $data = [ + Broadcast::PREVIEW_BY_OPENID => 'OPENID', + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($data, $response['params']); + + $broadcast = $this->getBroadcast(); + + $response = $broadcast->preview(Broadcast::MSG_TYPE_TEXT, 'CONTENT', 'WXH', Broadcast::PREVIEW_BY_WXH); + + $this->assertStringStartsWith(Broadcast::API_PREVIEW, $response['api']); + $data = [ + Broadcast::PREVIEW_BY_WXH => 'WXH', + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($data, $response['params']); + } + + /** + * Test delete(). + */ + public function testDelete() + { + $broadcast = $this->getBroadcast(); + + $response = $broadcast->delete('MSG_ID'); + + $this->assertStringStartsWith(Broadcast::API_DELETE, $response['api']); + $this->assertEquals('MSG_ID', $response['params']['msg_id']); + } + + /** + * Test status(). + */ + public function testStatus() + { + $broadcast = $this->getBroadcast(); + + $response = $broadcast->status('MSG_ID'); + + $this->assertStringStartsWith(Broadcast::API_GET, $response['api']); + $this->assertEquals('MSG_ID', $response['params']['msg_id']); + } +} diff --git a/tests/Broadcast/BroadcastMessageBuilderTest.php b/tests/Broadcast/BroadcastMessageBuilderTest.php new file mode 100755 index 000000000..eed2d38e0 --- /dev/null +++ b/tests/Broadcast/BroadcastMessageBuilderTest.php @@ -0,0 +1,150 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +use EasyWeChat\Broadcast\MessageBuilder; +use EasyWeChat\Broadcast\Broadcast; + +class BroadcastMessageBuilderTest extends PHPUnit_Framework_TestCase +{ + /** + * Test msgType(). + * + * @expectedException EasyWeChat\Core\Exceptions\InvalidArgumentException + */ + public function testMsgType() + { + $messageBuilder = new MessageBuilder(); + + $response = $messageBuilder->msgType(Broadcast::MSG_TYPE_TEXT); + + $this->assertEquals($messageBuilder, $response); + $this->assertEquals(Broadcast::MSG_TYPE_TEXT, $messageBuilder->msgType); + + // exception + $messageBuilder->msgType('link'); + } + + /** + * Test message(); + */ + public function testMessage() + { + $messageBuilder = new MessageBuilder(); + + $response = $messageBuilder->message('CONTENT'); + + $this->assertEquals($messageBuilder, $response); + $this->assertEquals('CONTENT', $messageBuilder->message); + } + + /** + * Test to(). + */ + public function testTo() + { + $messageBuilder = new MessageBuilder(); + + $response = $messageBuilder->to('GROUP'); + + $this->assertEquals($messageBuilder, $response); + $this->assertEquals('GROUP', $messageBuilder->to); + } + + /** + * Test build(). + * + * @expectedException EasyWeChat\Core\Exceptions\RuntimeException + */ + public function testBuild() + { + $messageBuilder = new MessageBuilder(); + $messageBuilder->msgType(Broadcast::MSG_TYPE_TEXT)->message('CONTENT'); + $message = [ + 'filter' => [ + 'is_to_all' => true, + ], + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($message, $messageBuilder->build()); + + $messageBuilder = new MessageBuilder(); + $messageBuilder->msgType(Broadcast::MSG_TYPE_VIDEO)->message('MEDIA_ID'); + $message = [ + 'filter' => [ + 'is_to_all' => true, + ], + 'mpvideo' => [ + 'media_id' => 'MEDIA_ID', + ], + 'msgtype' => 'mpvideo', + ]; + $this->assertEquals($message, $messageBuilder->build()); + + $messageBuilder = new MessageBuilder(); + $messageBuilder->msgType(Broadcast::MSG_TYPE_VIDEO)->message(['MEDIA_ID', 'TITLE', 'DESCRIPTION'])->to(['OPENID1', 'OPENID2', 'OPENID3']); + $message = [ + 'touser' => [ + 'OPENID1', + 'OPENID2', + 'OPENID3', + ], + 'video' => [ + 'media_id' => 'MEDIA_ID', + 'title' => 'TITLE', + 'description' => 'DESCRIPTION', + ], + 'msgtype' => 'video', + ]; + $this->assertEquals($message, $messageBuilder->build()); + + // exception + $messageBuilder = new MessageBuilder(); + $messageBuilder->build(); + } + + /** + * Test buildPreview(). + * + * @expectedException EasyWeChat\Core\Exceptions\RuntimeException + * @expectedException EasyWeChat\Core\Exceptions\InvalidArgumentException + */ + public function testBuildPreview() + { + $messageBuilder = new MessageBuilder(); + $messageBuilder->msgType(Broadcast::MSG_TYPE_TEXT)->message('CONTENT')->to('OPENID'); + $message = [ + Broadcast::PREVIEW_BY_OPENID => 'OPENID', + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($message, $messageBuilder->buildPreview(Broadcast::PREVIEW_BY_OPENID)); + + $messageBuilder = new MessageBuilder(); + $messageBuilder->msgType(Broadcast::MSG_TYPE_TEXT)->message('CONTENT')->to('WXH'); + $message = [ + Broadcast::PREVIEW_BY_WXH => 'WXH', + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + $this->assertEquals($message, $messageBuilder->buildPreview(Broadcast::PREVIEW_BY_WXH)); + + // exception + $messageBuilder = new MessageBuilder(); + $messageBuilder->build(); + } +} diff --git a/tests/Broadcast/BroadcastTransformerTest.php b/tests/Broadcast/BroadcastTransformerTest.php new file mode 100755 index 000000000..a1b7d383b --- /dev/null +++ b/tests/Broadcast/BroadcastTransformerTest.php @@ -0,0 +1,151 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +use EasyWeChat\Broadcast\Transformer; + +class BroadcastTransformerTest extends PHPUnit_Framework_TestCase +{ + /** + * Test transform(). + */ + public function testTransform() + { + $transformer = new Transformer('link', 'http://easywechat.org'); + + $this->assertEquals([], $transformer->transform()); + } + + /** + * Test transformText(). + */ + public function testTransformText() + { + $transformer = new Transformer('text', 'CONTENT'); + + $msg = [ + 'text' => [ + 'content' => 'CONTENT', + ], + 'msgtype' => 'text', + ]; + + $this->assertEquals($msg, $transformer->transform()); + } + + /** + * Test transformNews(). + */ + public function testTransformNews() + { + $transformer = new Transformer('news', 'MEDIA_ID'); + + $msg = [ + 'mpnews' => [ + 'media_id' => 'MEDIA_ID', + ], + 'msgtype' => 'mpnews', + ]; + + $this->assertEquals($msg, $transformer->transform()); + } + + /** + * Test transformImage(). + */ + public function testTransformImage() + { + $transformer = new Transformer('image', 'MEDIA_ID'); + + $msg = [ + 'image' => [ + 'media_id' => 'MEDIA_ID', + ], + 'msgtype' => 'image', + ]; + + $this->assertEquals($msg, $transformer->transform()); + } + + /** + * Test transformVideo(). + * + * @expectedException EasyWeChat\Core\Exceptions\InvalidArgumentException + */ + public function testTransformVideo() + { + $transformer = new Transformer('video', ['MEDIA_ID', 'TITLE', 'DESCRIPTION']); + + $msg = [ + 'video' => [ + 'media_id' => 'MEDIA_ID', + 'title' => 'TITLE', + 'description' => 'DESCRIPTION', + ], + 'msgtype' => 'video', + ]; + + $this->assertEquals($msg, $transformer->transform()); + + // exception + (new Transformer('video', ['MEDIA_ID', 'TITLE']))->transform(); + } + + /** + * Test transformMpvideo(). + */ + public function testTransformMpvideo() + { + $transformer = new Transformer('mpvideo', 'MEDIA_ID'); + + $msg = [ + 'mpvideo' => [ + 'media_id' => 'MEDIA_ID', + ], + 'msgtype' => 'mpvideo', + ]; + + $this->assertEquals($msg, $transformer->transform()); + } + + /** + * Test transformVoice(). + */ + public function testTransformVoice() + { + $transformer = new Transformer('voice', 'MEDIA_ID'); + + $msg = [ + 'voice' => [ + 'media_id' => 'MEDIA_ID', + ], + 'msgtype' => 'voice', + ]; + + $this->assertEquals($msg, $transformer->transform()); + } + + /** + * Test transformCard(). + */ + public function testTransformCard() + { + $transformer = new Transformer('card', 'CARD_ID'); + + $msg = [ + 'wxcard' => [ + 'card_id' => 'CARD_ID', + ], + 'msgtype' => 'wxcard', + ]; + + $this->assertEquals($msg, $transformer->transform()); + } +}