Skip to content

Commit

Permalink
Merge pull request #2 from 64robots/update/fix-typo
Browse files Browse the repository at this point in the history
Fix typo and add test coverage
  • Loading branch information
robmpreston authored Mar 13, 2019
2 parents 9f0d19b + 80c82f9 commit 273c110
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 19 deletions.
29 changes: 21 additions & 8 deletions src/Thinq.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
namespace R64\LaravelThinq;

use Exception;
use Illuminate\Support\Facades\App;

class Thinq
{
protected $thinqMessage;
protected $account_id;
protected $api_key;
public $config;
public $thinqMessage;

public function __construct()
public function __construct(ThinqConfig $config)
{
$this->account_id = config('thinq.account_id');
$this->api_key = config('thinq.api_key');
$this->config = $config;
}

public function withMessage(ThinqMessage $message)
Expand All @@ -26,9 +25,18 @@ public function withMessage(ThinqMessage $message)

public function sentSms()
{
// Since thinq is restricted to IP, disable or enable api call when testing
if ($this->shouldDisableApiCall()) {
return;
}

$apiKey = $this->config->getApiKey();
$accountId = $this->config->getAccountId();

$data = $this->thinqMessage->getMessage();
$authorization = base64_encode($this->api_key);
$url = "https://api.thinq.com/account/{$this->account_id}/product/origination/sms/send";

$authorization = base64_encode($apiKey);
$url = "https://api.thinq.com/account/{$accountId}/product/origination/sms/send";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
Expand Down Expand Up @@ -58,4 +66,9 @@ public function sentSilentSms()

}
}

private function shouldDisableApiCall()
{
return App::environment() === 'local' && $this->config->shouldDisableApiCalls();
}
}
11 changes: 0 additions & 11 deletions src/ThinqChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Facades\Log;

class ThinqChannel
{
Expand All @@ -19,11 +18,6 @@ public function __construct(Thinq $thinq, Dispatcher $events)

public function send($notifiable, Notification $notification)
{
// Since thinq is restricted to IP, disable or enable api call when testing
if ($this->shouldDisableApiCall()) {
return;
}

$message = $notification->toThinq($notifiable);

if(property_exists($notification, 'silent') && $notification->silent) {
Expand All @@ -32,9 +26,4 @@ public function send($notifiable, Notification $notification)
$this->thinq->withMessage($message)->sentSms();
}
}

private function shoudDisableApiCall()
{
return app()->environment('local') && config('thinq.disable_api_calls');
}
}
21 changes: 21 additions & 0 deletions src/ThinqConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace R64\LaravelThinq;

class ThinqConfig
{
public function getApiKey()
{
return config('thinq.api_key');
}

public function getAccountId()
{
return config('thinq.account_id');
}

public function shouldDisableApiCalls()
{
return config('thinq.disable_api_calls');
}
}
54 changes: 54 additions & 0 deletions tests/ThinqChannelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace R64\LaravelThinq\Tests;

use Mockery;
use R64\LaravelThinq\Thinq;
use R64\LaravelThinq\ThinqChannel;
use R64\LaravelThinq\ThinqMessage;
use Illuminate\Support\Facades\App;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Events\Dispatcher;
use Mockery\Adapter\Phpunit\MockeryTestCase;

class ThinqChannelTest extends MockeryTestCase
{
public $thinq;
public $channel;

public $dispatcher;

public function setUp()
{
parent::setUp();

$this->thinq = Mockery::mock(Thinq::class);
$this->dispatcher = Mockery::mock(Dispatcher::class);

$this->channel = new ThinqChannel($this->thinq, $this->dispatcher);
}

/**
* @test
*/
public function can_send_thinq_message()
{
$notifiable = new Class {};
$notification = Mockery::mock(Notification::class);

$message = new ThinqMessage('Message', '+1234567890', '+2346788778');
$notification->shouldReceive('toThinq')->andReturn($message);

$this->thinq->shouldReceive('withMessage')
->with($message)
->andReturn($this->thinq);

$this->thinq->shouldReceive('sentSms')
->once();

App::shouldReceive('environment')
->andReturn('production');

$this->channel->send($notifiable, $notification);
}
}
29 changes: 29 additions & 0 deletions tests/ThinqMessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace R64\LaravelThinq\Tests;

use PHPUnit\Framework\TestCase;
use R64\LaravelThinq\ThinqMessage;

class ThinqMessageTest extends TestCase
{
public $thinqMessage;

public function setUp()
{
parent::setUp();
$this->thinqMessage = new ThinqMessage('Hello, this is thinq message', '+2334988499', '+1234567890');
}

/**
* @test
*/
public function can_get_thinq_message()
{
$expected = $this->thinqMessage->getMessage();

$this->assertEquals('Hello, this is thinq message', $expected['message']);
$this->assertEquals('+2334988499', $expected['from_did']);
$this->assertEquals('+1234567890', $expected['to_did']);
}
}
109 changes: 109 additions & 0 deletions tests/ThinqTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace R64\LaravelThinq\Tests;

use Mockery;
use R64\LaravelThinq\Thinq;
use R64\LaravelThinq\ThinqConfig;
use R64\LaravelThinq\ThinqMessage;
use Illuminate\Support\Facades\App;
use Mockery\Adapter\Phpunit\MockeryTestCase;

class ThinqTest extends MockeryTestCase
{
public function setUp()
{
parent::setUp();

$this->config = Mockery::mock(ThinqConfig::class);
$this->thinqMessage = Mockery::mock(ThinqMessage::class);

$this->thinq = new Thinq($this->config);
}

/**
* @test
*/
public function can_send_a_message_to_thinq()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Unauthorized');

$this->config->shouldReceive([
'getApiKey' => '12345xxxx',
'getAccountId' => 'sdxxx12345',
]);

$this->thinqMessage->shouldReceive('getMessage')
->andReturn([
'from_did' => '+1234567890',
'to_did' => '+2346788778',
'message' => 'Message text',
]);

$this->thinq->thinqMessage = $this->thinqMessage;

App::shouldReceive('environment')
->andReturn('production');

$this->thinq->sentSms();
}

/**
* @test
*/
public function can_send_silent_sms_message_to_thinq()
{
$this->config->shouldReceive([
'getApiKey' => '12345xxxx',
'getAccountId' => 'sdxxx12345',
]);

$this->thinqMessage->shouldReceive('getMessage')
->andReturn([
'from_did' => '+1234567890',
'to_did' => '+2346788778',
'message' => 'Message text',
]);

$this->thinq->thinqMessage = $this->thinqMessage;

App::shouldReceive('environment')
->andReturn('production');

$this->thinq->sentSilentSms();
}

/**
* @test
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function will_not_make_api_call_when_disabled()
{
$this->config->shouldReceive([
'getApiKey' => '12345xxxx',
'getAccountId' => 'sdxxx12345',
'shouldDisableApiCalls' => true,
]);

$this->config->shouldReceive('shouldDisableApiCalls')
->andReturn(true);

$this->thinqMessage->shouldReceive('getMessage')
->andReturn([
'from_did' => '+1234567890',
'to_did' => '+2346788778',
'message' => 'Message text',
]);

$this->thinq->thinqMessage = $this->thinqMessage;

App::shouldReceive('environment')
->andReturn('local');

$expected = $this->thinq->sentSms();
$this->assertNull($expected);
}
}

0 comments on commit 273c110

Please sign in to comment.