Skip to content

Commit

Permalink
test message send with mockery
Browse files Browse the repository at this point in the history
  • Loading branch information
wowiwj committed May 17, 2019
1 parent eec3732 commit ce94dca
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 83 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 7 additions & 3 deletions src/DingTalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -114,4 +118,4 @@ public function feed(){
->setFeedCardMessage();
}

}
}
108 changes: 52 additions & 56 deletions src/DingTalkService.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace DingNotice;

use DingNotice\Messages\ActionCard;
use DingNotice\Messages\FeedCard;
use DingNotice\Messages\Link;
Expand All @@ -13,14 +14,6 @@ class DingTalkService
{

protected $config;
/**
* @var string
*/
protected $accessToken = "";
/**
* @var string
*/
protected $hookUrl = "https://oapi.dingtalk.com/robot/send";

/**
* @var Message
Expand All @@ -35,14 +28,27 @@ class DingTalkService
*/
protected $atAll = false;

/**
* @var SendClient
*/
protected $client;

/**
* DingTalkService constructor.
* @param $config
* @param null $client
*/
public function __construct($config)
public function __construct($config, SendClient $client = null)
{
$this->config = $config;
$this->setTextMessage('null');
$this->setAccessToken();

if ($client != null) {
$this->client = $client;
return;
}
$this->client = $this->createClient($config);

}

/**
Expand All @@ -56,45 +62,44 @@ public function setMessage($message)
/**
* @return array
*/
public function getMessage(){
public function getMessage()
{
return $this->message->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;
}


/**
* @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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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());
}

}
}
82 changes: 82 additions & 0 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Created by PhpStorm.
* User: wangju
* Date: 2019-05-17
* Time: 20:38
*/

namespace DingNotice;


use GuzzleHttp\Client;

class HttpClient implements SendClient
{
protected $client;
protected $config;
/**
* @var string
*/
protected $hookUrl = "https://oapi.dingtalk.com/robot/send";

/**
* @var string
*/
protected $accessToken = "";

public function __construct($config)
{
$this->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);
}
}
15 changes: 15 additions & 0 deletions src/SendClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: wangju
* Date: 2019-05-17
* Time: 20:37
*/

namespace DingNotice;


interface SendClient
{
public function send($params): array;
}
23 changes: 20 additions & 3 deletions tests/Feature/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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(){
Expand All @@ -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);
}
}
}
Loading

0 comments on commit ce94dca

Please sign in to comment.