Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sms events. #17

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Connectors/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Zing\LaravelSms\Connectors;

use GrahamCampbell\Manager\ConnectorInterface;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use function is_array;
use Overtrue\EasySms\Contracts\GatewayInterface;
Expand All @@ -15,6 +16,8 @@
use Overtrue\EasySms\Support\Config;
use Throwable;
use function trim;
use Zing\LaravelSms\Events\SmsSending;
use Zing\LaravelSms\Events\SmsSent;
use Zing\LaravelSms\Exceptions\CouldNotSendNotification;
use Zing\LaravelSms\Exceptions\InvalidArgumentException;

Expand Down Expand Up @@ -109,10 +112,10 @@ protected function formatMessage($message)
*/
public function send($number, $message)
{
try {
$number = $this->formatPhoneNumber($number);
$message = $this->formatMessage($message);
$number = $this->formatPhoneNumber($number);
$message = $this->formatMessage($message);

try {
$this->sending($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()));
$result = $this->driver->send($number, $message, $this->config);
Expand All @@ -127,9 +130,11 @@ public function send($number, $message)

public function sending($number, $message): void
{
Event::dispatch(new SmsSending($number, $message));
}

public function sent($number, $message, $result): void
{
Event::dispatch(new SmsSent($number, $message, $result));
}
}
33 changes: 33 additions & 0 deletions src/Events/SmsSending.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Zing\LaravelSms\Events;

use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;

class SmsSending
{
/**
* @var \Overtrue\EasySms\PhoneNumber
*/
public $number;

/**
* @var \Overtrue\EasySms\Message
*/
public $message;

/**
* SmsSending constructor.
*
* @param \Overtrue\EasySms\PhoneNumber $number
* @param \Overtrue\EasySms\Message $message
*/
public function __construct(PhoneNumber $number, Message $message)
{
$this->number = $number;
$this->message = $message;
}
}
40 changes: 40 additions & 0 deletions src/Events/SmsSent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Zing\LaravelSms\Events;

use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;

class SmsSent
{
/**
* @var \Overtrue\EasySms\PhoneNumber
*/
public $number;

/**
* @var \Overtrue\EasySms\Message
*/
public $message;

/**
* @var mixed
*/
public $result;

/**
* SmsSending constructor.
*
* @param \Overtrue\EasySms\PhoneNumber $number
* @param \Overtrue\EasySms\Message $message
* @param mixed $result
*/
public function __construct(PhoneNumber $number, Message $message, $result)
{
$this->number = $number;
$this->message = $message;
$this->result = $result;
}
}
9 changes: 9 additions & 0 deletions src/Gateways/YunpianGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function getBaseUri()
return 'http://yunpian.com';
}

/**
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $number
* @param \Overtrue\EasySms\Contracts\MessageInterface $message
* @param \Overtrue\EasySms\Support\Config $config
*
* @throws \Zing\LaravelSms\Exceptions\CouldNotSendNotification
*
* @return array
*/
public function send(PhoneNumberInterface $number, MessageInterface $message, Config $config)
{
$signature = $this->config->get('signature');
Expand Down
7 changes: 7 additions & 0 deletions src/SmsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public function __construct(Repository $config, ConnectionFactory $factory)
$this->factory = $factory;
}

/**
* @param array $config
*
* @throws \Zing\LaravelSms\Exceptions\InvalidArgumentException
*
* @return object|\Zing\LaravelSms\Connectors\Connector
*/
protected function createConnection(array $config)
{
return $this->factory->make($config);
Expand Down
65 changes: 65 additions & 0 deletions tests/SmsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Zing\LaravelSms\Tests;

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Mockery;
use Overtrue\EasySms\Message;
use Overtrue\EasySms\PhoneNumber;
use RuntimeException;
use Zing\LaravelSms\Channels\SmsChannel;
use Zing\LaravelSms\Events\SmsSending;
use Zing\LaravelSms\Events\SmsSent;
use Zing\LaravelSms\Exceptions\InvalidArgumentException;
use Zing\LaravelSms\Facades\Sms;
use Zing\LaravelSms\SmsManager;
Expand Down Expand Up @@ -261,4 +264,66 @@ public function testConnectionWithWrongDriver(): void
config()->set('sms.connections.test', ['driver' => $driver]);
Sms::connection('test');
}

/**
* @dataProvider provideNumberAndMessage
*
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface|string $number
* @param \Overtrue\EasySms\Contracts\MessageInterface|string $message
*/
public function testSmsSending($number, $message): void
{
$expectedMessage = $message;
if (is_string($expectedMessage)) {
$expectedMessage = new Message(
[
'content' => $expectedMessage,
'template' => $expectedMessage,
]
);
}

Event::fake();
Sms::connection('log')->send($number, $message);
Event::assertDispatched(
SmsSending::class,
function (SmsSending $smsSending) use ($number,$expectedMessage) {
self::assertSame((string) $number, (string) $smsSending->number);
self::assertSameMessage($expectedMessage, $smsSending->message);

return true;
}
);
}

/**
* @dataProvider provideNumberAndMessage
*
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface|string $number
* @param \Overtrue\EasySms\Contracts\MessageInterface|string $message
*/
public function testSmsSent($number, $message): void
{
$expectedMessage = $message;
if (is_string($expectedMessage)) {
$expectedMessage = new Message(
[
'content' => $expectedMessage,
'template' => $expectedMessage,
]
);
}

Event::fake();
Sms::connection('log')->send($number, $message);
Event::assertDispatched(
SmsSent::class,
function (SmsSent $smsSending) use ($number,$expectedMessage) {
self::assertSame((string) $number, (string) $smsSending->number);
self::assertSameMessage($expectedMessage, $smsSending->message);

return true;
}
);
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Zing\LaravelSms\Tests;

use Orchestra\Testbench\TestCase as BaseTestCase;
use PHPUnit\Framework\Constraint\IsEqual;
use Zing\LaravelSms\Facades\Sms;
use Zing\LaravelSms\Gateways\LogGateway;
use Zing\LaravelSms\Gateways\NullGateway;
Expand All @@ -13,6 +14,11 @@

class TestCase extends BaseTestCase
{
public static function assertSameMessage($expected, $actual, string $message = ''): void
{
static::assertThat($actual, new IsEqual($expected), $message);
}

protected function getPackageProviders($app)
{
return [
Expand Down