提交 d7e8fd6d 编写于 作者: O overtrue

Revert #136

上级 6cefa1fd
......@@ -338,10 +338,16 @@ class EasySms
$globalSettings = $this->config->get("gateways.{$gateway}", []);
if (is_string($gateway) && !empty($globalSettings) && is_array($setting)) {
$formatted[$gateway] = array_merge($globalSettings, $setting);
$formatted[$gateway] = new Config(array_merge($globalSettings, $setting));
}
}
return $this->strategy()->apply($formatted);
$result = [];
foreach ($this->strategy()->apply($formatted) as $name) {
$result[$name] = $formatted[$name];
}
return $result;
}
}
......@@ -13,8 +13,8 @@ namespace Overtrue\EasySms\Gateways;
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;
......
......@@ -14,7 +14,6 @@ namespace Overtrue\EasySms;
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Overtrue\EasySms\Support\Config;
/**
* Class Messenger.
......@@ -56,23 +55,23 @@ class Messenger
$results = [];
$isSuccessful = false;
foreach ($gateways as $gateway) {
foreach ($gateways as $gateway => $config) {
try {
$results[$gateway] = [
'gateway' => $gateway,
'status' => self::STATUS_SUCCESS,
'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($this->easySms->getConfig()->get("gateways.{$gateway}"))),
'result' => $this->easySms->gateway($gateway)->send($to, $message, $config),
];
$isSuccessful = true;
break;
} catch (\Throwable $e) {
} catch (\Exception $e) {
$results[$gateway] = [
'gateway' => $gateway,
'status' => self::STATUS_FAILURE,
'exception' => $e,
];
} catch (\Exception $e) {
} catch (\Throwable $e) {
$results[$gateway] = [
'gateway' => $gateway,
'status' => self::STATUS_FAILURE,
......
......@@ -135,6 +135,50 @@ class EasySmsTest extends TestCase
$this->assertInstanceOf(Messenger::class, $easySms->getMessenger());
}
public function testFormatGateways()
{
$config = [
'gateways' => [
'foo' => [
'a' => 'b',
],
'bar' => [
'c' => 'd',
],
],
];
$easySms = \Mockery::mock(EasySms::class.'[formatMessage]', [$config])->makePartial()->shouldAllowMockingProtectedMethods();
// gateway names
$gateways = $easySms->formatGateways(['foo', 'bar']);
$this->assertCount(2, $gateways);
$this->arrayHasKey('foo', $gateways);
$this->arrayHasKey('bar', $gateways);
$this->assertSame('b', $gateways['foo']->get('a'));
$this->assertSame('d', $gateways['bar']->get('c'));
// gateway names && override config
$gateways = $easySms->formatGateways(['foo', 'bar' => ['c' => 'e']]);
$this->assertCount(2, $gateways);
$this->arrayHasKey('foo', $gateways);
$this->arrayHasKey('bar', $gateways);
$this->assertSame('b', $gateways['foo']->get('a'));
$this->assertSame('e', $gateways['bar']->get('c'));
// gateway names && append config
$gateways = $easySms->formatGateways(['foo' => ['f' => 'g'], 'bar' => ['c' => 'e']]);
$this->assertCount(2, $gateways);
$this->arrayHasKey('foo', $gateways);
$this->arrayHasKey('bar', $gateways);
$this->assertSame('b', $gateways['foo']->get('a'));
$this->assertSame('g', $gateways['foo']->get('f'));
$this->assertSame('e', $gateways['bar']->get('c'));
}
}
class DummyGatewayForTest implements GatewayInterface
......
......@@ -145,13 +145,13 @@ class ChuanglanGatewayTest extends TestCase
$config = ['channel' => ChuanglanGateway::CHANNEL_VALIDATE_CODE];
$config = new Config($config);
$endpoint = 'https://smsbj1.253.com/msg/send/json';
$this->assertEquals($endpoint, $method->invoke($gateway, $config));
$this->assertSame($endpoint, $method->invoke($gateway, $config));
// 营销通道
$config = ['channel' => ChuanglanGateway::CHANNEL_PROMOTION_CODE];
$config = new Config($config);
$endpoint = 'https://smssh1.253.com/msg/send/json';
$this->assertEquals($endpoint, $method->invoke($gateway, $config));
$this->assertSame($endpoint, $method->invoke($gateway, $config));
}
/**
......@@ -168,12 +168,12 @@ class ChuanglanGatewayTest extends TestCase
// 验证码通道
$config = ['channel' => ChuanglanGateway::CHANNEL_VALIDATE_CODE];
$config = new Config($config);
$this->assertEquals(ChuanglanGateway::CHANNEL_VALIDATE_CODE, $method->invoke($gateway, $config));
$this->assertSame(ChuanglanGateway::CHANNEL_VALIDATE_CODE, $method->invoke($gateway, $config));
// 营销通道
$config = ['channel' => ChuanglanGateway::CHANNEL_PROMOTION_CODE];
$config = new Config($config);
$this->assertEquals(ChuanglanGateway::CHANNEL_PROMOTION_CODE, $method->invoke($gateway, $config));
$this->assertSame(ChuanglanGateway::CHANNEL_PROMOTION_CODE, $method->invoke($gateway, $config));
}
/**
......@@ -211,7 +211,7 @@ class ChuanglanGatewayTest extends TestCase
// 验证码通道
$config = ['channel' => ChuanglanGateway::CHANNEL_VALIDATE_CODE];
$config = new Config($config);
$this->assertEquals('这是短信内容。', $method->invoke($gateway, $content, $config));
$this->assertSame('这是短信内容。', $method->invoke($gateway, $content, $config));
}
/**
......@@ -234,7 +234,7 @@ class ChuanglanGatewayTest extends TestCase
'unsubscribe' => '回TD退订',
];
$config = new Config($config);
$this->assertEquals('【通讯云】这是短信内容。回TD退订', $method->invoke($gateway, $content, $config));
$this->assertSame('【通讯云】这是短信内容。回TD退订', $method->invoke($gateway, $content, $config));
}
/**
......
......@@ -42,8 +42,8 @@ class YuntongxunGatewayTest extends TestCase
'templateId' => 5589,
'appId' => 'mock-app-id',
'datas' => ['mock-data-1', 'mock-data-2'],
] && $params['headers']['Accept'] == 'application/json'
&& $params['headers']['Content-Type'] == 'application/json;charset=utf-8';
] && 'application/json' == $params['headers']['Accept']
&& 'application/json;charset=utf-8' == $params['headers']['Content-Type'];
})
)
->andReturn([
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册