From f8d47d2b298a72d28ed0e3da4636303ae70abe0a Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Fri, 2 Jul 2021 01:12:00 +0100 Subject: [PATCH 1/9] [REFACTOR] refactor and format some codes for readability sake --- src/TermiiServiceProvider.php | 42 ++++++----------- tests/Channel/TermiiChannelTest.php | 47 ++++++++++++------- tests/Entities/TermiiTestNotifiable.php | 1 - tests/Entities/TermiiTestNotification.php | 2 +- ...TermiiTestNotificationWithCustomClient.php | 5 +- ...estNotificationWithCustomClientAndFrom.php | 5 +- .../TermiiTestNotificationWithCustomFrom.php | 3 +- 7 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/TermiiServiceProvider.php b/src/TermiiServiceProvider.php index d34bc77..e7b58d0 100644 --- a/src/TermiiServiceProvider.php +++ b/src/TermiiServiceProvider.php @@ -17,26 +17,19 @@ class TermiiServiceProvider extends ServiceProvider */ public function register() { - - - $this->app->bind( TermiiClient::class, function (){ - - return 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) ) ; - + $this->app->singleton(Termii::class, function (Application $app) { + return new Termii($app->make(TermiiClient::class)) ; }); - Notification::resolved(function (ChannelManager $service) { $service->extend('termii', function ( Application $app) { return new Channels\TermiiSmsChannel( $app->make(TermiiClient::class), - config('termii.sender_id') + \config('termii.sender_id') ); }); }); @@ -50,13 +43,10 @@ public function boot() public function addPublishes() { - $this->publishes([ - - __DIR__.'/../config/termii.php' => config_path('termii.php') + __DIR__ . '/../config/termii.php' => \config_path('termii.php') ], 'termii.config'); - } protected function addCommands() @@ -64,9 +54,7 @@ protected function addCommands() // Console only commands if ($this->app->runningInConsole()) { $this->commands([ - Commands\InstallCommand::class, - ]); } } @@ -74,15 +62,15 @@ protected function addCommands() public function getOptions() { return [ - 'sender_id' => config('termii.sender_id'), - 'channel' => config('termii.channel'), - "attempts" => config('termii.pin.attempts'), - "time_to_live" => config('termii.pin.time_to_live'), - "length" => config('termii.pin.length'), - "placeholder" => config('termii.pin.placeholder'), - 'pin_type' => config('termii.pin.type'), - 'message_type' => config('termii.message_type'), - 'type' => config('termii.type'), + 'sender_id' => \config('termii.sender_id'), + 'channel' => \config('termii.channel'), + "attempts" => \config('termii.pin.attempts'), + "time_to_live" => \config('termii.pin.time_to_live'), + "length" => \config('termii.pin.length'), + "placeholder" => \config('termii.pin.placeholder'), + 'pin_type' => \config('termii.pin.type'), + 'message_type' => \config('termii.message_type'), + 'type' => \config('termii.type'), ]; } } diff --git a/tests/Channel/TermiiChannelTest.php b/tests/Channel/TermiiChannelTest.php index e442015..a3b2dd6 100644 --- a/tests/Channel/TermiiChannelTest.php +++ b/tests/Channel/TermiiChannelTest.php @@ -15,21 +15,25 @@ class TermiiChannelTest extends TestCase { - public function test_sms_is_sent_via_termii() + public function testSmsIsSentViaTermii() { - $notification = new TermiiTestNotification; - $notifiable = new TermiiTestNotifiable; + $notification = new TermiiTestNotification(); + $notifiable = new TermiiTestNotifiable(); $channel = new TermiiSmsChannel( - $termii = Mock::mock(Client::class), 'Olawale' + $termii = Mock::mock(Client::class), + 'Olawale' ); $sms = Mock::mock(Sms::class); $sms->shouldReceive('send') ->with( - $notifiable->phone, 'Hello world', 'Olawale', NULL + $notifiable->phone, + 'Hello world', + 'Olawale', + null ) ->once(); @@ -43,7 +47,7 @@ public function test_sms_is_sent_via_termii() $channel->send($notifiable, $notification); } - public function test_sms_is_sent_via_custom_client() + public function testSmsIsSentViaCustomClient() { $customClient = Mock::mock(Client::class); @@ -51,11 +55,14 @@ public function test_sms_is_sent_via_custom_client() $sms = Mock::mock(Sms::class); $notification = new TermiiTestNotificationWithCustomClient($customClient); - $notifiable = new TermiiTestNotifiable; + $notifiable = new TermiiTestNotifiable(); $sms->shouldReceive('send') ->with( - $notifiable->phone, 'Hello world', 'Olawale', NULL + $notifiable->phone, + 'Hello world', + 'Olawale', + null ) ->once(); @@ -67,20 +74,22 @@ public function test_sms_is_sent_via_custom_client() ->andReturn($sms); $channel = new TermiiSmsChannel( - Mock::mock(Client::class), 'Olawale' + Mock::mock(Client::class), + 'Olawale' ); $channel->send($notifiable, $notification); } - public function test_sms_is_sent_via_custom_from() + public function testSmsIsSentViaCustomFrom() { - $notification = new TermiiTestNotificationWithCustomFrom; - $notifiable = new TermiiTestNotifiable; + $notification = new TermiiTestNotificationWithCustomFrom(); + $notifiable = new TermiiTestNotifiable(); $channel = new TermiiSmsChannel( - $termii = Mock::mock(Client::class), 'Olawale' + $termii = Mock::mock(Client::class), + 'Olawale' ); $sms = Mock::mock(Sms::class); @@ -101,7 +110,7 @@ public function test_sms_is_sent_via_custom_from() $channel->send($notifiable, $notification); } - public function test_sms_is_sent_via_custom_from_and_client() + public function testSmsIsSentViaCustomFromAndClient() { $customClient = Mock::mock(Client::class); @@ -111,11 +120,14 @@ public function test_sms_is_sent_via_custom_from_and_client() $notification = new TermiiTestNotificationWithCustomClientAndFrom( $customClient ); - $notifiable = new TermiiTestNotifiable; + $notifiable = new TermiiTestNotifiable(); $sms->shouldReceive('send') ->with( - $notifiable->phone, 'Hello world', 'Adedotun', NULL + $notifiable->phone, + 'Hello world', + 'Adedotun', + null ) ->once(); @@ -132,5 +144,4 @@ public function test_sms_is_sent_via_custom_from_and_client() $channel->send($notifiable, $notification); } - -} \ No newline at end of file +} diff --git a/tests/Entities/TermiiTestNotifiable.php b/tests/Entities/TermiiTestNotifiable.php index b1fc715..a516b46 100644 --- a/tests/Entities/TermiiTestNotifiable.php +++ b/tests/Entities/TermiiTestNotifiable.php @@ -14,5 +14,4 @@ public function routeNotificationForTermii($notification) { return $this->phone; } - } diff --git a/tests/Entities/TermiiTestNotification.php b/tests/Entities/TermiiTestNotification.php index 5cd9052..f1f9136 100644 --- a/tests/Entities/TermiiTestNotification.php +++ b/tests/Entities/TermiiTestNotification.php @@ -11,4 +11,4 @@ public function toTermii($notifiable) { return new TermiiMessage('Hello world'); } -} \ No newline at end of file +} diff --git a/tests/Entities/TermiiTestNotificationWithCustomClient.php b/tests/Entities/TermiiTestNotificationWithCustomClient.php index edfc543..a4ca40b 100644 --- a/tests/Entities/TermiiTestNotificationWithCustomClient.php +++ b/tests/Entities/TermiiTestNotificationWithCustomClient.php @@ -10,7 +10,8 @@ class TermiiTestNotificationWithCustomClient extends Notification { public $client; - public function __construct(Client $client) { + public function __construct(Client $client) + { $this->client = $client; } @@ -19,4 +20,4 @@ public function toTermii($notifiable) { return (new TermiiMessage('Hello world'))->client($this->client); } -} \ No newline at end of file +} diff --git a/tests/Entities/TermiiTestNotificationWithCustomClientAndFrom.php b/tests/Entities/TermiiTestNotificationWithCustomClientAndFrom.php index 4d8f0de..8e3c9fb 100644 --- a/tests/Entities/TermiiTestNotificationWithCustomClientAndFrom.php +++ b/tests/Entities/TermiiTestNotificationWithCustomClientAndFrom.php @@ -10,7 +10,8 @@ class TermiiTestNotificationWithCustomClientAndFrom extends Notification { public $client; - public function __construct(Client $client) { + public function __construct(Client $client) + { $this->client = $client; } @@ -19,4 +20,4 @@ public function toTermii($notifiable) { return (new TermiiMessage('Hello world'))->client($this->client)->from('Adedotun'); } -} \ No newline at end of file +} diff --git a/tests/Entities/TermiiTestNotificationWithCustomFrom.php b/tests/Entities/TermiiTestNotificationWithCustomFrom.php index 2632a11..829f370 100644 --- a/tests/Entities/TermiiTestNotificationWithCustomFrom.php +++ b/tests/Entities/TermiiTestNotificationWithCustomFrom.php @@ -2,7 +2,6 @@ namespace ManeOlawale\Laravel\Termii\Tests\Entities; -use ManeOlawale\Termii\Client; use Illuminate\Notifications\Notification; use ManeOlawale\Laravel\Termii\Messages\TermiiMessage; @@ -13,4 +12,4 @@ public function toTermii($notifiable) { return (new TermiiMessage('Hello world'))->from('Adedotun'); } -} \ No newline at end of file +} From f9409b98767e1fc09c0618f5bbf51df86fa02e56 Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Fri, 2 Jul 2021 10:51:03 +0100 Subject: [PATCH 2/9] [REFACTOR] refactor and format some codes for readability sake part 2 --- config/termii.php | 2 +- src/Channels/TermiiSmsChannel.php | 6 +- src/Commands/InstallCommand.php | 16 +--- src/Entities/Token.php | 154 +++++++++++------------------- src/Messages/Message.php | 33 ++++--- src/Messages/TermiiMessage.php | 18 ++-- src/TermiiServiceProvider.php | 50 +++++++--- 7 files changed, 126 insertions(+), 153 deletions(-) diff --git a/config/termii.php b/config/termii.php index 13ce160..fa53e1f 100644 --- a/config/termii.php +++ b/config/termii.php @@ -24,7 +24,7 @@ * Sms Name for Termii message * */ - "sms_name" => env('TERMII_SMS_NAME', env('APP_NAME','Termii')), + "sms_name" => env('TERMII_SMS_NAME', env('APP_NAME', 'Termii')), /** * User agent for Termii message diff --git a/src/Channels/TermiiSmsChannel.php b/src/Channels/TermiiSmsChannel.php index 1c3d297..018848c 100644 --- a/src/Channels/TermiiSmsChannel.php +++ b/src/Channels/TermiiSmsChannel.php @@ -29,7 +29,7 @@ class TermiiSmsChannel * @param string $from * @return void */ - public function __construct( Client $termii, string $from) + public function __construct(Client $termii, string $from) { $this->from = $from; $this->termii = $termii; @@ -54,12 +54,10 @@ public function send($notifiable, Notification $notification) $message = new TermiiMessage($message); } - $client = ($message->client instanceof Client)? $message->client : $this->termii; + $client = ($message->client instanceof Client) ? $message->client : $this->termii; $result = $client->sms->send($to, $message->getContent(), $message->from ?? $this->from, $message->channel); return $result; - } - } diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index c77d293..98aa67c 100755 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -1,4 +1,4 @@ -call('vendor:publish', [ - '--tag' => 'termii.config' - ]); $this->line(""); sleep(2); - $this->line(" Termii installed sucessfully!!"); - - } - -} \ No newline at end of file +} diff --git a/src/Entities/Token.php b/src/Entities/Token.php index 6ef5e3f..f9a12ff 100644 --- a/src/Entities/Token.php +++ b/src/Entities/Token.php @@ -1,10 +1,12 @@ -key = $key; - if ($signature){ + if ($signature) { $this->loadFromSignature($signature); - } else if ( ($session = $this->getRequest()->session()) ) { + } elseif (($session = $this->getRequest()->session())) { $this->loadFromSession($session, $key); } } @@ -102,7 +104,7 @@ public function getRequest() /** * Fetch the payload from an encrypted string - * + * * @param string @signature * @return void */ @@ -115,94 +117,76 @@ protected function loadFromSignature(string $signature) } $this->signature = $signature; - $this->payload = json_decode($json, true) ?? []; - $this->updateProperties(); - - $this->loaded = ($this->payload)? true : false; + $this->loaded = ($this->payload) ? true : false; } /** * Fetch the payload from session - * + * * @param string @key * @return void */ protected function loadFromSession($session, string $key) { - $this->payload = json_decode($session->get($key), true) ?? []; - $this->updateProperties(); - - $this->loaded = ($this->payload)? true : false; + $this->loaded = ($this->payload) ? true : false; } /** * Flush all the content and session of the instance * left only with phone number, key and text - * + * * @return self $this; */ public function flush() { $this->pin_id = null; - $this->pin = null; - $this->expires_at = null; - $this->generated_at = null; - $this->loaded = false; - $this->signature = null; - $this->payload = []; - if ( ($session = $this->getRequest()->session())) { + if (($session = $this->getRequest()->session())) { $session->forget($this->key); } - + return $this; } /** * Update the instance properties from the payload - * + * * @return void */ protected function updateProperties() { - if (!$this->payload){ + if (!$this->payload) { return false; } $this->key = $this->payload['key']; - $this->pin_id = $this->payload['pin_id']; - $this->pin = $this->payload['pin'] ?? null; - $this->expires_at = Date::parse($this->payload['expires_at']); - $this->generated_at = Date::parse($this->payload['generated_at']); - $this->phonenumber = $this->payload['phonenumber']; - $this->in_app = $this->payload['in_app']; } /** * Make the token an in-app token, Only for non-loaded instance of this class - * + * * @return self $this */ public function inApp() { - if ($this->loaded){ + if ($this->loaded) { return $this; } @@ -213,54 +197,52 @@ public function inApp() /** * Set the phonenumber of the token - * + * * @return self $this */ public function to(string $phonenumber) { - if ($this->loaded){ + if ($this->loaded) { return $this; } $this->phonenumber = $phonenumber; - return $this; } /** * Set the text of the token - * + * * @return self $this */ public function text(string $text) { - if ($this->loaded){ + if ($this->loaded) { return $this; } $this->text = $text; - return $this; } /** * Dynamic function calls to set the pin option - * + * * @return self $this */ public function __call(string $name, array $parameters) { - $this->pin_options[$name] = count($parameters)? $parameters[0] : true; + $this->pin_options[$name] = count($parameters) ? $parameters[0] : true; } /** * Get the pin id - * + * * @return string */ public function id() { - if ($this->loaded){ + if ($this->loaded) { return false; } return $this->pin_id; @@ -268,7 +250,7 @@ public function id() /** * Get the key of the instance - * + * * @return string */ public function key() @@ -277,12 +259,12 @@ public function key() } /** - * Get the pin, Only for in-app tokens * + * Get the pin, Only for in-app tokens * @return string */ public function pin() { - if ($this->loaded){ + if ($this->loaded) { return false; } return $this->pin; @@ -290,7 +272,7 @@ public function pin() /** * Get the signature - * * + * * @return string */ public function signature() @@ -300,12 +282,12 @@ public function signature() /** * This generate a new token resourse from termii - * + * * @param array $options */ - public function start( array $options = []) + public function start(array $options = []) { - if ($this->loaded){ + if ($this->loaded) { return false; } @@ -313,102 +295,78 @@ public function start( array $options = []) $options = $this->pin_options + $options; - if ($this->in_app){ + if ($this->in_app) { $data = $token->sendInAppToken($this->phonenumber, $options); } else { $data = $token->sendToken($this->phonenumber, $this->text, $options); } - $this->payload['key'] = $this->key; - if ($this->in_app){ - + if ($this->in_app) { $this->payload['pin_id'] = $data['data']['pin_id']; - $this->payload['pin'] = $data['data']['otp']; - $this->payload['phonenumber'] = $data['data']['phone_number']; - - }else{ - + } else { $this->payload['pin_id'] = $data['pinId']; - $this->payload['pin'] = null; - $this->payload['phonenumber'] = $data['to']; - } - $this->payload['expires_at'] = (string)Date::now()->addMinutes($options['time_to_live'] ?? config('pin.time_to_live')); - + $this->payload['expires_at'] = (string)Date::now()->addMinutes($options['time_to_live'] ?? + Config::get('pin.time_to_live')); $this->payload['generated_at'] = (string)Date::now(); - $this->payload['in_app'] = $this->in_app; - - $this->updateProperties(); - $this->loaded = true; - if ( ($session = $this->getRequest()->session())) { + if (($session = $this->getRequest()->session())) { $session->put($this->key, json_encode($this->payload)); } - - return $this; } /** - * Change if a + * Check if the token is valid + * + * @return bool */ public function isValid() { - if (!$this->loaded) return false; + if (!$this->loaded) { + return false; + } return $this->pin_id && $this->expires_at > Date::now(); } /** * Verify the token - * + * * @param string */ public function verify(string $pin) { - if (!is_int($pin) && !is_string($pin)){ + if (!is_int($pin) && !is_string($pin)) { return false; } - if ($this->in_app){ - + if ($this->in_app) { return ($this->pin === $pin); - - }else{ - + } else { $token = Termii::token(); - - return $token->verified($this->pin_id, $pin);; + return $token->verified($this->pin_id, $pin); } } - public function __serialize(): array - { - return $this->payload; - } - - public function __unserialize(array $data): void - { - $this->payload = $data; - - $this->updateProperties(); - } - + /** + * Cast the token to a string signature + * + * @return string + */ public function __toString() { - return json_encode($this->payload); + return $this->signature(); } - - } diff --git a/src/Messages/Message.php b/src/Messages/Message.php index 1d67daf..ce40104 100644 --- a/src/Messages/Message.php +++ b/src/Messages/Message.php @@ -47,7 +47,7 @@ abstract class Message * @param string $content * @return void */ - public function __construct( string $content = '') + public function __construct(string $content = '') { $this->content = $content; } @@ -56,9 +56,9 @@ public function __construct( string $content = '') * Set the message content. * * @param string $content - * @return $this + * @return self */ - public function content( string $content) + public function content(string $content): self { $this->content = $content; @@ -69,9 +69,9 @@ public function content( string $content) * Set the sender id, Device id or phone number the message should be sent from. * * @param string $from - * @return $this + * @return self */ - public function from($from) + public function from($from): self { $this->from = $from; @@ -82,9 +82,9 @@ public function from($from) * Set the message channel. * * @param string $from - * @return $this + * @return self */ - public function channel( string $channel) + public function channel(string $channel): self { $this->channel = $channel; @@ -94,9 +94,9 @@ public function channel( string $channel) /** * Set the message type. * - * @return $this + * @return self */ - public function unicode() + public function unicode(): self { $this->type('unicode'); @@ -107,9 +107,9 @@ public function unicode() * Set the message type. * * @param string $type - * @return $this + * @return self */ - public function type(string $type) + public function type(string $type): self { $this->type = $type; @@ -120,16 +120,21 @@ public function type(string $type) * Set the Termii client instance. * * @param \ManeOlawale\Termii\Client $client - * @return $this + * @return self */ - public function client(Client $client) + public function client(Client $client): self { $this->client = $client; return $this; } - public function getContent() : string + /** + * Get the text content of the message + * + * @return string + */ + public function getContent(): string { return $this->content; } diff --git a/src/Messages/TermiiMessage.php b/src/Messages/TermiiMessage.php index 849e36d..eef7c8f 100644 --- a/src/Messages/TermiiMessage.php +++ b/src/Messages/TermiiMessage.php @@ -16,20 +16,22 @@ class TermiiMessage extends Message * Add a line of text to the message content. * * @param string $text - * @return $this + * @return self */ - public function line( string $text = null) + public function line(string $text = null): self { $this->lines[] = $text; - return $this; } - public function getContent() : string + /** + * Get the text content of the message + * + * @return string + */ + public function getContent(): string { - $lines = (($this->content)? "\n" : "").implode( "\n",$this->lines); - - return trim($this->content.$lines); + $lines = (($this->content) ? "\n" : "") . implode("\n", $this->lines); + return trim($this->content . $lines); } - } diff --git a/src/TermiiServiceProvider.php b/src/TermiiServiceProvider.php index e7b58d0..b6ce64f 100644 --- a/src/TermiiServiceProvider.php +++ b/src/TermiiServiceProvider.php @@ -6,6 +6,8 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Notifications\ChannelManager; use Illuminate\Foundation\Application; +use Illuminate\Support\Facades\App; +use Illuminate\Support\Facades\Config; use ManeOlawale\Termii\Client as TermiiClient; class TermiiServiceProvider extends ServiceProvider @@ -18,7 +20,7 @@ class TermiiServiceProvider extends ServiceProvider public function register() { $this->app->bind(TermiiClient::class, function () { - return new TermiiClient(\config('termii.key'), $this->getOptions()); + return new TermiiClient(Config::get('termii.key'), $this->getOptions()); }); $this->app->singleton(Termii::class, function (Application $app) { @@ -26,29 +28,44 @@ public function register() }); Notification::resolved(function (ChannelManager $service) { - $service->extend('termii', function ( Application $app) { + $service->extend('termii', function (Application $app) { return new Channels\TermiiSmsChannel( $app->make(TermiiClient::class), - \config('termii.sender_id') + Config::get('termii.sender_id') ); }); }); } + /** + * Boot the provider + * + * @return void + */ public function boot() { $this->addPublishes(); $this->addCommands(); } + /** + * Register publishable assets + * + * @return void + */ public function addPublishes() { $this->publishes([ - __DIR__ . '/../config/termii.php' => \config_path('termii.php') + __DIR__ . '/../config/termii.php' => App::configPath('termii.php') ], 'termii.config'); } + /** + * Add termii commands + * + * @return void + */ protected function addCommands() { // Console only commands @@ -59,18 +76,23 @@ protected function addCommands() } } - public function getOptions() + /** + * Get array of options from the config + * + * @return array + */ + public function getOptions(): array { return [ - 'sender_id' => \config('termii.sender_id'), - 'channel' => \config('termii.channel'), - "attempts" => \config('termii.pin.attempts'), - "time_to_live" => \config('termii.pin.time_to_live'), - "length" => \config('termii.pin.length'), - "placeholder" => \config('termii.pin.placeholder'), - 'pin_type' => \config('termii.pin.type'), - 'message_type' => \config('termii.message_type'), - 'type' => \config('termii.type'), + 'sender_id' => Config::get('termii.sender_id'), + 'channel' => Config::get('termii.channel'), + "attempts" => Config::get('termii.pin.attempts'), + "time_to_live" => Config::get('termii.pin.time_to_live'), + "length" => Config::get('termii.pin.length'), + "placeholder" => Config::get('termii.pin.placeholder'), + 'pin_type' => Config::get('termii.pin.type'), + 'message_type' => Config::get('termii.message_type'), + 'type' => Config::get('termii.type'), ]; } } From 9172b07648c58898c882bf6f18bff76921df4ae3 Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 18:30:25 +0100 Subject: [PATCH 3/9] chore: add test and make them pass --- composer.json | 5 +- src/Entities/Token.php | 80 ++++++---- src/Termii.php | 2 +- tests/MockingTraits.php | 40 +++++ tests/TermiiTest.php | 323 ++++++++++++++++++++++++++++++++++++++++ tests/TestBench.php | 38 +++++ tests/TestCase.php | 1 + 7 files changed, 456 insertions(+), 33 deletions(-) create mode 100644 tests/MockingTraits.php create mode 100644 tests/TermiiTest.php create mode 100644 tests/TestBench.php diff --git a/composer.json b/composer.json index 6fbcbbf..63c5910 100755 --- a/composer.json +++ b/composer.json @@ -17,11 +17,12 @@ ], "require": { "php": ">=7.2", - "mane-olawale/termii": "^1.0", + "mane-olawale/termii": "^1.2", "illuminate/notifications": "^6.0|^7.0|^8.0", "illuminate/support": "^6.0|^7.0|^8.0" }, "require-dev": { + "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0", "phpunit/phpunit": "~8.0|~9.0", "mockery/mockery": "~1.4" }, @@ -36,7 +37,7 @@ } }, "scripts": { - "tests": "./vendor/bin/phpunit --verbose" + "test": "./vendor/bin/phpunit --verbose" }, "extra": { "laravel": { diff --git a/src/Entities/Token.php b/src/Entities/Token.php index f9a12ff..78fdbc6 100644 --- a/src/Entities/Token.php +++ b/src/Entities/Token.php @@ -7,15 +7,21 @@ use Illuminate\Support\Facades\Date; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Config; -use ManeOlawale\Laravel\Termii\Facades\Termii; +use ManeOlawale\Laravel\Termii\Termii; class Token { /** - * The key to referrence the token + * Instance of laravel termii + * @var \ManeOlawale\Laravel\Termii\Termii + */ + public $termii; + + /** + * The tag to referrence the token * @var string */ - public $key; + public $tag; /** * The token payload @@ -39,7 +45,7 @@ class Token * The pin itself, Only available after generation or resolving a token signature of in-app token * @var string */ - protected $pin; + public $pin; /** * The pin itself, Only available after generation or resolving a token signature of in-app token @@ -57,7 +63,7 @@ class Token * This flag indicates that the payload and properties are populated * @var boolean false */ - public $loaded = false; + protected $loaded = false; /** * The recieving phone number @@ -80,17 +86,18 @@ class Token /** * Create a new instance * - * @param string $key + * @param string $tag * @param string $signature */ - public function __construct(string $key, string $signature = null) + public function __construct(Termii $termii, string $tag, string $signature = null) { - $this->key = $key; + $this->termii = $termii; + $this->tag = $tag; if ($signature) { $this->loadFromSignature($signature); } elseif (($session = $this->getRequest()->session())) { - $this->loadFromSession($session, $key); + $this->loadFromSession($session, $tag); } } @@ -125,19 +132,19 @@ protected function loadFromSignature(string $signature) /** * Fetch the payload from session * - * @param string @key + * @param string $tag * @return void */ - protected function loadFromSession($session, string $key) + protected function loadFromSession($session, string $tag) { - $this->payload = json_decode($session->get($key), true) ?? []; + $this->payload = json_decode($session->get($tag), true) ?? []; $this->updateProperties(); $this->loaded = ($this->payload) ? true : false; } /** * Flush all the content and session of the instance - * left only with phone number, key and text + * left only with phone number, tag and text * * @return self $this; */ @@ -153,7 +160,7 @@ public function flush() $this->payload = []; if (($session = $this->getRequest()->session())) { - $session->forget($this->key); + $session->forget($this->tag); } return $this; @@ -170,7 +177,7 @@ protected function updateProperties() return false; } - $this->key = $this->payload['key']; + $this->tag = $this->payload['tag']; $this->pin_id = $this->payload['pin_id']; $this->pin = $this->payload['pin'] ?? null; $this->expires_at = Date::parse($this->payload['expires_at']); @@ -242,20 +249,30 @@ public function __call(string $name, array $parameters) */ public function id() { - if ($this->loaded) { + if (!$this->loaded) { return false; } return $this->pin_id; } /** - * Get the key of the instance + * Get the tag of the instance * * @return string */ - public function key() + public function tag() + { + return $this->tag; + } + + /** + * Check token instance is loaded + * + * @return bool + */ + public function isLoaded(): bool { - return $this->key; + return $this->loaded; } /** @@ -264,8 +281,8 @@ public function key() */ public function pin() { - if ($this->loaded) { - return false; + if (!$this->loaded || !$this->in_app) { + return null; } return $this->pin; } @@ -275,8 +292,12 @@ public function pin() * * @return string */ - public function signature() + public function signature(): string { + if (empty($this->payload)) { + return ''; + } + return $this->signature = Crypt::encryptString(json_encode($this->payload ?? [])); } @@ -291,9 +312,9 @@ public function start(array $options = []) return false; } - $token = Termii::token(); + $token = $this->termii->token(); - $options = $this->pin_options + $options; + $options = array_merge($this->pin_options, $options); if ($this->in_app) { $data = $token->sendInAppToken($this->phonenumber, $options); @@ -301,7 +322,7 @@ public function start(array $options = []) $data = $token->sendToken($this->phonenumber, $this->text, $options); } - $this->payload['key'] = $this->key; + $this->payload['tag'] = $this->tag; if ($this->in_app) { $this->payload['pin_id'] = $data['data']['pin_id']; @@ -321,7 +342,7 @@ public function start(array $options = []) $this->loaded = true; if (($session = $this->getRequest()->session())) { - $session->put($this->key, json_encode($this->payload)); + $session->put($this->tag, json_encode($this->payload)); } return $this; @@ -346,17 +367,16 @@ public function isValid() * * @param string */ - public function verify(string $pin) + public function validate(string $pin) { if (!is_int($pin) && !is_string($pin)) { return false; } if ($this->in_app) { - return ($this->pin === $pin); + return ($this->pin == $pin); } else { - $token = Termii::token(); - return $token->verified($this->pin_id, $pin); + return $this->termii->token()->verified($this->pin_id, $pin); } } diff --git a/src/Termii.php b/src/Termii.php index 9935000..f4bf67f 100644 --- a/src/Termii.php +++ b/src/Termii.php @@ -47,7 +47,7 @@ public function client() */ public function verify(string $key, string $signature = null) { - return new Entities\Token($key, $signature); + return new Entities\Token($this, $key, $signature); } /** diff --git a/tests/MockingTraits.php b/tests/MockingTraits.php new file mode 100644 index 0000000..9f638b2 --- /dev/null +++ b/tests/MockingTraits.php @@ -0,0 +1,40 @@ +app))->getOptions()); + + /** + * @var \GuzzleHttp\Client + */ + $mock = \Mockery::mock(Guzzle::class); + + $client->fillOptions([ + 'httpManager' => new GuzzleHttpManager($client, $mock), + ]); + + $mock->shouldReceive([ + 'request' => $response, + ]); + + return $client; + } +} diff --git a/tests/TermiiTest.php b/tests/TermiiTest.php new file mode 100644 index 0000000..11df968 --- /dev/null +++ b/tests/TermiiTest.php @@ -0,0 +1,323 @@ +getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode($data = [ + 'message_id' => '9122821270554876574', + 'message' => 'Successfully Sent', + 'balance' => 9, + 'user' => 'Peter Mcleish' + ]) + ))); + + $this->assertEquals($data, $termii->send('2347041945964', 'Lotus give me my phone', 'Olawale', 'generic')); + } + + public function testGetServices() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('The [insight] is not a valid Endpoint tag.'); + + $termii = new Termii($this->getClientWithMockedResponse()); + + $this->assertInstanceOf(Sms::class, $termii->sms()); + $this->assertInstanceOf(Sender::class, $termii->sender()); + $this->assertInstanceOf(Insights::class, $termii->insights()); + $this->assertInstanceOf(Token::class, $termii->token()); + $this->assertInstanceOf(Client::class, $termii->client()); + $termii->insight(); + } + + public function testChangeClient() + { + $termii = new Termii($old = new Client('{Your api key goes here}')); + + $termii->usingClient(new Client('{Your api key goes here}')); + + $this->assertNotTrue($old === $termii->client()); + } + + public function testVerify() + { + $termii = new Termii(new Client('{Your api key goes here}')); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify('forgot_password')); + $this->assertNotTrue($token->isLoaded()); + $this->assertEmpty($token->id()); + } + + public function testVerifyWithSession() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'expires_at' => now()->addMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => false, + ]; + + Session::put($payload['tag'], json_encode($payload)); + $termii = new Termii(new Client('{Your api key goes here}')); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertEmpty($token->pin()); + } + + public function testVerifyIsValidWithSession() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'expires_at' => now()->subMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => false, + ]; + + Session::put($payload['tag'], json_encode($payload)); + $termii = new Termii(new Client('{Your api key goes here}')); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertTrue($token->isLoaded()); + $this->assertNotTrue($token->isValid()); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertEmpty($token->pin()); + } + + public function testVerifyValidateInappWithSession() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'pin' => '123456', + 'expires_at' => now()->addMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => true, + ]; + + Session::put($payload['tag'], json_encode($payload)); + + $termii = new Termii($this->getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode([ + // + ]) + ))); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->validate($payload['pin'])); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertTrue($token->pin() === $payload['pin']); + } + + public function testVerifyValidateWithSession() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'expires_at' => now()->addMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => false, + ]; + + Session::put($payload['tag'], json_encode($payload)); + + $termii = new Termii($this->getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode([ + 'pinId' => 'c8dcd048-5e7f-4347-8c89-4470c3af0b', + 'verified' => true, + 'msisdn' => '2347041945964' + ]) + ))); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->validate('123456')); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertEmpty($token->pin()); + } + + public function testVerifyStartWithSession() + { + $termii = new Termii($this->getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode($data = [ + 'pinId' => 'c8dcd048-5e7f-4347-8c89-4470c3af0b', + 'to' => '2347041945964', + 'status' => 'Message Sent', + ]) + ))); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify('forgot_password')); + $token->to('2347041945964')->text('{pin} is your account activation code')->start(); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->id() === $data['pinId']); + $this->assertEmpty($token->pin()); + } + + public function testVerifyStartInappWithSession() + { + $termii = new Termii($this->getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode($data = [ + 'status' => 'success', + 'data' => [ + 'pin_id' => 'c8dcd048-5e7f-4347-8c89-4470c3af0b', + 'otp' => '123456', + 'phone_number' => '2347041945964', + 'phone_number_other' => 'Termii', + ] + ]) + ))); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify('forgot_password')); + $token->to('2347041945964')->text('{pin} is your account activation code')->inApp()->start(); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->id() === $data['data']['pin_id']); + $this->assertTrue($token->pin() === $data['data']['otp']); + } + + + /* ---------- SIGNATURE ---------- */ + + + public function testVerifyWithSignature() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'expires_at' => now()->addMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => false, + ]; + + $sig = Crypt::encryptString(json_encode($payload)); + $termii = new Termii(new Client('{Your api key goes here}')); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertEmpty($token->pin()); + } + + public function testVerifyIsValidWithSignature() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'expires_at' => now()->subMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => false, + ]; + + $sig = Crypt::encryptString(json_encode($payload)); + $termii = new Termii(new Client('{Your api key goes here}')); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertTrue($token->isLoaded()); + $this->assertNotTrue($token->isValid()); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertEmpty($token->pin()); + } + + public function testVerifyValidateInappWithSignature() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'pin' => '123456', + 'expires_at' => now()->addMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => true, + ]; + + $sig = Crypt::encryptString(json_encode($payload)); + + $termii = new Termii($this->getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode([ + // + ]) + ))); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->validate($payload['pin'])); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertTrue($token->pin() === $payload['pin']); + } + + public function testVerifyValidateWithSignature() + { + $payload = [ + 'tag' => 'forgot_password', + 'pin_id' => Str::uuid()->toString(), + 'expires_at' => now()->addMinutes(20), + 'generated_at' => now(), + 'phonenumber' => '2347041945964', + 'in_app' => false, + ]; + + $sig = Crypt::encryptString(json_encode($payload)); + + $termii = new Termii($this->getClientWithMockedResponse(new Response( + 200, + ['Content-Type' => 'application/json'], + json_encode([ + 'pinId' => 'c8dcd048-5e7f-4347-8c89-4470c3af0b', + 'verified' => true, + 'msisdn' => '2347041945964' + ]) + ))); + + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertTrue($token->isLoaded()); + $this->assertTrue($token->isValid()); + $this->assertTrue($token->validate('123456')); + $this->assertTrue($token->id() === $payload['pin_id']); + $this->assertEmpty($token->pin()); + } +} diff --git a/tests/TestBench.php b/tests/TestBench.php new file mode 100644 index 0000000..066b80e --- /dev/null +++ b/tests/TestBench.php @@ -0,0 +1,38 @@ +set('termii', $config); + $app['request']->setLaravelSession($app['session.store']); + } + + /** + * Get package providers. + * + * @param \Illuminate\Foundation\Application $app + * + * @return array + */ + protected function getPackageProviders($app) + { + return [ + TermiiServiceProvider::class, + ]; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 504cad4..9555498 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,4 +8,5 @@ class TestCase extends BaseTestCase { use MockeryPHPUnitIntegration; + use MockingTraits; } From 35ab2c740b6ab283185df7db9f23b4800efd09d9 Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 22:31:53 +0100 Subject: [PATCH 4/9] feat: add HttpManager for laravel http client --- src/HttpManager.php | 80 +++++++++++++++++++++++++++++++++++ src/TermiiServiceProvider.php | 8 +++- 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/HttpManager.php diff --git a/src/HttpManager.php b/src/HttpManager.php new file mode 100644 index 0000000..3fe3843 --- /dev/null +++ b/src/HttpManager.php @@ -0,0 +1,80 @@ +termii = $termii; + } + + /** + * Handle requests + * + * @since 1.0 + * + * @param string $method + * @param string $route + * @param array $data + * @return \Psr\Http\Message\ResponseInterface + */ + public function request(string $method, string $route, array $data): ResponseInterface + { + if ($method === 'GET') { + $body = [ + 'query' => $data['query'] + ]; + } else { + $body = [ + 'body' => $data['body'] + ]; + } + + $headers = $data['headers']; + + return $this->http()->withHeaders($headers)->send($method, $route, $body)->toPsrResponse(); + } + + /** + * Handle requests + * + * @since 1.0 + * + * @return \Illuminate\Http\Client\PendingRequest + */ + public function http(): PendingRequest + { + return Http::asJson(); + } +} diff --git a/src/TermiiServiceProvider.php b/src/TermiiServiceProvider.php index b6ce64f..3665b21 100644 --- a/src/TermiiServiceProvider.php +++ b/src/TermiiServiceProvider.php @@ -19,8 +19,12 @@ class TermiiServiceProvider extends ServiceProvider */ public function register() { - $this->app->bind(TermiiClient::class, function () { - return new TermiiClient(Config::get('termii.key'), $this->getOptions()); + $this->app->singleton(HttpManager::class, function () { + return new HttpManager(); + }); + + $this->app->singleton(TermiiClient::class, function (Application $app) { + return new TermiiClient(Config::get('termii.key'), $this->getOptions(), $app->make(HttpManager::class)); }); $this->app->singleton(Termii::class, function (Application $app) { From 5e62d3ecf6d696a87aa9f4a7b250d92cdc356d7a Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 22:34:10 +0100 Subject: [PATCH 5/9] fix: session not set error on stateless requests --- src/Entities/Token.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Entities/Token.php b/src/Entities/Token.php index 78fdbc6..3328dea 100644 --- a/src/Entities/Token.php +++ b/src/Entities/Token.php @@ -96,17 +96,21 @@ public function __construct(Termii $termii, string $tag, string $signature = nul if ($signature) { $this->loadFromSignature($signature); - } elseif (($session = $this->getRequest()->session())) { - $this->loadFromSession($session, $tag); + } elseif ($this->session()) { + $this->loadFromSession($tag); } } /** * Get the current HTTP request */ - public function getRequest() + public function session() { - return App::make('request'); + if (!App::make('request')->hasSession()) { + return false; + } + + return App::make('request')->session(); } /** @@ -135,9 +139,9 @@ protected function loadFromSignature(string $signature) * @param string $tag * @return void */ - protected function loadFromSession($session, string $tag) + protected function loadFromSession(string $tag) { - $this->payload = json_decode($session->get($tag), true) ?? []; + $this->payload = json_decode($this->session()->get($tag), true) ?? []; $this->updateProperties(); $this->loaded = ($this->payload) ? true : false; } @@ -159,7 +163,7 @@ public function flush() $this->signature = null; $this->payload = []; - if (($session = $this->getRequest()->session())) { + if (($session = $this->session())) { $session->forget($this->tag); } @@ -341,7 +345,7 @@ public function start(array $options = []) $this->updateProperties(); $this->loaded = true; - if (($session = $this->getRequest()->session())) { + if (($session = $this->session())) { $session->put($this->tag, json_encode($this->payload)); } From d0e117ee65cb81bc122356729b1a3d1e2284576d Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 22:38:06 +0100 Subject: [PATCH 6/9] chore: style fix, rename method verify to otp and validate to verify --- src/Entities/Token.php | 2 +- src/Termii.php | 8 +++----- tests/TermiiTest.php | 30 +++++++++++++++--------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Entities/Token.php b/src/Entities/Token.php index 3328dea..ebb79bb 100644 --- a/src/Entities/Token.php +++ b/src/Entities/Token.php @@ -371,7 +371,7 @@ public function isValid() * * @param string */ - public function validate(string $pin) + public function verify(string $pin) { if (!is_int($pin) && !is_string($pin)) { return false; diff --git a/src/Termii.php b/src/Termii.php index f4bf67f..6cc4929 100644 --- a/src/Termii.php +++ b/src/Termii.php @@ -6,7 +6,6 @@ class Termii { - /** * The custom Termii client instance. * @@ -19,7 +18,7 @@ class Termii * * @param \ManeOlawale\Termii\Client $client */ - public function __construct( Client $client ) + public function __construct(Client $client) { $this->client = $client; } @@ -41,11 +40,11 @@ public function client() /** * Make new teken - * + * * @param string $key * @param string $signature */ - public function verify(string $key, string $signature = null) + public function otp(string $key, string $signature = null) { return new Entities\Token($this, $key, $signature); } @@ -67,5 +66,4 @@ public function send(string $to, string $message, string $from = null, string $c { return $this->client->sms->send($to, $message, $from, $channel); } - } diff --git a/tests/TermiiTest.php b/tests/TermiiTest.php index 11df968..bf62945 100644 --- a/tests/TermiiTest.php +++ b/tests/TermiiTest.php @@ -61,7 +61,7 @@ public function testVerify() { $termii = new Termii(new Client('{Your api key goes here}')); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify('forgot_password')); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp('forgot_password')); $this->assertNotTrue($token->isLoaded()); $this->assertEmpty($token->id()); } @@ -80,7 +80,7 @@ public function testVerifyWithSession() Session::put($payload['tag'], json_encode($payload)); $termii = new Termii(new Client('{Your api key goes here}')); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'])); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); $this->assertTrue($token->id() === $payload['pin_id']); @@ -101,7 +101,7 @@ public function testVerifyIsValidWithSession() Session::put($payload['tag'], json_encode($payload)); $termii = new Termii(new Client('{Your api key goes here}')); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'])); $this->assertTrue($token->isLoaded()); $this->assertNotTrue($token->isValid()); $this->assertTrue($token->id() === $payload['pin_id']); @@ -130,10 +130,10 @@ public function testVerifyValidateInappWithSession() ]) ))); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'])); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); - $this->assertTrue($token->validate($payload['pin'])); + $this->assertTrue($token->verify($payload['pin'])); $this->assertTrue($token->id() === $payload['pin_id']); $this->assertTrue($token->pin() === $payload['pin']); } @@ -161,10 +161,10 @@ public function testVerifyValidateWithSession() ]) ))); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'])); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'])); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); - $this->assertTrue($token->validate('123456')); + $this->assertTrue($token->verify('123456')); $this->assertTrue($token->id() === $payload['pin_id']); $this->assertEmpty($token->pin()); } @@ -181,7 +181,7 @@ public function testVerifyStartWithSession() ]) ))); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify('forgot_password')); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp('forgot_password')); $token->to('2347041945964')->text('{pin} is your account activation code')->start(); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); @@ -205,7 +205,7 @@ public function testVerifyStartInappWithSession() ]) ))); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify('forgot_password')); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp('forgot_password')); $token->to('2347041945964')->text('{pin} is your account activation code')->inApp()->start(); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); @@ -232,7 +232,7 @@ public function testVerifyWithSignature() $sig = Crypt::encryptString(json_encode($payload)); $termii = new Termii(new Client('{Your api key goes here}')); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'], $sig)); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); $this->assertTrue($token->id() === $payload['pin_id']); @@ -253,7 +253,7 @@ public function testVerifyIsValidWithSignature() $sig = Crypt::encryptString(json_encode($payload)); $termii = new Termii(new Client('{Your api key goes here}')); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'], $sig)); $this->assertTrue($token->isLoaded()); $this->assertNotTrue($token->isValid()); $this->assertTrue($token->id() === $payload['pin_id']); @@ -282,10 +282,10 @@ public function testVerifyValidateInappWithSignature() ]) ))); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'], $sig)); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); - $this->assertTrue($token->validate($payload['pin'])); + $this->assertTrue($token->verify($payload['pin'])); $this->assertTrue($token->id() === $payload['pin_id']); $this->assertTrue($token->pin() === $payload['pin']); } @@ -313,10 +313,10 @@ public function testVerifyValidateWithSignature() ]) ))); - $this->assertInstanceOf(EntitiesToken::class, $token = $termii->verify($payload['tag'], $sig)); + $this->assertInstanceOf(EntitiesToken::class, $token = $termii->otp($payload['tag'], $sig)); $this->assertTrue($token->isLoaded()); $this->assertTrue($token->isValid()); - $this->assertTrue($token->validate('123456')); + $this->assertTrue($token->verify('123456')); $this->assertTrue($token->id() === $payload['pin_id']); $this->assertEmpty($token->pin()); } From c9534ea86513050bc3ef08ea7951fd82f1139d72 Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 22:38:53 +0100 Subject: [PATCH 7/9] feat: add workflow --- .github/workflows/tests.yml | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..978ec6a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,45 @@ +name: Laravel Termii Tests + +on: + pull_request: + push: + branches: + - master + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + matrix: + php: + - "7.3" + - "7.4" + - "8.0" + dependency-version: + # - prefer-lowest + - prefer-stable + + name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} tests + steps: + # basically git clone + - uses: actions/checkout@v2 + + - name: Setup Git + run: | + git --version + git config --global user.email "test@github.com" + git config --global user.name "GitHub Action" + git --version + - name: Setup PHP + # use PHP of specific version + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php }} + coverage: none # disable xdebug, pcov + tools: composer + + - name: Install Composer Dependencies + run: | + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + - name: Run PHPUnit Tests + run: composer test \ No newline at end of file From 47d16a05bfbb4c22e122dd5e7141232d679c31d7 Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 22:44:33 +0100 Subject: [PATCH 8/9] chore: add CONTRIBUTING.md --- CONTRIBUTING.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b8c3b59 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/Mane-Olawale/laravel-termii). + + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **Create feature branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + + +## Running Tests + +``` bash +$ composer test +``` + + +**Happy coding**! From 0ad2b53fff09711092228d3f89e7d0d6226c6717 Mon Sep 17 00:00:00 2001 From: Ilesanmi Olawale Adedotun Date: Mon, 14 Feb 2022 22:48:06 +0100 Subject: [PATCH 9/9] chore: add issue templates --- .github/ISSUE_TEMPLATE/bug_report.md | 26 +++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 14 ++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..e1c5f27 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,26 @@ +--- +name: Bug report +about: Create a report to help us fix the problem +title: '[BUG]' +labels: '' +assignees: Mane-Olawale + +--- + +- PHP Version: #.#.# +- Laravel Version: #.#.# +- Operating system & Version: #.#.# + +### Description: +A clear and concise description of what the bug is. + +### Error/Exception Message: +If applicable, paste the full excption to help explain your problem. + +### Screenshots: +If applicable, add screenshots to help explain your problem. + +### Steps To Reproduce: +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..319e0ef --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,14 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: "[FEATURE]" +labels: '' +assignees: Mane-Olawale + +--- + +### Problem description +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +### Propose a solution +A clear and concise description of what you want to happen.