Skip to content

Commit

Permalink
Merge pull request #2 from Mane-Olawale/development
Browse files Browse the repository at this point in the history
[ADDITION: Tests] Added channel tests and test entities
  • Loading branch information
Mane-Olawale authored Jun 12, 2021
2 parents 59d166b + 62294d4 commit 603e618
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 16 deletions.
14 changes: 3 additions & 11 deletions src/Channels/TermiiSmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use ManeOlawale\Laravel\Termii\Messages\TermiiMessage;
use Illuminate\Notifications\Notification;
use ManeOlawale\Laravel\Termii\Termii;
use ManeOlawale\Termii\Client;

class TermiiSmsChannel
Expand All @@ -30,7 +29,7 @@ class TermiiSmsChannel
* @param string $from
* @return void
*/
public function __construct( Termii $termii, string $from)
public function __construct( Client $termii, string $from)
{
$this->from = $from;
$this->termii = $termii;
Expand All @@ -55,16 +54,9 @@ public function send($notifiable, Notification $notification)
$message = new TermiiMessage($message);
}

if ($message->client){
$client = $this->termii->client();
$this->termii->usingClient($message->client);
}

$result = $this->termii->send($to, $message->getContent(), $message->from, $message->channel);
$client = ($message->client instanceof Client)? $message->client : $this->termii;

if ($message->client){
$this->termii->usingClient($client);
}
$result = $client->sms->send($to, $message->getContent(), $message->from ?? $this->from, $message->channel);

return $result;

Expand Down
2 changes: 1 addition & 1 deletion src/Messages/TermiiMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getContent() : string
{
$lines = (($this->content)? "\n" : "").implode( "\n",$this->lines);

return $this->content.$lines;
return trim($this->content.$lines);
}

}
16 changes: 12 additions & 4 deletions src/TermiiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Foundation\Application;
use ManeOlawale\Termii\Client as TermiiClient;

class TermiiServiceProvider extends ServiceProvider
Expand All @@ -17,17 +18,24 @@ class TermiiServiceProvider extends ServiceProvider
public function register()
{

$this->app->singleton( Termii::class, function (){

return new Termii( new TermiiClient( config('termii.key'), $this->getOptions()) ) ;
$this->app->bind( TermiiClient::class, function (){

return new TermiiClient( config('termii.key'), $this->getOptions());

});

$this->app->singleton( Termii::class, function ( Application $app){

return new Termii( $app->make(TermiiClient::class) ) ;

});


Notification::resolved(function (ChannelManager $service) {
$service->extend('termii', function ($app) {
$service->extend('termii', function ( Application $app) {
return new Channels\TermiiSmsChannel(
$app->make(Termii::class),
$app->make(TermiiClient::class),
config('termii.sender_id')
);
});
Expand Down
136 changes: 136 additions & 0 deletions tests/Channel/TermiiChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests\Channel;

use Mockery as Mock;
use ManeOlawale\Laravel\Termii\Tests\TestCase;
use ManeOlawale\Laravel\Termii\Tests\Entities\TermiiTestNotification;
use ManeOlawale\Laravel\Termii\Tests\Entities\TermiiTestNotificationWithCustomClient;
use ManeOlawale\Laravel\Termii\Tests\Entities\TermiiTestNotificationWithCustomClientAndFrom;
use ManeOlawale\Laravel\Termii\Tests\Entities\TermiiTestNotificationWithCustomFrom;
use ManeOlawale\Laravel\Termii\Tests\Entities\TermiiTestNotifiable;
use ManeOlawale\Laravel\Termii\Channels\TermiiSmsChannel;
use ManeOlawale\Termii\Client;
use ManeOlawale\Termii\Api\Sms;

class TermiiChannelTest extends TestCase
{
public function test_sms_is_sent_via_termii()
{
$notification = new TermiiTestNotification;
$notifiable = new TermiiTestNotifiable;


$channel = new TermiiSmsChannel(
$termii = Mock::mock(Client::class), 'Olawale'
);

$sms = Mock::mock(Sms::class);

$sms->shouldReceive('send')
->with(
$notifiable->phone, 'Hello world', 'Olawale', NULL
)
->once();

$termii->shouldReceive('api')
->with(
'sms'
)
->once()
->andReturn($sms);

$channel->send($notifiable, $notification);
}

public function test_sms_is_sent_via_custom_client()
{

$customClient = Mock::mock(Client::class);

$sms = Mock::mock(Sms::class);

$notification = new TermiiTestNotificationWithCustomClient($customClient);
$notifiable = new TermiiTestNotifiable;

$sms->shouldReceive('send')
->with(
$notifiable->phone, 'Hello world', 'Olawale', NULL
)
->once();

$customClient->shouldReceive('api')
->with(
'sms'
)
->once()
->andReturn($sms);

$channel = new TermiiSmsChannel(
Mock::mock(Client::class), 'Olawale'
);

$channel->send($notifiable, $notification);
}

public function test_sms_is_sent_via_custom_from()
{
$notification = new TermiiTestNotificationWithCustomFrom;
$notifiable = new TermiiTestNotifiable;


$channel = new TermiiSmsChannel(
$termii = Mock::mock(Client::class), 'Olawale'
);

$sms = Mock::mock(Sms::class);

$sms->shouldReceive('send')
->with(
$notifiable->phone, 'Hello world', 'Adedotun', NULL
)
->once();

$termii->shouldReceive('api')
->with(
'sms'
)
->once()
->andReturn($sms);

$channel->send($notifiable, $notification);
}

public function test_sms_is_sent_via_custom_from_and_client()
{

$customClient = Mock::mock(Client::class);

$sms = Mock::mock(Sms::class);

$notification = new TermiiTestNotificationWithCustomClientAndFrom(
$customClient
);
$notifiable = new TermiiTestNotifiable;

$sms->shouldReceive('send')
->with(
$notifiable->phone, 'Hello world', 'Adedotun', NULL
)
->once();

$customClient->shouldReceive('api')
->with(
'sms'
)
->once()
->andReturn($sms);

$channel = new TermiiSmsChannel(
Mock::mock(Client::class), 'Olawale'
);

$channel->send($notifiable, $notification);
}

}
18 changes: 18 additions & 0 deletions tests/Entities/TermiiTestNotifiable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests\Entities;

use Illuminate\Notifications\Notifiable;

class TermiiTestNotifiable
{
use Notifiable;

public $phone = '5555555555';

public function routeNotificationForTermii($notification)
{
return $this->phone;
}

}
14 changes: 14 additions & 0 deletions tests/Entities/TermiiTestNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests\Entities;

use Illuminate\Notifications\Notification;
use ManeOlawale\Laravel\Termii\Messages\TermiiMessage;

class TermiiTestNotification extends Notification
{
public function toTermii($notifiable)
{
return new TermiiMessage('Hello world');
}
}
22 changes: 22 additions & 0 deletions tests/Entities/TermiiTestNotificationWithCustomClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests\Entities;

use ManeOlawale\Termii\Client;
use Illuminate\Notifications\Notification;
use ManeOlawale\Laravel\Termii\Messages\TermiiMessage;

class TermiiTestNotificationWithCustomClient extends Notification
{
public $client;

public function __construct(Client $client) {
$this->client = $client;
}


public function toTermii($notifiable)
{
return (new TermiiMessage('Hello world'))->client($this->client);
}
}
22 changes: 22 additions & 0 deletions tests/Entities/TermiiTestNotificationWithCustomClientAndFrom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests\Entities;

use ManeOlawale\Termii\Client;
use Illuminate\Notifications\Notification;
use ManeOlawale\Laravel\Termii\Messages\TermiiMessage;

class TermiiTestNotificationWithCustomClientAndFrom extends Notification
{
public $client;

public function __construct(Client $client) {
$this->client = $client;
}


public function toTermii($notifiable)
{
return (new TermiiMessage('Hello world'))->client($this->client)->from('Adedotun');
}
}
16 changes: 16 additions & 0 deletions tests/Entities/TermiiTestNotificationWithCustomFrom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests\Entities;

use ManeOlawale\Termii\Client;
use Illuminate\Notifications\Notification;
use ManeOlawale\Laravel\Termii\Messages\TermiiMessage;

class TermiiTestNotificationWithCustomFrom extends Notification
{

public function toTermii($notifiable)
{
return (new TermiiMessage('Hello world'))->from('Adedotun');
}
}
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ManeOlawale\Laravel\Termii\Tests;

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
use MockeryPHPUnitIntegration;
}

0 comments on commit 603e618

Please sign in to comment.