diff --git a/src/EasySms.php b/src/EasySms.php index e9387ad..c6584c9 100644 --- a/src/EasySms.php +++ b/src/EasySms.php @@ -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; } } diff --git a/src/Gateways/ChuanglanGateway.php b/src/Gateways/ChuanglanGateway.php index 1994b94..94d942d 100644 --- a/src/Gateways/ChuanglanGateway.php +++ b/src/Gateways/ChuanglanGateway.php @@ -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; diff --git a/src/Messenger.php b/src/Messenger.php index b886e08..c09f03d 100644 --- a/src/Messenger.php +++ b/src/Messenger.php @@ -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. @@ -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, diff --git a/tests/EasySmsTest.php b/tests/EasySmsTest.php index 4860d26..5748630 100644 --- a/tests/EasySmsTest.php +++ b/tests/EasySmsTest.php @@ -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 diff --git a/tests/Gateways/ChuanglanGatewayTest.php b/tests/Gateways/ChuanglanGatewayTest.php index 3a69d44..7b42b42 100644 --- a/tests/Gateways/ChuanglanGatewayTest.php +++ b/tests/Gateways/ChuanglanGatewayTest.php @@ -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)); } /** @@ -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)); } /** @@ -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)); } /** @@ -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)); } /** diff --git a/tests/Gateways/YuntongxunGatewayTest.php b/tests/Gateways/YuntongxunGatewayTest.php index d612b5e..f03e825 100644 --- a/tests/Gateways/YuntongxunGatewayTest.php +++ b/tests/Gateways/YuntongxunGatewayTest.php @@ -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([