Skip to content

Commit

Permalink
Revert #136
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Sep 14, 2018
1 parent 6cefa1f commit d7e8fd6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
10 changes: 8 additions & 2 deletions src/EasySms.php
Expand Up @@ -338,10 +338,16 @@ protected function formatGateways(array $gateways)
$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;
}
}
2 changes: 1 addition & 1 deletion src/Gateways/ChuanglanGateway.php
Expand Up @@ -13,8 +13,8 @@

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;

Expand Down
9 changes: 4 additions & 5 deletions src/Messenger.php
Expand Up @@ -14,7 +14,6 @@
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
use Overtrue\EasySms\Support\Config;

/**
* Class Messenger.
Expand Down Expand Up @@ -56,23 +55,23 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, array
$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,
Expand Down
44 changes: 44 additions & 0 deletions tests/EasySmsTest.php
Expand Up @@ -135,6 +135,50 @@ public function testGetMessenger()

$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
Expand Down
12 changes: 6 additions & 6 deletions tests/Gateways/ChuanglanGatewayTest.php
Expand Up @@ -145,13 +145,13 @@ public function testBuildEndpoint()
$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));
}

/**
Expand All @@ -168,12 +168,12 @@ public function testGetChannel()
// 验证码通道
$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));
}

/**
Expand Down Expand Up @@ -211,7 +211,7 @@ public function testValidateCodeChannelWrapChannelContent()
// 验证码通道
$config = ['channel' => ChuanglanGateway::CHANNEL_VALIDATE_CODE];
$config = new Config($config);
$this->assertEquals('这是短信内容。', $method->invoke($gateway, $content, $config));
$this->assertSame('这是短信内容。', $method->invoke($gateway, $content, $config));
}

/**
Expand All @@ -234,7 +234,7 @@ public function testPromotionChannelWrapChannelContent()
'unsubscribe' => '回TD退订',
];
$config = new Config($config);
$this->assertEquals('【通讯云】这是短信内容。回TD退订', $method->invoke($gateway, $content, $config));
$this->assertSame('【通讯云】这是短信内容。回TD退订', $method->invoke($gateway, $content, $config));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Gateways/YuntongxunGatewayTest.php
Expand Up @@ -42,8 +42,8 @@ public function testSend()
'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([
Expand Down

0 comments on commit d7e8fd6

Please sign in to comment.