Skip to content

Commit 96e7113

Browse files
committed
Additional tests to achieve 100% code coverage
1 parent 5cef1de commit 96e7113

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

src/Exceptions/CouldNotSendNotification.php

-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,4 @@ public static function invalidReceiver(): self
3030
method or a phone_number attribute to your notifiable.'
3131
);
3232
}
33-
34-
public static function missingAlphaNumericSender(): self
35-
{
36-
return new static(
37-
'Notification was not sent. Missing `alphanumeric_sender` in config'
38-
);
39-
}
4033
}

tests/Integration/TwilioProviderTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ public function it_can_create_the_application_with_sid()
4141
$this->assertInstanceOf(TwilioChannel::class, $this->app->get(TwilioChannel::class));
4242
}
4343

44+
/** @test */
45+
public function it_can_create_the_application_with_token_auth()
46+
{
47+
$this->app['config']->set('twilio-notification-channel.auth_token', 'token');
48+
$this->app['config']->set('twilio-notification-channel.account_sid', '1234');
49+
50+
$this->assertInstanceOf(TwilioChannel::class, $this->app->get(TwilioChannel::class));
51+
}
52+
4453
/** @test */
4554
public function it_provides_three_classes()
4655
{

tests/Unit/IntegrationTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace NotificationChannels\Twilio\Tests\Unit;
44

55
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Notifications\Events\NotificationFailed;
67
use Illuminate\Notifications\Notification;
78
use Mockery;
89
use Mockery\Adapter\Phpunit\MockeryTestCase;
10+
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
911
use NotificationChannels\Twilio\Twilio;
1012
use NotificationChannels\Twilio\TwilioCallMessage;
1113
use NotificationChannels\Twilio\TwilioChannel;
@@ -28,7 +30,7 @@ class IntegrationTest extends MockeryTestCase
2830
/** @var Dispatcher */
2931
protected $events;
3032

31-
public function setUp(): void
33+
protected function setUp(): void
3234
{
3335
parent::setUp();
3436

@@ -144,6 +146,27 @@ public function it_can_make_a_call()
144146
$channel->send(new NotifiableWithAttribute, $this->notification);
145147
}
146148

149+
/** @test */
150+
public function it_cant_make_a_call_when_the_from_config_is_missing()
151+
{
152+
$message = TwilioCallMessage::create('http://example.com');
153+
$this->notification->shouldReceive('toTwilio')->andReturn($message);
154+
155+
$config = new TwilioConfig([]);
156+
$twilio = new Twilio($this->twilioService, $config);
157+
$channel = new TwilioChannel($twilio, $this->events);
158+
159+
$this->twilioService->calls->shouldNotReceive('create');
160+
161+
$this->events->shouldReceive('dispatch')
162+
->atLeast()->once()
163+
->with(Mockery::type(NotificationFailed::class));
164+
165+
$this->expectException(CouldNotSendNotification::class);
166+
167+
$channel->send(new NotifiableWithAttribute, $this->notification);
168+
}
169+
147170
protected function smsMessageWillBeSentToTwilioWith(...$args)
148171
{
149172
$this->twilioService->messages->shouldReceive('create')

tests/Unit/TwilioChannelTest.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TwilioChannelTest extends MockeryTestCase
2626
/** @var Dispatcher */
2727
protected $dispatcher;
2828

29-
public function setUp(): void
29+
protected function setUp(): void
3030
{
3131
parent::setUp();
3232

@@ -91,6 +91,22 @@ public function it_will_send_a_sms_message_to_the_result_of_the_route_method_of_
9191
$this->channel->send($notifiable, $notification);
9292
}
9393

94+
/** @test */
95+
public function it_will_send_a_sms_message_to_the_result_of_the_route_method_of_the_notifiable_if_it_uses_the_twilio_channel_explicitly()
96+
{
97+
$notifiable = new NotifiableWithTwilioChannel;
98+
$notification = Mockery::mock(Notification::class);
99+
100+
$message = new TwilioSmsMessage('Message text');
101+
$notification->shouldReceive('toTwilio')->andReturn($message);
102+
103+
$this->twilio->shouldReceive('sendMessage')
104+
->atLeast()->once()
105+
->with($message, '+1111111111', false);
106+
107+
$this->channel->send($notifiable, $notification);
108+
}
109+
94110
/** @test */
95111
public function it_will_make_a_call_to_the_phone_number_attribute_of_the_notifiable()
96112
{
@@ -240,6 +256,16 @@ class Notifiable
240256
public function routeNotificationFor() {}
241257
}
242258

259+
class NotifiableWithTwilioChannel
260+
{
261+
public function routeNotificationFor(string $channel)
262+
{
263+
if ($channel === 'twilio') {
264+
return '+1111111111';
265+
}
266+
}
267+
}
268+
243269
class NotifiableWithMethod
244270
{
245271
public function routeNotificationFor()

tests/Unit/TwilioSmsMessageTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class TwilioSmsMessageTest extends TwilioMessageTestCase
88
{
9-
public function setUp(): void
9+
protected function setUp(): void
1010
{
1111
parent::setUp();
1212

@@ -47,6 +47,15 @@ public function it_can_return_the_alphanumeric_sender_if_set()
4747
$this->assertEquals('TestSender', $message->getFrom());
4848
}
4949

50+
/** @test */
51+
public function it_can_return_the_messaging_service_sid_if_set()
52+
{
53+
$message = TwilioSmsMessage::create('myMessage');
54+
$message->messagingServiceSid('TestSid');
55+
56+
$this->assertEquals('TestSid', $message->getMessagingServiceSid());
57+
}
58+
5059
/** @test */
5160
public function it_can_set_optional_parameters()
5261
{

0 commit comments

Comments
 (0)