Skip to content

Commit

Permalink
Fixed overtrue#317
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Nov 19, 2021
1 parent bd37116 commit f12e462
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ composer.lock
/index.php
/config.php
.phpunit.result.cache
cghooks.lock

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
<a href="https://travis-ci.org/overtrue/easy-sms"><img src="https://travis-ci.org/overtrue/easy-sms.svg?branch=master" alt="Build Status"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/v/unstable.svg" alt="Latest Unstable Version"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
<a href="https://scrutinizer-ci.com/g/overtrue/easy-sms/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/easy-sms/badges/coverage.png?b=master" alt="Code Coverage"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/downloads" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/overtrue/easy-sms"><img src="https://poser.pugx.org/overtrue/easy-sms/license" alt="License"></a>
</p>

[![Sponsor me](https://raw.githubusercontent.com/overtrue/overtrue/master/sponsor-me-button-s.svg)](https://github.com/sponsors/overtrue)
<p align="center">
<a href="https://github.com/sponsors/overtrue"><img src="https://raw.githubusercontent.com/overtrue/overtrue/master/sponsor-me-button-s.svg" alt="Sponsor me" style="max-width: 100%;"></a>
</p>

## 特点

Expand Down
40 changes: 38 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"phpunit/phpunit": "^5.7 || ^7.5 || ^8.5.19 || ^9.5.8",
"mockery/mockery": "~1.3.3 || ^1.4.2"
"mockery/mockery": "~1.3.3 || ^1.4.2",
"brainmaestro/composer-git-hooks": "^2.8",
"vimeo/psalm": "^4.10",
"phpstan/phpstan": "^1.0",
"jetbrains/phpstorm-attributes": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -25,5 +30,36 @@
"authors": [{
"name": "overtrue",
"email": "[email protected]"
}]
}],
"extra": {
"hooks": {
"pre-commit": [
"composer check-style",
"composer psalm",
"composer test"
],
"pre-push": [
"composer check-style"
]
}
},
"scripts": {
"post-update-cmd": [
"cghooks remove",
"cghooks add --ignore-lock",
"cghooks update"
],
"post-merge": "composer install",
"post-install-cmd": [
"cghooks remove",
"cghooks add --ignore-lock",
"cghooks update"
],
"phpstan": "phpstan analyse",
"check-style": "php-cs-fixer fix --using-cache=no --diff --config=.php-cs-fixer.dist.php --dry-run --allow-risky=yes --ansi",
"fix-style": "php-cs-fixer fix --using-cache=no --config=.php-cs-fixer.dist.php --allow-risky=yes --ansi",
"test": "phpunit --colors",
"psalm": "psalm --show-info=true --no-cache",
"psalm-fix": "psalm --no-cache --alter --issues=MissingReturnType,MissingParamType"
}
}
1 change: 0 additions & 1 deletion src/EasySms.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Overtrue\EasySms\Gateways\Gateway;
use Overtrue\EasySms\Strategies\OrderStrategy;
use Overtrue\EasySms\Support\Config;
use RuntimeException;

/**
* Class EasySms.
Expand Down
12 changes: 10 additions & 2 deletions src/Gateways/QcloudGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
]);

if (!empty($result['Response']['Error']['Code'])) {
throw new GatewayErrorException($result['Response']['Error']['Message'], $result['Response']['Error']['Code'], $result);
throw new GatewayErrorException($result['Response']['Error']['Message'], 400, $result);
}

if (!empty($result['Response']['SendStatusSet'])) {
foreach ($result['Response']['SendStatusSet'] as $group) {
if ($group['Code'] != 'Ok') {
throw new GatewayErrorException($group['Message'], 400, $result);
}
}
}

return $result;
Expand All @@ -103,7 +111,7 @@ protected function generateSign($params, $timestamp)
$secretKey = $this->config->get('secret_key');
$secretId = $this->config->get('secret_id');

$canonicalRequest = 'POST'."\n".
$canonicalRequest = 'POST'."\n".
'/'."\n".
'' ."\n".
'content-type:application/json; charset=utf-8'."\n".
Expand Down
3 changes: 1 addition & 2 deletions src/Gateways/SmsbaoGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Overtrue\EasySms\Gateways;


use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
Expand All @@ -32,7 +31,7 @@ class SmsbaoGateway extends Gateway
const SUCCESS_CODE = '0';

protected $errorStatuses = [
'0' => '短信发送成功',
'0' => '短信发送成功',
'-1' => '参数不全',
'-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!',
'30' => '密码错误',
Expand Down
3 changes: 1 addition & 2 deletions src/Gateways/TiniyoGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Overtrue\EasySms\Gateways;

use GuzzleHttp\Exception\ClientException;
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
Expand All @@ -30,7 +29,7 @@ class TiniyoGateway extends Gateway
const ENDPOINT_URL = 'https://api.tiniyo.com/v1/Account/%s/Message';

const SUCCESS_CODE = '000000';

public function getName()
{
return 'tiniyo';
Expand Down
8 changes: 4 additions & 4 deletions src/Gateways/YunpianGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
'exceptions' => false,
];

if(!is_null($template)){
if (!is_null($template)) {
$function = 'tpl_single_send';
$data = [];

Expand All @@ -63,14 +63,14 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
$data[] = urlencode('#'.$key.'#') . '=' . urlencode($value);
}

$option['form_params'] = array_merge($option['form_params'],[
$option['form_params'] = array_merge($option['form_params'], [
'tpl_id' => $template,
'tpl_value' => implode('&', $data)
]);
}else{
} else {
$content = $message->getContent($this);
$signature = $config->get('signature', '');
$option['form_params'] = array_merge($option['form_params'],[
$option['form_params'] = array_merge($option['form_params'], [
'text' => 0 === \stripos($content, '') ? $content : $signature.$content
]);
}
Expand Down
2 changes: 0 additions & 2 deletions tests/EasySmsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
use Overtrue\EasySms\Gateways\AliyunGateway;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\Messenger;
use Overtrue\EasySms\PhoneNumber;
use Overtrue\EasySms\Support\Config;
use RuntimeException;

class EasySmsTest extends TestCase
{
Expand Down
47 changes: 46 additions & 1 deletion tests/Gateways/QcloudGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,54 @@ public function testSend()
], $gateway->send(new PhoneNumber(18888888888), $message, $config));

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(0);
$this->expectExceptionCode(400);
$this->expectExceptionMessage('The provided credentials could not be validated. Please check your signature is correct.');

$gateway->send(new PhoneNumber(18888888888), $message, $config);
}

public function testSendWithPartialErrors()
{
$config = [
'sdk_app_id' => 'mock-sdk-app-id',
'secret_key' => 'mock-secret-key',
'secret_id' => 'mock-secret-id',
'sign_name' => 'mock-api-sign-name'
];

$gateway = \Mockery::mock(QcloudGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods();

$gateway->shouldReceive('request')
->andReturn([
'Response' => [
"SendStatusSet" => [
[
"SerialNo" => "2028:f825e6b16e23f73f4123",
"PhoneNumber" => "8618888888888",
"Fee" => 1,
"SessionContext" => "",
"Code" => "InvalidParameterValue.TemplateParameterFormatError",
"Message" => "Verification code template parameter format error",
"IsoCode" => "CN"
]
]
],
'RequestId' => '0dc99542-c61a-4a16-9545-ec8ec202c543'
])->once();

$message = new Message([
'template' => 'template-id',
'data' => [
"888888",
],
]);

$config = new Config($config);

$this->expectException(GatewayErrorException::class);
$this->expectExceptionCode(400);
$this->expectExceptionMessage('Verification code template parameter format error');

$gateway->send(new PhoneNumber(18888888888), $message, $config);
}
}
1 change: 0 additions & 1 deletion tests/Gateways/SmsbaoGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Overtrue\EasySms\Tests\Gateways;


use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Gateways\SmsbaoGateway;
use Overtrue\EasySms\Message;
Expand Down

0 comments on commit f12e462

Please sign in to comment.