Skip to content

Commit

Permalink
Apply SetList::SYMPLIFY (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
zingimmick authored Mar 26, 2021
1 parent 40e6b16 commit 63e7751
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 165 deletions.
10 changes: 1 addition & 9 deletions changelog-linker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(
Option::AUTHORS_TO_IGNORE,
[
'zingimmick',
'dependabot[bot]',
'fossabot',
'renovate[bot]',
]
);
$parameters->set(Option::AUTHORS_TO_IGNORE, ['zingimmick', 'dependabot[bot]', 'fossabot', 'renovate[bot]']);

$parameters->set(Option::CHANGELOG_FORMAT, ChangelogFormat::CATEGORIES_ONLY);
};
9 changes: 1 addition & 8 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/vendor/zing/coding-standard/config/config.php');
$parameters = $containerConfigurator->parameters();
$parameters->set(
Option::SETS,
[
SetList::CLEAN_CODE,
SetList::COMMON,
SetList::PSR_12,
]
);
$parameters->set(Option::SETS, [SetList::PSR_12, SetList::SYMPLIFY, SetList::COMMON, SetList::CLEAN_CODE]);
$parameters->set(Option::SKIP, [
YodaStyleFixer::class => null,
PhpUnitInternalClassFixer::class,
Expand Down
3 changes: 2 additions & 1 deletion src/Channels/SmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function send($notifiable, Notification $notification): void
return;
}

$this->smsManager->connection($message->connection)->send($receiver, $message);
$this->smsManager->connection($message->connection)
->send($receiver, $message);
}

/**
Expand Down
25 changes: 19 additions & 6 deletions src/Commands/SmsSwitchConnectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class SmsSwitchConnectionCommand extends Command
*/
protected function getArguments()
{
return [
new InputArgument('connection', InputArgument::REQUIRED, 'Which connection to use'),
];
return [new InputArgument('connection', InputArgument::REQUIRED, 'Which connection to use')];
}

/**
Expand All @@ -38,9 +36,24 @@ protected function getArguments()
protected function getOptions()
{
return [
new InputOption('show', 's', InputOption::VALUE_NONE, 'Display the sms default connection instead of modifying files'),
new InputOption('always-no', null, InputOption::VALUE_NONE, 'Skip generating sms default connection if it already exists'),
new InputOption('force', 'f', InputOption::VALUE_NONE, 'Skip confirmation when overwriting an existing sms default connection'),
new InputOption(
'show',
's',
InputOption::VALUE_NONE,
'Display the sms default connection instead of modifying files'
),
new InputOption(
'always-no',
null,
InputOption::VALUE_NONE,
'Skip generating sms default connection if it already exists'
),
new InputOption(
'force',
'f',
InputOption::VALUE_NONE,
'Skip confirmation when overwriting an existing sms default connection'
),
];
}

Expand Down
12 changes: 10 additions & 2 deletions src/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,17 @@ public function send($number, $message)

try {
Event::dispatch(new SmsSending($number, $message));
Log::debug(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $number, $message->getContent($this->driver), $message->getTemplate($this->driver), json_encode($message->getData($this->driver)), $message->getMessageType()));
$content = sprintf(
'number: %s, message: "%s", template: "%s", data: %s, type: %s',
$number,
$message->getContent($this->driver),
$message->getTemplate($this->driver),
json_encode($message->getData($this->driver)),
$message->getMessageType()
);
Log::debug($content);
$result = $this->driver->send($number, $message, $this->config);
Log::debug(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $number, $message->getContent($this->driver), $message->getTemplate($this->driver), json_encode($message->getData($this->driver)), $message->getMessageType()), $result);
Log::debug($content, $result);
Event::dispatch(new SmsSent($number, $message, $result));

return $result;
Expand Down
3 changes: 2 additions & 1 deletion src/Events/SmsSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Zing\LaravelSms\Events;

use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;

class SmsSent
{
Expand All @@ -30,7 +31,7 @@ class SmsSent
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
* @param mixed $result
*/
public function __construct(\Overtrue\EasySms\Contracts\PhoneNumberInterface $number, MessageInterface $message, $result)
public function __construct(PhoneNumberInterface $number, MessageInterface $message, $result)
{
$this->number = $number;
$this->message = $message;
Expand Down
9 changes: 8 additions & 1 deletion src/Gateways/LogGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ public function send(PhoneNumberInterface $to, MessageInterface $message, Config
{
$channel = $this->config->get('channel');
$level = $this->config->get('level', 'info');
Log::channel($channel)->{$level}(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $to, $message->getContent($this), $message->getTemplate($this), json_encode($message->getData($this)), $message->getMessageType()));
Log::channel($channel)->{$level}(sprintf(
'number: %s, message: "%s", template: "%s", data: %s, type: %s',
$to,
$message->getContent($this),
$message->getTemplate($this),
json_encode($message->getData($this)),
$message->getMessageType()
));

return [
'success' => true,
Expand Down
33 changes: 10 additions & 23 deletions src/SmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,24 @@ public function boot(): void
return;
}

$this->publishes(
[
$this->getConfigPath() => config_path('sms.php'),
],
'config'
);
$this->publishes([
$this->getConfigPath() => config_path('sms.php'),
], 'config');
}

public function register(): void
{
$this->registerConfig();
Notification::resolved(
function (ChannelManager $service): void {
$service->extend(
self::SMS,
function (Container $app) {
return $app->make(SmsChannel::class);
}
);
}
);
$this->app->singleton(
self::SMS,
function (Container $app) {
return $app->make(SmsManager::class);
$service->extend(self::SMS, function (Container $app) {
return $app->make(SmsChannel::class);
});
}
);
$this->app->singleton(self::SMS, function (Container $app) {
return $app->make(SmsManager::class);
});
$this->app->alias(self::SMS, Sms::class);
$this->registerCommands();
}
Expand All @@ -76,10 +67,6 @@ protected function registerConfig(): void
protected function registerCommands(): void
{
$this->app->singleton('command.sms.gateway', SmsSwitchConnectionCommand::class);
$this->commands(
[
'command.sms.gateway',
]
);
$this->commands(['command.sms.gateway']);
}
}
61 changes: 25 additions & 36 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ class CommandTest extends TestCase
{
public function testCommand(): void
{
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(SmsSwitchConnectionCommand::class, [
'connection' => 'default',
])->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
Expand All @@ -25,40 +22,34 @@ public function testCommand(): void
]
)->assertExitCode(0);
file_put_contents($this->envPath(), '');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
]
)
->expectsQuestion('This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?', false)
$this->artisan(SmsSwitchConnectionCommand::class, [
'connection' => 'default',
])->assertExitCode(0);
$this->artisan(SmsSwitchConnectionCommand::class, [
'connection' => 'default-2',
])
->expectsQuestion(
'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?',
false
)
->assertExitCode(0);
self::assertSame(config('sms.default'), 'default');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default-2',
]
)->expectsQuestion('This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?', true)
$this->artisan(SmsSwitchConnectionCommand::class, [
'connection' => 'default-2',
])->expectsQuestion(
'This maybe invalidate existing sms feature. Are you sure you want to override the sms default connection?',
true
)
->assertExitCode(0);
self::assertSame(config('sms.default'), 'default-2');
}

public function testAlwaysNo(): void
{
file_put_contents($this->envPath(), '');
$this->artisan(
SmsSwitchConnectionCommand::class,
[
'connection' => 'default',
]
)->assertExitCode(0);
$this->artisan(SmsSwitchConnectionCommand::class, [
'connection' => 'default',
])->assertExitCode(0);
$this->artisan(
SmsSwitchConnectionCommand::class,
[
Expand Down Expand Up @@ -93,11 +84,9 @@ protected function tearDown(): void
unlink($this->envPath());
}

config(
[
'sms.default' => $this->connection,
]
);
config([
'sms.default' => $this->connection,
]);

parent::tearDown();
}
Expand Down
29 changes: 15 additions & 14 deletions tests/Drivers/MeiLianDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,21 @@ public function testDefaultSignature($number, $message, $expected): void
$response = 'success:Missing recipient';

$driver = Mockery::mock(MeilianGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();
$driver->shouldReceive('request')->with(
'post',
'http://m.5c.com.cn/api/send/index.php',
[
'headers' => [],
'form_params' => [
'username' => 'mock-username',
'password' => 'mock-password',
'apikey' => 'mock-api-key',
'mobile' => $number,
'content' => $expected,
],
]
)->andReturn($response);
$driver->shouldReceive('request')
->with(
'post',
'http://m.5c.com.cn/api/send/index.php',
[
'headers' => [],
'form_params' => [
'username' => 'mock-username',
'password' => 'mock-password',
'apikey' => 'mock-api-key',
'mobile' => $number,
'content' => $expected,
],
]
)->andReturn($response);
$config = new Config($config);

$this->assertSame(
Expand Down
38 changes: 22 additions & 16 deletions tests/Drivers/YunpianDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,19 @@ public function testDefaultSignature($number, $message, $expected): void
];

$driver = Mockery::mock(YunpianGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();
$driver->shouldReceive('request')->with(
'post',
'/v1/sms/send.json',
[
'headers' => [],
'form_params' => [
'apikey' => 'mock-api-key',
'mobile' => $number,
'text' => $expected,
],
]
)->andReturn($response);
$driver->shouldReceive('request')
->with(
'post',
'/v1/sms/send.json',
[
'headers' => [],
'form_params' => [
'apikey' => 'mock-api-key',
'mobile' => $number,
'text' => $expected,
],
]
)->andReturn($response);
$config = new Config($config);

$this->assertSame($response, $driver->send(new PhoneNumber($number), SmsMessage::text($message), $config));
Expand All @@ -131,10 +132,15 @@ public function testDefaultSignature($number, $message, $expected): void
public function testGetOptions(): void
{
$driver = Mockery::mock(YunpianGateway::class, [[]])->shouldAllowMockingProtectedMethods();
$driver->shouldReceive('getBaseOptions')->once()->passthru();
$driver->allows('getGuzzleOptions')->passthru();
$driver->allows('getBaseUri')->passthru();
$driver->allows('getTimeout')->passthru();
$driver->shouldReceive('getBaseOptions')
->once()
->passthru();
$driver->allows('getGuzzleOptions')
->passthru();
$driver->allows('getBaseUri')
->passthru();
$driver->allows('getTimeout')
->passthru();
self::assertSame('http://yunpian.com', Arr::get($driver->getBaseOptions(), 'base_uri'));
}

Expand Down
6 changes: 4 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ protected function getEnvironmentSetUp($app): void

public function testAllDriversImplementsGatewayInterface(): void
{
$drivers = collect(config('sms.connections'))->pluck('driver');
$drivers = collect(config('sms.connections'))
->pluck('driver');
$drivers->each(
function ($driver): void {
if (class_exists($driver)) {
Expand All @@ -34,7 +35,8 @@ function ($driver): void {

public function testAllDriversHasDefaultConfig(): void
{
$drivers = collect(config('sms.connections'))->pluck('driver');
$drivers = collect(config('sms.connections'))
->pluck('driver');
$gateways = collect(ClassMapGenerator::createMap('vendor/overtrue/easy-sms'))
->keys()
->filter(
Expand Down
5 changes: 4 additions & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function testSend(): void

public function testAlias(): void
{
$this->assertSame(forward_static_call([\Sms::class, 'connection']), forward_static_call([Sms::class, 'connection']));
$this->assertSame(
forward_static_call([\Sms::class, 'connection']),
forward_static_call([Sms::class, 'connection'])
);
}
}
Loading

0 comments on commit 63e7751

Please sign in to comment.