diff --git a/src/Kernel/Contracts/MediaInterface.php b/src/Kernel/Contracts/MediaInterface.php new file mode 100644 index 000000000..63768cff8 --- /dev/null +++ b/src/Kernel/Contracts/MediaInterface.php @@ -0,0 +1,25 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Kernel\Contracts; + +/** + * Interface MediaInterface. + * + * @author overtrue + */ +interface MediaInterface extends MessageInterface +{ + /** + * @return string + */ + public function getMediaId(): string; +} diff --git a/src/Kernel/Contracts/MessageInterface.php b/src/Kernel/Contracts/MessageInterface.php new file mode 100644 index 000000000..6c4e3ba42 --- /dev/null +++ b/src/Kernel/Contracts/MessageInterface.php @@ -0,0 +1,35 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Kernel\Contracts; + +/** + * Interface MessageInterface. + * + * @author overtrue + */ +interface MessageInterface +{ + /** + * @return string + */ + public function getType(): string; + + /** + * @return mixed + */ + public function transformForJsonRequest(): array; + + /** + * @return string + */ + public function transformToXml(): string; +} diff --git a/src/Kernel/Messages/Article.php b/src/Kernel/Messages/Article.php index 9ec292b40..aaac63967 100644 --- a/src/Kernel/Messages/Article.php +++ b/src/Kernel/Messages/Article.php @@ -16,6 +16,11 @@ */ class Article extends Message { + /** + * @var string + */ + protected $type = 'mpnews'; + /** * Properties. * @@ -36,8 +41,19 @@ class Article extends Message * * @var array */ - protected $aliases = [ - 'source_url' => 'content_source_url', - 'show_cover' => 'show_cover_pic', + protected $jsonAlias = [ + 'content_source_url' => 'source_url', + 'show_cover_pic' => 'show_cover', + ]; + + /** + * @var array + */ + protected $requirements = [ + 'thumb_media_id', + 'title', + 'content', + 'source_url', + 'show_cover', ]; } diff --git a/src/Kernel/Messages/Card.php b/src/Kernel/Messages/Card.php index ddf06eeb2..0e212312e 100644 --- a/src/Kernel/Messages/Card.php +++ b/src/Kernel/Messages/Card.php @@ -41,12 +41,12 @@ class Card extends Message protected $properties = ['card_id']; /** - * Material constructor. + * Media constructor. * * @param string $cardId */ - public function __construct($cardId) + public function __construct(string $cardId) { - $this->set('card_id', $cardId); + parent::__construct(['card_id' => $cardId]); } } diff --git a/src/Kernel/Messages/Factory.php b/src/Kernel/Messages/Factory.php new file mode 100644 index 000000000..6db9078f8 --- /dev/null +++ b/src/Kernel/Messages/Factory.php @@ -0,0 +1,36 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Kernel\Messages; + +/** + * Class Factory. + * + * @author overtrue + */ +class Factory +{ + /** + * @param array $attributes + */ + public static function createFromMedia($attributes = []) + { + //TODO: 从素材转换为消息对象 + } + + /** + * @param array $attributes + */ + public static function createFromUserMessage($attributes = []) + { + //TODO: 从请求消息解析为消息对象 + } +} diff --git a/src/Kernel/Messages/File.php b/src/Kernel/Messages/File.php index b07dd2809..b1d073685 100644 --- a/src/Kernel/Messages/File.php +++ b/src/Kernel/Messages/File.php @@ -16,7 +16,7 @@ * * @property string $media_id */ -class File extends Message +class File extends Media { /** * Messages type. @@ -33,18 +33,4 @@ class File extends Message protected $properties = [ 'media_id', ]; - - /** - * Set media_id. - * - * @param string $mediaId - * - * @return \EasyWeChat\Kernel\Messages\File - */ - public function file($mediaId) - { - $this->setAttribute('media_id', $mediaId); - - return $this; - } } diff --git a/src/Kernel/Messages/Image.php b/src/Kernel/Messages/Image.php index 2ff9a35ef..a3761ae18 100644 --- a/src/Kernel/Messages/Image.php +++ b/src/Kernel/Messages/Image.php @@ -16,7 +16,7 @@ * * @property string $media_id */ -class Image extends Message +class Image extends Media { /** * Messages type. @@ -31,18 +31,4 @@ class Image extends Message * @var array */ protected $properties = ['media_id']; - - /** - * Set media_id. - * - * @param string $mediaId - * - * @return Image - */ - public function media($mediaId) - { - $this->setAttribute('media_id', $mediaId); - - return $this; - } } diff --git a/src/Kernel/Messages/Material.php b/src/Kernel/Messages/Material.php deleted file mode 100644 index 5c38b0b85..000000000 --- a/src/Kernel/Messages/Material.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace EasyWeChat\Kernel\Messages; - -/** - * Class MaterialClient. - */ -class Material extends Message -{ - /** - * Properties. - * - * @var array - */ - protected $properties = ['media_id']; - - /** - * MaterialClient constructor. - * - * @param string $mediaId - * @param string $type - */ - public function __construct($type, $mediaId) - { - $this->set('media_id', $mediaId); - $this->setType($type); - } -} diff --git a/src/Kernel/Messages/Media.php b/src/Kernel/Messages/Media.php new file mode 100644 index 000000000..b306f4b39 --- /dev/null +++ b/src/Kernel/Messages/Media.php @@ -0,0 +1,58 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Kernel\Messages; + +use EasyWeChat\Kernel\Contracts\MediaInterface; + +/** + * Class Media. + */ +class Media extends Message implements MediaInterface +{ + /** + * Properties. + * + * @var array + */ + protected $properties = ['media_id']; + + /** + * @var array + */ + protected $requirements = [ + 'media_id', + ]; + + /** + * MaterialClient constructor. + * + * @param string $type + * @param string $mediaId + * @param array $attributes + */ + public function __construct(string $type, string $mediaId, array $attributes = []) + { + parent::__construct(array_merge(['media_id' => $mediaId], $attributes)); + + $this->setType($type); + } + + /** + * @return string + */ + public function getMediaId(): string + { + $this->checkRequiredAttributes(); + + return $this->get('media_id'); + } +} diff --git a/src/Kernel/Messages/Message.php b/src/Kernel/Messages/Message.php index d7b29dee4..bc24bdb2c 100644 --- a/src/Kernel/Messages/Message.php +++ b/src/Kernel/Messages/Message.php @@ -11,12 +11,14 @@ namespace EasyWeChat\Kernel\Messages; +use EasyWeChat\Kernel\Contracts\MessageInterface; +use EasyWeChat\Kernel\Support\XML; use EasyWeChat\Kernel\Traits\HasAttributes; /** * Class Messages. */ -abstract class Message +abstract class Message implements MessageInterface { use HasAttributes; @@ -31,44 +33,45 @@ abstract class Message const DEVICE_TEXT = 512; const FILE = 1024; const TEXT_CARD = 2048; + const TRANSFER = 4096; const EVENT = 1048576; const ALL = 1049598; /** - * Messages type. - * * @var string */ protected $type; /** - * Messages id. - * * @var int */ protected $id; /** - * Messages target user open id. - * * @var string */ protected $to; /** - * Messages sender open id. - * * @var string */ protected $from; /** - * Messages attributes. - * * @var array */ protected $properties = []; + /** + * @var array + */ + protected $jsonAlias = []; + + /** + * @var array + */ + protected $xmlAlias = []; + /** * Message constructor. * @@ -84,7 +87,7 @@ public function __construct(array $attributes = []) * * @return string */ - public function getType() + public function getType(): string { return $this->type; } @@ -131,4 +134,55 @@ public function __set($property, $value) return $this; } + + /** + * @param array $appends + * + * @return array + */ + public function transformForJsonRequest(array $appends = []): array + { + $data = array_merge(['msgtype' => $this->getType()], $appends); + + $data = $this->propertiesToArray($data, $this->jsonAlias); + + return $data; + } + + /** + * @param array $appends + * @param bool $returnAsArray + * + * @return string + */ + public function transformToXml(array $appends = [], bool $returnAsArray = false): string + { + $data = array_merge(['MsgType' => $this->getType()], $appends); + + $data = $this->propertiesToArray($data, $this->xmlAlias); + + return $returnAsArray ? $data : XML::build($data); + } + + /** + * @param array $data + * @param array $aliases + * + * @return array|mixed + */ + protected function propertiesToArray(array $data, array $aliases = []): array + { + $this->checkRequiredAttributes(); + + foreach ($this->attributes as $property => $value) { + if (is_null($value) && !$this->isRequired($property)) { + continue; + } + $alias = array_search($property, $aliases, true); + + $data[$alias ?: $property] = $this->get($property); + } + + return $data; + } } diff --git a/src/Kernel/Messages/Music.php b/src/Kernel/Messages/Music.php index e53317060..277a5d9be 100644 --- a/src/Kernel/Messages/Music.php +++ b/src/Kernel/Messages/Music.php @@ -43,18 +43,4 @@ class Music extends Message 'thumb_media_id', 'format', ]; - - /** - * 设置音乐封面. - * - * @param string $mediaId - * - * @return $this - */ - public function thumb($mediaId) - { - $this->setAttribute('thumb_media_id', $mediaId); - - return $this; - } } diff --git a/src/Kernel/Messages/News.php b/src/Kernel/Messages/News.php index 9f5ee7463..0ee6f4553 100644 --- a/src/Kernel/Messages/News.php +++ b/src/Kernel/Messages/News.php @@ -40,7 +40,7 @@ class News extends Message * * @var array */ - protected $aliases = [ - 'image' => 'pic_url', + protected $jsonAlias = [ + 'pic_url' => 'image', ]; } diff --git a/src/Kernel/Messages/README.md b/src/Kernel/Messages/README.md deleted file mode 100644 index 88765ae16..000000000 --- a/src/Kernel/Messages/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# message - -message diff --git a/src/Kernel/Messages/Raw.php b/src/Kernel/Messages/Raw.php index effafdd61..f89078327 100644 --- a/src/Kernel/Messages/Raw.php +++ b/src/Kernel/Messages/Raw.php @@ -33,7 +33,7 @@ class Raw extends Message * * @param string $content */ - public function __construct($content) + public function __construct(string $content) { parent::__construct(['content' => strval($content)]); } diff --git a/src/Kernel/Messages/Video.php b/src/Kernel/Messages/Video.php index 327b98e6e..0ec0b6047 100644 --- a/src/Kernel/Messages/Video.php +++ b/src/Kernel/Messages/Video.php @@ -20,7 +20,7 @@ * @property string $description * @property string $thumb_media_id */ -class Video extends Message +class Video extends Media { /** * Messages type. @@ -40,32 +40,4 @@ class Video extends Message 'media_id', 'thumb_media_id', ]; - - /** - * 设置视频消息. - * - * @param string $mediaId - * - * @return Video - */ - public function media($mediaId) - { - $this->setAttribute('media_id', $mediaId); - - return $this; - } - - /** - * 设置视频封面. - * - * @param string $mediaId - * - * @return Video - */ - public function thumb($mediaId) - { - $this->setAttribute('thumb_media_id', $mediaId); - - return $this; - } } diff --git a/src/Kernel/Messages/Voice.php b/src/Kernel/Messages/Voice.php index fc826f650..3ef3b6e39 100644 --- a/src/Kernel/Messages/Voice.php +++ b/src/Kernel/Messages/Voice.php @@ -34,18 +34,4 @@ class Voice extends Message 'media_id', 'recognition', ]; - - /** - * Set media id. - * - * @param string $mediaId - * - * @return Voice - */ - public function media($mediaId) - { - $this->setAttribute('media_id', $mediaId); - - return $this; - } } diff --git a/src/Kernel/Messages/composer.json b/src/Kernel/Messages/composer.json deleted file mode 100644 index b7f609d2e..000000000 --- a/src/Kernel/Messages/composer.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "easywechat/message", - "description": "message module for EasyWeChat SDK.", - "keywords": ["wechat", "weixin", "SDK", "message", "easywechat"], - "license": "MIT", - "authors": [ - { - "name": "overtrue", - "email": "anzhengchao@gmail.com" - } - ], - "autoload": { - "psr-4": { - "EasyWeChat\\Message\\": "." - } - }, - "minimum-stability":"dev", - "require-dev": { - "phpunit/phpunit": "~4.0", - "mockery/mockery": "^1.0@dev" - }, - "require": { - "easywechat/support": "dev-master" - } -} diff --git a/src/Kernel/Traits/HasAttributes.php b/src/Kernel/Traits/HasAttributes.php index 3f172d6fc..534f5b41f 100644 --- a/src/Kernel/Traits/HasAttributes.php +++ b/src/Kernel/Traits/HasAttributes.php @@ -76,11 +76,21 @@ public function setAttribute($attribute, $value) * * @return mixed */ - public function getAttribute($attribute, $default) + public function getAttribute($attribute, $default = null) { return $this->attributes->get($attribute, $default); } + /** + * @param string $attribute + * + * @return bool + */ + public function isRequired($attribute) + { + return in_array($attribute, $this->requirements); + } + /** * Set attribute. * @@ -125,7 +135,7 @@ protected function validate($attribute, $value) * * @return $this */ - public function set($attribute, $value = null) + public function set($attribute, $value) { $this->setAttribute($this->getRealKey($attribute), $value); @@ -235,8 +245,8 @@ protected function getRealKey($key) protected function checkRequiredAttributes() { foreach ($this->requirements as $attribute) { - if (!isset($this->attributes, $attribute)) { - throw new InvalidArgumentException("'{$attribute}' cannot be empty."); + if (is_null($this->get($attribute))) { + throw new InvalidArgumentException(sprintf('"%s" cannot be empty.', $attribute)); } } } diff --git a/src/OfficialAccount/Broadcasting/MessageTransformer.php b/src/OfficialAccount/Broadcasting/MessageTransformer.php index 24c7d144f..c35ceff8b 100644 --- a/src/OfficialAccount/Broadcasting/MessageTransformer.php +++ b/src/OfficialAccount/Broadcasting/MessageTransformer.php @@ -11,178 +11,142 @@ namespace EasyWeChat\OfficialAccount\Broadcasting; -use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; +use EasyWeChat\Kernel\Messages\Message; +use EasyWeChat\Kernel\Messages\News; +use EasyWeChat\Kernel\Messages\Text; /** * Class MessageTransformer. * * @author overtrue */ -class MessageTransformer +class Transformer { /** - * Messages type. + * transform message to XML. * - * @var string - */ - protected $msgType; - - /** - * message. - * - * @var mixed - */ - protected $message; - - /** - * MessageTransformer constructor. - * - * @param $msgType - * @param $message - */ - public function __construct($msgType, $message) - { - $this->msgType = $msgType; - - $this->message = $message; - } - - /** - * Transform message. + * @param array|string|Message $message * * @return array */ - public function transform() + public function transform($message) { - $handle = sprintf('transform%s', ucfirst($this->msgType)); + if (is_array($message)) { + $class = News::class; + } else { + if (is_string($message)) { + $message = new Text(['content' => $message]); + } + + $class = get_class($message); + } + + $handle = 'transform'.substr($class, strlen('EasyWeChat\Kernel\Messages\\')); - return method_exists($this, $handle) ? $this->$handle($this->message) : []; + return method_exists($this, $handle) ? $this->$handle($message) : []; } /** * Transform text message. * - * @param string $message + * @param Message $message * * @return array */ - public function transformText($message) + public function transformText(Message $message) { return [ - 'text' => [ - 'content' => $message, - ], 'msgtype' => 'text', - ]; - } - - /** - * Transform news message. - * - * @param string $message - * - * @return array - */ - public function transformNews($message) - { - return [ - 'mpnews' => [ - 'media_id' => $message, + 'text' => [ + 'content' => $message->get('content'), ], - 'msgtype' => 'mpnews', ]; } /** * Transform image message. * - * @param string $message + * @param Message $message * * @return array */ - public function transformImage($message) + public function transformImage(Message $message) { return [ + 'msgtype' => 'image', 'image' => [ - 'media_id' => $message, + 'media_id' => $message->get('media_id'), ], - 'msgtype' => 'image', ]; } /** * Transform video message. * - * @param array $message + * @param Message $message * * @return array - * - * @throws InvalidArgumentException */ - public function transformVideo(array $message) + public function transformVideo(Message $message) { - if (3 !== count($message)) { - throw new InvalidArgumentException('send message to openids, the message must be three arguments.'); - } - return [ + 'msgtype' => 'mpvideo', 'mpvideo' => [ - 'media_id' => $message[0], - 'title' => $message[1], - 'description' => $message[2], + 'media_id' => $message->get('media_id'), + 'title' => $message->get('title'), + 'description' => $message->get('description'), ], - 'msgtype' => 'mpvideo', ]; } /** - * Transform mpvideo message. + * Transform voice message. * - * @param string $message + * @param Message $message * * @return array */ - public function transformMpvideo($message) + public function transformVoice(Message $message) { return [ - 'mpvideo' => [ - 'media_id' => $message, + 'msgtype' => 'voice', + 'voice' => [ + 'media_id' => $message->get('media_id'), ], - 'msgtype' => 'mpvideo', ]; } /** - * Transform voice message. + * Transform articles message. * - * @param string $message + * @param \EasyWeChat\Kernel\Messages\Message $message * * @return array */ - public function transformVoice($message) + public function transformNews(Message $message) { return [ - 'voice' => [ - 'media_id' => $message, + 'msgtype' => 'mpnews', + 'mpnews' => [ + 'media_id' => $message->get('media_id'), ], - 'msgtype' => 'voice', ]; } /** * Transform card message. * - * @param $message + * @param Message $message * * @return array */ - public function transformCard($message) + public function transformCard(Message $message) { return [ + 'msgtype' => 'wxcard', 'wxcard' => [ - 'card_id' => $message, + 'card_id' => $message->get('card_id'), ], - 'msgtype' => 'wxcard', ]; } } diff --git a/src/OfficialAccount/Broadcasting/Messenger.php b/src/OfficialAccount/Broadcasting/Messenger.php index 7ee55e4ef..0940e32be 100644 --- a/src/OfficialAccount/Broadcasting/Messenger.php +++ b/src/OfficialAccount/Broadcasting/Messenger.php @@ -13,6 +13,7 @@ use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Exceptions\RuntimeException; +use EasyWeChat\Kernel\Messages\Message; /** * Class Messenger. @@ -28,72 +29,21 @@ class Messenger */ protected $to; - /** - * Messages type. - * - * @var string - */ - protected $msgType; - /** * Messages. * - * @var mixed + * @var Message */ protected $message; - /** - * Messages types. - * - * @var array - */ - private $msgTypes = [ - Client::MSG_TYPE_TEXT, - Client::MSG_TYPE_NEWS, - Client::MSG_TYPE_IMAGE, - Client::MSG_TYPE_VIDEO, - Client::MSG_TYPE_VOICE, - Client::MSG_TYPE_CARD, - ]; - - /** - * Preview bys. - * - * @var array - */ - private $previewBys = [ - Client::PREVIEW_BY_OPENID, - Client::PREVIEW_BY_NAME, - ]; - - /** - * Set message type. - * - * @param string $msgType - * - * @return Messenger - * - * @throws InvalidArgumentException - */ - public function msgType($msgType) - { - if (!in_array($msgType, $this->msgTypes, true)) { - throw new InvalidArgumentException('This message type not exist.'); - } - - $this->msgType = $msgType; - - return $this; - } - /** * Set message. * - * @param string|array $message + * @param array|\EasyWeChat\Kernel\Messages\Message $message * - * @return Messenger + * @return \EasyWeChat\OfficialAccount\Broadcasting\Messenger */ - public function message($message) + public function message(Message $message) { $this->message = $message; @@ -131,15 +81,7 @@ public function build() throw new RuntimeException('No message content to send.'); } - // 群发视频消息给用户列表时,视频消息格式需要另外处理,具体见文档 - if ($this->msgType === Client::MSG_TYPE_VIDEO) { - if (is_array($this->message)) { - $this->message = array_shift($this->message); - } - $this->msgType = 'mpvideo'; - } - - $content = (new MessageTransformer($this->msgType, $this->message))->transform(); + $content = (new MessageTransformer($this->message))->transform(); $group = isset($this->to) ? $this->to : null; @@ -148,6 +90,22 @@ public function build() return $message; } + /** + * @return array + */ + public function previewByOpenId() + { + return $this->buildPreview(Client::PREVIEW_BY_OPENID); + } + + /** + * @return array + */ + public function previewByName() + { + return $this->buildPreview(Client::PREVIEW_BY_NAME); + } + /** * Build preview message. * @@ -158,21 +116,8 @@ public function build() * @throws RuntimeException * @throws InvalidArgumentException */ - public function buildPreview($by) + protected function buildPreview($by) { - if (!in_array($by, $this->previewBys, true)) { - throw new InvalidArgumentException('This preview by not exist.'); - } - - if (empty($this->msgType)) { - throw new RuntimeException('Message type not exist.'); - } elseif ($this->msgType === Client::MSG_TYPE_VIDEO) { - if (is_array($this->message)) { - $this->message = array_shift($this->message); - } - $this->msgType = 'mpvideo'; - } - if (empty($this->message)) { throw new RuntimeException('No message content to send.'); } @@ -181,7 +126,7 @@ public function buildPreview($by) throw new RuntimeException('No to.'); } - $content = (new MessageTransformer($this->msgType, $this->message))->transform(); + $content = (new MessageTransformer($this->message))->transform(); $message = array_merge($this->buildTo($this->to, $by), $content); diff --git a/src/Payment/Client.php b/src/Payment/Client.php index 9e370ba21..01a7b57df 100644 --- a/src/Payment/Client.php +++ b/src/Payment/Client.php @@ -13,7 +13,6 @@ use EasyWeChat\Kernel\Support; use EasyWeChat\Payment\Traits\HandleNotify; -use EasyWeChat\Payment\Traits\JssdkHelpers; use EasyWeChat\Payment\Traits\WorksInSandbox; /** @@ -23,7 +22,7 @@ */ class Client extends BaseClient { - use WorksInSandbox, JssdkHelpers, HandleNotify; + use WorksInSandbox, HandleNotify; // order id types. const TRANSACTION_ID = 'transaction_id'; diff --git a/tests/Kernel/EncryptorTest.php b/tests/Kernel/EncryptorTest.php index 2532dc860..185584366 100644 --- a/tests/Kernel/EncryptorTest.php +++ b/tests/Kernel/EncryptorTest.php @@ -52,10 +52,10 @@ public function testDecrypt() { $encrypted = "\n \n \n\n"; $decrypted = $this->getEncryptor()->decrypt('4f3ad57b6989f09f4eb392acce4f9e93942ed890', '260774613', '1458300676', $encrypted); - $this->assertEquals('asdasdasd', $decrypted['ToUserName']); - $this->assertEquals('asdasdasdsadasd', $decrypted['FromUserName']); - $this->assertEquals('1234567898', $decrypted['CreateTime']); - $this->assertEquals('hello', $decrypted['Content']); + $this->assertSame('asdasdasd', $decrypted['ToUserName']); + $this->assertSame('asdasdasdsadasd', $decrypted['FromUserName']); + $this->assertSame('1234567898', $decrypted['CreateTime']); + $this->assertSame('hello', $decrypted['Content']); } public function testEncryptAndDecrypt() @@ -73,11 +73,11 @@ public function testEncryptAndDecrypt() $xml = XML::build($raw); $encrypted = $this->getEncryptor()->encrypt($xml, 'xxxxxx', '1407743423'); $array = XML::parse($encrypted); - $this->assertEquals('1407743423', $array['TimeStamp']); - $this->assertEquals('xxxxxx', $array['Nonce']); + $this->assertSame('1407743423', $array['TimeStamp']); + $this->assertSame('xxxxxx', $array['Nonce']); $this->assertNotEmpty($array['Encrypt']); $this->assertNotEmpty($array['MsgSignature']); - $this->assertEquals($raw, $this->getEncryptor()->decrypt($array['MsgSignature'], $array['Nonce'], $array['TimeStamp'], $encrypted)); + $this->assertSame($raw, $this->getEncryptor()->decrypt($array['MsgSignature'], $array['Nonce'], $array['TimeStamp'], $encrypted)); } public function testEncryptException() diff --git a/tests/Kernel/Messages/ArticleTest.php b/tests/Kernel/Messages/ArticleTest.php new file mode 100644 index 000000000..2f99197e5 --- /dev/null +++ b/tests/Kernel/Messages/ArticleTest.php @@ -0,0 +1,44 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Tests\Kernel\Messages; + +use EasyWeChat\Kernel\Messages\Article; +use EasyWeChat\Tests\TestCase; + +class ArticleTest extends TestCase +{ + public function testGetMediaId() + { + $article = new Article( + [ + 'thumb_media_id' => 'mock-thumb_media_id', + 'author' => 'mock-author', + 'title' => 'mock-title', + 'content' => 'mock-content', + 'digest' => 'mock-digest', + 'source_url' => 'mock-source_url', + 'show_cover' => 'mock-show_cover', + ] + ); + $this->assertSame('mpnews', $article->getType()); + $this->assertSame([ + 'msgtype' => 'mpnews', + 'thumb_media_id' => 'mock-thumb_media_id', + 'author' => 'mock-author', + 'title' => 'mock-title', + 'content' => 'mock-content', + 'digest' => 'mock-digest', + 'content_source_url' => 'mock-source_url', + 'show_cover_pic' => 'mock-show_cover', + ], $article->transformForJsonRequest()); + } +} diff --git a/tests/Kernel/Messages/CardTest.php b/tests/Kernel/Messages/CardTest.php new file mode 100644 index 000000000..3eee37b46 --- /dev/null +++ b/tests/Kernel/Messages/CardTest.php @@ -0,0 +1,25 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Test\Kernel\Messages; + +use EasyWeChat\Kernel\Messages\Card; +use EasyWeChat\Tests\TestCase; + +class CardTest extends TestCase +{ + public function testBasicFeatures() + { + $card = new Card('mock-card-id'); + + $this->assertSame('mock-card-id', $card->card_id); + } +} diff --git a/tests/Kernel/Messages/FactoryTest.php b/tests/Kernel/Messages/FactoryTest.php new file mode 100644 index 000000000..20e49b343 --- /dev/null +++ b/tests/Kernel/Messages/FactoryTest.php @@ -0,0 +1,27 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Tests\Kernel\Messages; + +use EasyWeChat\Tests\TestCase; + +class FactoryTest extends TestCase +{ + public function testCreateFromMedia() + { + $this->markTestIncomplete(); + } + + public function testCreateFromUserMessage() + { + $this->markTestIncomplete(); + } +} diff --git a/tests/Kernel/Messages/MediaTest.php b/tests/Kernel/Messages/MediaTest.php new file mode 100644 index 000000000..a28116349 --- /dev/null +++ b/tests/Kernel/Messages/MediaTest.php @@ -0,0 +1,28 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Tests\Kernel\Messages; + +use EasyWeChat\Kernel\Contracts\MediaInterface; +use EasyWeChat\Kernel\Messages\Media; +use EasyWeChat\Tests\TestCase; + +class MediaTest extends TestCase +{ + public function testGetMediaId() + { + $media = new Media('image', 'mock-media-id', ['title' => 'mock-title']); + $this->assertInstanceOf(MediaInterface::class, $media); + $this->assertSame('image', $media->getType()); + $this->assertSame('mock-media-id', $media->getMediaId()); + $this->assertSame('mock-title', $media->title); + } +} diff --git a/tests/Kernel/Messages/MessageTest.php b/tests/Kernel/Messages/MessageTest.php new file mode 100644 index 000000000..ff97e0591 --- /dev/null +++ b/tests/Kernel/Messages/MessageTest.php @@ -0,0 +1,164 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Tests\Kernel\Messages; + +use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; +use EasyWeChat\Kernel\Messages\Message; +use EasyWeChat\Kernel\Support\XML; +use EasyWeChat\Tests\TestCase; + +class MessageTest extends TestCase +{ + public function testBasicFeatures() + { + $message = new DummyMessageForMessageTest([ + 'media_id' => '12345', + ]); + $this->assertSame('dummy', $message->getType()); + $this->assertSame('12345', $message->media_id); + $this->assertSame('12345', $message->get('media_id')); + $this->assertSame('12345', $message->getAttribute('media_id')); + + $this->assertEmpty($message->to); + + // type + $message->setType('new-type'); + $this->assertSame('new-type', $message->getType()); + + // setter + $message->to = ['touser' => 'mock-openid']; + $this->assertSame(['touser' => 'mock-openid'], $message->to); + + // attributes + $message->new_attribute = 'mock-content'; + $this->assertSame('mock-content', $message->new_attribute); + } + + public function testTransformForJsonRequest() + { + // required + $message = new DummyMessageForMessageTest([ + 'media_id' => '12345', + 'foo' => 'f', + 'bar' => 'b', + 'required_id' => 'r', + 'title' => null, + ]); + + $this->assertSame([ + 'msgtype' => 'dummy', + 'append_id' => 'ap_id', + 'media_id' => '12345', + 'foo_id' => 'f', + 'bar_name' => 'b', + 'required_id' => 'r', + ], $message->transformForJsonRequest(['append_id' => 'ap_id'])); + + // optional + $message = new DummyMessageForMessageTest([ + 'media_id' => '12345', + 'title' => 'the title', + 'foo' => 'f', + 'bar' => 'b', + 'required_id' => 'r', + ]); + + $this->assertSame([ + 'msgtype' => 'dummy', + 'append_id' => 'ap_id', + 'media_id' => '12345', + 'title' => 'the title', + 'foo_id' => 'f', + 'bar_name' => 'b', + 'required_id' => 'r', + ], $message->transformForJsonRequest(['append_id' => 'ap_id'])); + } + + public function testTransformToXml() + { + // required + $message = new DummyMessageForMessageTest([ + 'media_id' => '12345', + 'foo' => 'f', + 'bar' => 'b', + 'required_id' => 'r', + 'title' => null, + ]); + + $this->assertSame(XML::build([ + 'MsgType' => 'dummy', + 'append_id' => 'ap_id', + 'media_id' => '12345', + 'Foo' => 'f', + 'Bar' => 'b', + 'required_id' => 'r', + ]), $message->transformToXml(['append_id' => 'ap_id'])); + + // optional + $message = new DummyMessageForMessageTest([ + 'media_id' => '12345', + 'title' => 'the title', + 'foo' => 'f', + 'bar' => 'b', + 'required_id' => 'r', + ]); + + $this->assertSame(XML::build([ + 'MsgType' => 'dummy', + 'append_id' => 'ap_id', + 'media_id' => '12345', + 'title' => 'the title', + 'Foo' => 'f', + 'Bar' => 'b', + 'required_id' => 'r', + ]), $message->transformToXml(['append_id' => 'ap_id'])); + } + + public function testMissingRequiredKey() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('"required_id" cannot be empty.'); + $message = new DummyMessageForMessageTest([ + 'media_id' => '12345', + 'foo' => 'f', + 'bar' => 'b', + ]); + $message->transformForJsonRequest(); + } +} + +class DummyMessageForMessageTest extends Message +{ + protected $type = 'dummy'; + + protected $properties = [ + 'foo', + 'bar', + 'media_id', + 'title', + 'required_id', + ]; + + protected $jsonAlias = [ + 'foo_id' => 'foo', + 'bar_name' => 'bar', + ]; + + protected $xmlAlias = [ + 'Foo' => 'foo', + 'Bar' => 'bar', + ]; + + protected $requirements = [ + 'media_id', 'required_id', + ]; +} diff --git a/tests/Kernel/Messages/RawTest.php b/tests/Kernel/Messages/RawTest.php new file mode 100644 index 000000000..f1369a703 --- /dev/null +++ b/tests/Kernel/Messages/RawTest.php @@ -0,0 +1,26 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Tests\Kernel\Messages; + +use EasyWeChat\Kernel\Messages\Raw; +use EasyWeChat\Tests\TestCase; + +class RawTest extends TestCase +{ + public function testBasicFeatures() + { + $content = 'foo'; + $raw = new Raw($content); + + $this->assertSame($content, $raw->content); + } +}