Skip to content

Commit cdf5557

Browse files
committed
Refactor and added some tests
1 parent 0d8b0e0 commit cdf5557

File tree

5 files changed

+127
-26
lines changed

5 files changed

+127
-26
lines changed

src/MessagebirdChannel.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
namespace NotificationChannels\Messagebird;
44

5-
use Exception;
65
use Illuminate\Notifications\Notification;
7-
use MessageBird\Exceptions\AuthenticateException;
8-
use MessageBird\Exceptions\BalanceException;
9-
use NotificationChannels\Messagebird\Exceptions\CouldNotSendNotification;
10-
use MessageBird\Client;
116

127
class MessagebirdChannel
138
{
149
/** @var \MessageBird\Client */
1510
protected $client;
1611

17-
public function __construct(Client $client)
12+
public function __construct(MessagebirdClient $client)
1813
{
1914
$this->client = $client;
2015
}
@@ -35,22 +30,6 @@ public function send($notifiable, Notification $notification)
3530
$message = MessagebirdMessage::create($message);
3631
}
3732

38-
if (empty($message->originator)) {
39-
$message->setOriginator(config('services.messagebird.originator'));
40-
}
41-
42-
if (empty($message->recipients)) {
43-
$message->setRecipients(config('services.messagebird.recipients'));
44-
}
45-
46-
try {
47-
$this->client->messages->create($message);
48-
} catch (AuthenticateException $exception) {
49-
throw CouldNotSendNotification::couldNotAuthenticate();
50-
} catch (BalanceException $exception) {
51-
throw CouldNotSendNotification::notEnoughCredits();
52-
} catch (Exception $exception) {
53-
throw CouldNotSendNotification::serviceRespondedWithAnError($exception);
54-
}
33+
$this->client->send($message);
5534
}
5635
}

src/MessagebirdClient.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace NotificationChannels\Messagebird;
4+
5+
use Exception;
6+
use MessageBird\Exceptions\AuthenticateException;
7+
use MessageBird\Exceptions\BalanceException;
8+
use NotificationChannels\Messagebird\Exceptions\CouldNotSendNotification;
9+
use MessageBird\Client;
10+
11+
class MessagebirdClient
12+
{
13+
protected $client;
14+
15+
public function __construct(Client $client)
16+
{
17+
$this->client = $client;
18+
}
19+
20+
public function send($message) {
21+
if (empty($message->originator)) {
22+
$message->setOriginator(config('services.messagebird.originator'));
23+
}
24+
if (empty($message->recipients)) {
25+
$message->setRecipients(config('services.messagebird.recipients'));
26+
}
27+
28+
try {
29+
$this->client->messages->create($message);
30+
} catch (AuthenticateException $exception) {
31+
throw CouldNotSendNotification::couldNotAuthenticate();
32+
} catch (BalanceException $exception) {
33+
throw CouldNotSendNotification::notEnoughCredits();
34+
} catch (Exception $exception) {
35+
throw CouldNotSendNotification::serviceRespondedWithAnError($exception);
36+
}
37+
}
38+
}

src/MessagebirdServiceProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ class MessagebirdServiceProvider extends ServiceProvider
1414
public function boot()
1515
{
1616
$this->app->when(MessagebirdChannel::class)
17-
->needs(Client::class)
17+
->needs(MessagebirdClient::class)
1818
->give(function () {
1919
$config = config('services.messagebird');
2020

2121
if (is_null($config)) {
2222
throw InvalidConfiguration::configurationNotSet();
2323
}
2424

25-
return new Client(
25+
return new MessagebirdClient(new Client(
2626
$config['access_key']
27-
);
27+
));
2828
});
2929
}
3030
}

tests/MessagebirdChannelTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace NotificationChannels\Messagebird\Test;
4+
5+
use Illuminate\Notifications\Notifiable;
6+
use Illuminate\Notifications\Notification;
7+
use MessageBird\Client;
8+
use Mockery;
9+
use NotificationChannels\Messagebird\MessagebirdChannel;
10+
use NotificationChannels\Messagebird\MessagebirdClient;
11+
use NotificationChannels\Messagebird\MessagebirdMessage;
12+
use PHPUnit_Framework_TestCase;
13+
14+
class MessagebirdChannelTest extends PHPUnit_Framework_TestCase
15+
{
16+
public function setUp()
17+
{
18+
$this->notification = new TestNotification;
19+
$this->notifiable = new TestNotifiable;
20+
$this->messagebirdClient = Mockery::mock(new Client('test_1234567890'));
21+
$this->client = Mockery::mock(new MessagebirdClient($this->messagebirdClient));
22+
$this->channel = new MessagebirdChannel($this->client);
23+
}
24+
25+
public function tearDown()
26+
{
27+
Mockery::close();
28+
parent::tearDown();
29+
}
30+
31+
/** @test */
32+
public function it_can_be_instantiated()
33+
{
34+
$this->assertInstanceOf(MessagebirdClient::class, $this->client);
35+
$this->assertInstanceOf(MessagebirdChannel::class, $this->channel);
36+
}
37+
}
38+
39+
class TestNotifiable
40+
{
41+
use Notifiable;
42+
43+
public function routeNotificationForMessagebird()
44+
{
45+
return '0031650520659';
46+
}
47+
}
48+
49+
class TestNotification extends Notification
50+
{
51+
public function toMessagebird($notifiable)
52+
{
53+
return (new MessagebirdMessage("Message content"))->setOriginator('APPNAME')->setRecipients('0031650520659');
54+
}
55+
}

tests/MessagebirdClientTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace NotificationChannels\Messagebird\Test;
4+
5+
use MessageBird\Client;
6+
use Mockery;
7+
use NotificationChannels\Messagebird\MessagebirdClient;
8+
use PHPUnit_Framework_TestCase;
9+
10+
class MessagebirdClientTest extends PHPUnit_Framework_TestCase
11+
{
12+
public function setUp()
13+
{
14+
$this->messagebirdClient = Mockery::mock(new Client('test_1234567890'));
15+
$this->client = Mockery::mock(new MessagebirdClient($this->messagebirdClient));
16+
}
17+
18+
public function tearDown()
19+
{
20+
Mockery::close();
21+
parent::tearDown();
22+
}
23+
24+
/** @test */
25+
public function it_can_be_instantiated()
26+
{
27+
$this->assertInstanceOf(MessagebirdClient::class, $this->client);
28+
}
29+
}

0 commit comments

Comments
 (0)