From ce94dca5d8ee81eb8d0e25dd4072f99cfeb1af2d Mon Sep 17 00:00:00 2001 From: wangju <35649084@qq.com> Date: Fri, 17 May 2019 21:52:22 +0800 Subject: [PATCH] test message send with mockery --- composer.json | 3 +- src/DingTalk.php | 10 ++- src/DingTalkService.php | 108 ++++++++++++++++----------------- src/HttpClient.php | 82 +++++++++++++++++++++++++ src/SendClient.php | 15 +++++ tests/Feature/ActionTest.php | 23 ++++++- tests/Feature/FeedTest.php | 29 ++++++++- tests/Feature/LinkTest.php | 18 +++++- tests/Feature/MarkdownTest.php | 23 ++++++- tests/Feature/TextTest.php | 23 ++++++- tests/TestCase.php | 47 +++++++++++--- 11 files changed, 298 insertions(+), 83 deletions(-) create mode 100644 src/HttpClient.php create mode 100644 src/SendClient.php diff --git a/composer.json b/composer.json index a313be7..c1bbc45 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "guzzlehttp/guzzle": "^6.2" }, "require-dev": { - "phpunit/phpunit": "^5.7" + "phpunit/phpunit": "^5.7", + "mockery/mockery": "^1.2" }, "autoload": { "psr-4": { diff --git a/src/DingTalk.php b/src/DingTalk.php index 2ff162f..6072243 100644 --- a/src/DingTalk.php +++ b/src/DingTalk.php @@ -18,13 +18,17 @@ class DingTalk */ protected $dingTalkService; + protected $client; + /** * DingTalk constructor. * @param $config + * @param SendClient $client */ - public function __construct($config) + public function __construct($config,$client = null) { $this->config = $config; + $this->client = $client; $this->with(); } @@ -34,7 +38,7 @@ public function __construct($config) */ public function with($robot = 'default'){ $this->robot = $robot; - $this->dingTalkService = new DingTalkService($this->config[$robot]); + $this->dingTalkService = new DingTalkService($this->config[$robot],$this->client); return $this; } @@ -114,4 +118,4 @@ public function feed(){ ->setFeedCardMessage(); } -} \ No newline at end of file +} diff --git a/src/DingTalkService.php b/src/DingTalkService.php index d552298..c825bda 100644 --- a/src/DingTalkService.php +++ b/src/DingTalkService.php @@ -1,6 +1,7 @@ config = $config; $this->setTextMessage('null'); - $this->setAccessToken(); + + if ($client != null) { + $this->client = $client; + return; + } + $this->client = $this->createClient($config); + } /** @@ -56,7 +62,8 @@ public function setMessage($message) /** * @return array */ - public function getMessage(){ + public function getMessage() + { return $this->message->getMessage(); } @@ -64,27 +71,24 @@ public function getMessage(){ * @param array $mobiles * @param bool $atAll */ - public function setAt($mobiles = [], $atAll = false){ + public function setAt($mobiles = [], $atAll = false) + { $this->mobiles = $mobiles; $this->atAll = $atAll; - if ($this->message){ - $this->message->sendAt($mobiles,$atAll); + if ($this->message) { + $this->message->sendAt($mobiles, $atAll); } } - - /** - * - */ - public function setAccessToken(){ - $this->accessToken = $this->config['token']; - } - /** - * @return string + * create a guzzle client + * @return HttpClient + * @author wangju 2019-05-17 20:25 */ - public function getRobotUrl(){ - return $this->hookUrl . "?access_token={$this->accessToken}"; + protected function createClient($config) + { + $client = new HttpClient($config); + return $client; } @@ -92,9 +96,10 @@ public function getRobotUrl(){ * @param $content * @return $this */ - public function setTextMessage($content){ + public function setTextMessage($content) + { $this->message = new Text($content); - $this->message->sendAt($this->mobiles,$this->atAll); + $this->message->sendAt($this->mobiles, $this->atAll); return $this; } @@ -105,9 +110,10 @@ public function setTextMessage($content){ * @param string $picUrl * @return $this */ - public function setLinkMessage($title, $text, $messageUrl, $picUrl = ''){ - $this->message = new Link($title,$text,$messageUrl,$picUrl); - $this->message->sendAt($this->mobiles,$this->atAll); + public function setLinkMessage($title, $text, $messageUrl, $picUrl = '') + { + $this->message = new Link($title, $text, $messageUrl, $picUrl); + $this->message->sendAt($this->mobiles, $this->atAll); return $this; } @@ -116,9 +122,10 @@ public function setLinkMessage($title, $text, $messageUrl, $picUrl = ''){ * @param $text * @return $this */ - public function setMarkdownMessage($title, $markdown){ - $this->message = new Markdown($title,$markdown); - $this->message->sendAt($this->mobiles,$this->atAll); + public function setMarkdownMessage($title, $markdown) + { + $this->message = new Markdown($title, $markdown); + $this->message->sendAt($this->mobiles, $this->atAll); return $this; } @@ -130,43 +137,32 @@ public function setMarkdownMessage($title, $markdown){ * @param int $btnOrientation * @return ActionCard|Message */ - public function setActionCardMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0){ + public function setActionCardMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0) + { $this->message = new ActionCard($this, $title, $markdown, $hideAvatar, $btnOrientation); - $this->message->sendAt($this->mobiles,$this->atAll); + $this->message->sendAt($this->mobiles, $this->atAll); return $this->message; } /** * @return FeedCard|Message */ - public function setFeedCardMessage(){ + public function setFeedCardMessage() + { $this->message = new FeedCard($this); - $this->message->sendAt($this->mobiles,$this->atAll); + $this->message->sendAt($this->mobiles, $this->atAll); return $this->message; } /** - * @return bool|string + * @return bool|array */ - public function send(){ - if (! $this->config['enabled']){ + public function send() + { + if (!$this->config['enabled']) { return false; } - - $client = new Client([ - 'timeout' => $this->config['timeout'] ?? 2.0, - ]); - - $request = $client->post($this->getRobotUrl(),[ - 'body' => json_encode($this->message->getBody()), - 'headers' => [ - 'Content-Type' => 'application/json', - ], - 'verify' => $this->config['ssl_verify'] ?? true, - ]); - - $result = $request->getBody()->getContents(); - return $result; + return $this->client->send($this->message->getBody()); } -} \ No newline at end of file +} diff --git a/src/HttpClient.php b/src/HttpClient.php new file mode 100644 index 0000000..5a1a376 --- /dev/null +++ b/src/HttpClient.php @@ -0,0 +1,82 @@ +config = $config; + $this->setAccessToken(); + $this->client = $this->createClient(); + } + + /** + * + */ + public function setAccessToken(){ + $this->accessToken = $this->config['token']; + } + + /** + * create a guzzle client + * @return Client + * @author wangju 2019-05-17 20:25 + */ + protected function createClient() + { + $client = new Client([ + 'timeout' => $this->config['timeout'] ?? 2.0, + ]); + return $client; + } + + /** + * @return string + */ + public function getRobotUrl(){ + return $this->hookUrl . "?access_token={$this->accessToken}"; + } + + /** + * send message + * @param $url + * @param $params + * @return array + * @author wangju 2019-05-17 20:48 + */ + public function send($params): array + { + $request = $this->client->post($this->getRobotUrl(), [ + 'body' => json_encode($params), + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'verify' => $this->config['ssl_verify'] ?? true, + ]); + + $result = $request->getBody()->getContents(); + return json_decode($result, true); + } +} diff --git a/src/SendClient.php b/src/SendClient.php new file mode 100644 index 0000000..f894b63 --- /dev/null +++ b/src/SendClient.php @@ -0,0 +1,15 @@ +setUp(); } + /** + * available content to set + * @param $content + * @return bool + * @author wangju 2019-05-17 21:50 + */ + protected function matchContent($content) + { + return $content['title'] && $content['text']; + } + /** * A basic test example. * @@ -31,7 +42,10 @@ public function testPushActionSingleMessage() ->actionCard($this->title,$this->text,1) ->single("阅读全文","https://www.dingtalk.com/") ->send(); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } public function testPushActionBtnsMessageAtAllUser(){ @@ -40,6 +54,9 @@ public function testPushActionBtnsMessageAtAllUser(){ ->addButtons("内容不错","https://www.dingtalk.com/") ->addButtons("不感兴趣","https://www.dingtalk.com/") ->send(); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } -} \ No newline at end of file +} diff --git a/tests/Feature/FeedTest.php b/tests/Feature/FeedTest.php index 588c5f9..b8ec05b 100644 --- a/tests/Feature/FeedTest.php +++ b/tests/Feature/FeedTest.php @@ -16,6 +16,23 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->setUp(); } + /** + * available content to set + * @param $content + * @return bool + * @author wangju 2019-05-17 21:50 + */ + protected function matchContent($content) + { + if (empty($content)){ + return false; + } + return array_reduce($content,function ($carry,$item){ + if ($carry === null) return true; + return $carry && $item['title'] && $item['messageURL'] && $item['picURL']; + }); + } + /** * A basic test example. * @@ -24,7 +41,10 @@ public function __construct($name = null, array $data = [], $dataName = '') public function testPushTextMessage() { $result =$this->ding->text("我就是我,@{$this->testUser} 是不一样的烟火"); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } public function testPushTextMessageAtAllUser(){ @@ -34,6 +54,9 @@ public function testPushTextMessageAtAllUser(){ ->addLinks('时代的火车向前开2',$this->messageUrl,$this->picUrl) ->send(); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } -} \ No newline at end of file +} diff --git a/tests/Feature/LinkTest.php b/tests/Feature/LinkTest.php index 83bf05a..31d21fb 100644 --- a/tests/Feature/LinkTest.php +++ b/tests/Feature/LinkTest.php @@ -20,6 +20,17 @@ public function __construct($name = null, array $data = [], $dataName = '') } + /** + * available content to set + * @param $content + * @return bool + * @author wangju 2019-05-17 21:50 + */ + protected function matchContent($content) + { + return $content['text'] && $content['title'] && $content['messageUrl']; + } + /** * A basic test example. * @@ -28,7 +39,10 @@ public function __construct($name = null, array $data = [], $dataName = '') public function testPushLinkMessage() { $result = $this->ding->link($this->title,$this->text,$this->messageUrl,$this->picUrl); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}", $result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } -} \ No newline at end of file +} diff --git a/tests/Feature/MarkdownTest.php b/tests/Feature/MarkdownTest.php index dbf2e4c..95484fa 100644 --- a/tests/Feature/MarkdownTest.php +++ b/tests/Feature/MarkdownTest.php @@ -19,6 +19,17 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->setUp(); } + /** + * available content to set + * @param $content + * @return bool + * @author wangju 2019-05-17 21:50 + */ + protected function matchContent($content) + { + return $content['title'] && $content['text']; + } + /** * A basic test example. * @@ -27,13 +38,19 @@ public function __construct($name = null, array $data = [], $dataName = '') public function testPushMarkdownMessage() { $result =$this->ding->markdown($this->title,$this->markdown); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } public function testPushMarkdownMessageAtAllUser(){ $result =$this->ding ->at([],true) ->markdown($this->title,$this->markdown); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } -} \ No newline at end of file +} diff --git a/tests/Feature/TextTest.php b/tests/Feature/TextTest.php index ca39c4f..adb7b16 100644 --- a/tests/Feature/TextTest.php +++ b/tests/Feature/TextTest.php @@ -2,6 +2,7 @@ namespace DingNotice\Tests\Feature; +use DingNotice\SendClient; use DingNotice\Tests\TestCase; @@ -13,6 +14,18 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->setUp(); } + /** + * available content to set + * @param $content + * @return bool + * @author wangju 2019-05-17 21:50 + */ + protected function matchContent($content) + { + $text = $content['content']; + return !empty($text); + } + /** * A basic test example. * @@ -21,13 +34,19 @@ public function __construct($name = null, array $data = [], $dataName = '') public function testPushTextMessage() { $result =$this->ding->text("我就是我,@{$this->testUser} 是不一样的烟火"); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } public function testPushTextMessageAtAllUser(){ $result =$this->ding ->at([],true) ->text("我就是我,@{$this->testUser} 是不一样的烟火"); - $this->assertSame("{\"errcode\":0,\"errmsg\":\"ok\"}",$result); + $this->assertSame([ + 'errmsg' => 'ok', + 'errcode' => 0 + ],$result); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 4fceb04..bc0f8aa 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace DingNotice\Tests; use DingNotice\DingTalk; +use DingNotice\SendClient; use PHPUnit\Framework\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase @@ -12,27 +13,53 @@ abstract class TestCase extends BaseTestCase */ protected $ding; protected $testUser; + protected $config; public function setUp(){ - $token = 'b650efc8cda1c8bf0f7c2aa54803e7ed120b1e26420deff8ae9086791f383ecc'; + $token = 'f80be582aafed07cfced271c333c7ba7f46b873ebf7168e570919296b8062bad'; $this->testUser = '18888888888'; $robot1['timeout'] = 30.0; $robot1['enabled'] = true; $robot1['token'] = $token; - $robot2['timeout'] = 30.0; - $robot2['enabled'] = true; - $robot2['token'] = "bb0ba5d6ae464ea038374abcc683f3306d9c8177041936c5a2d79adf3b066c8b"; - $config['default'] = $robot1; - $config['other'] = $robot2; - $ding = new DingTalk($config); - $this->ding = $ding; - sleep(10); + $this->config = $config; + $this->ding = $this->mockDingClient(); + } + + /** + * mock ding client + * @param null $client + * @return DingTalk + * @author wangju 2019-05-17 20:53 + */ + protected function mockDingClient($client = null) + { + $client = \Mockery::mock(SendClient::class); + $client->shouldReceive('send')->withArgs(function ($arg) { + $messageType = $arg['msgtype']; + + if (!in_array($messageType, ['text', 'actionCard', 'feedCard', 'link', 'markdown'])) { + return false; + } + if (!array_key_exists($messageType, $arg)) { + return false; + } + return $this->matchContent($arg[$messageType]); + })->andReturn([ + 'errmsg' => 'ok', + 'errcode' => 0 + ]); + $ding = new DingTalk($this->config, $client); + return $ding; + } + protected function matchContent($content) + { + return true; } -} \ No newline at end of file +}