Skip to content

Commit

Permalink
Refactor tests to avoid dynamic properties
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Feb 12, 2023
1 parent 13bb152 commit 518be6e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 83 deletions.
3 changes: 1 addition & 2 deletions src/Providers/TwilioProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Illuminate\Support\ServiceProvider;
use Twilio\Http\Client as TwilioHttpClient;
use Twilio\Http\CurlClient;
use Twilio\Http\GuzzleClient;

final class TwilioProvider extends ServiceProvider implements DeferrableProvider
{
Expand Down Expand Up @@ -67,7 +66,7 @@ private function registerHttpClient(): void
$this->app->bind(
TwilioHttpClient::class,
static function (Application $app): TwilioHttpClient {
// If Guzzle is installed, then we will either use Laravel's native client
// If Guzzle is installed, then we will use Laravel's native client
if (class_exists(Guzzle::class)) {
return new LaravelHttpClient($app->make(Factory::class));
}
Expand Down
176 changes: 95 additions & 81 deletions tests/TwilioClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,136 @@

namespace BabDev\Twilio\Tests;

use BabDev\Twilio\TwilioClient;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use BabDev\Twilio\Facades\TwilioClient;
use BabDev\Twilio\Providers\TwilioProvider;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
use Orchestra\Testbench\TestCase;
use Twilio\Rest\Api\V2010\Account\CallInstance;
use Twilio\Rest\Api\V2010\Account\CallList;
use Twilio\Rest\Api\V2010\Account\MessageInstance;
use Twilio\Rest\Api\V2010\Account\MessageList;
use Twilio\Rest\Client;

final class TwilioClientTest extends TestCase
{
public function testTheSdkInstanceCanBeRetrieved(): void
protected function getEnvironmentSetUp($app): void
{
$defaultFrom = '+19418675309';
// Setup connections configuration
$app['config']->set(
'twilio.connections.twilio',
[
'sid' => 'account-sid',
'token' => 'api_token',
'from' => '+15558675309',
]
);
}

/** @var MockObject&Client $twilio */
$twilio = $this->createMock(Client::class);
/**
* @return class-string<ServiceProvider>
*/
protected function getPackageProviders($app): array
{
return [
TwilioProvider::class,
];
}

$this->assertSame($twilio, (new TwilioClient($twilio, $defaultFrom))->twilio());
public function testTheSdkInstanceCanBeRetrieved(): void
{
$this->assertInstanceOf(Client::class, TwilioClient::twilio());
}

public function testACallCanBeCreated(): void
{
$to = '+15558675309';
$defaultFrom = '+19418675309';
$params = [
'url' => 'https://www.babdev.com',
];

/** @var MockObject&CallList $calls */
$calls = $this->createMock(CallList::class);
$calls->expects($this->once())
->method('create')
->with($to, $defaultFrom, $params)
->willReturn($this->createMock(CallInstance::class));
$to = '+15558675309';

/** @var MockObject&Client $twilio */
$twilio = $this->createMock(Client::class);
$twilio->calls = $calls;
Http::fake([
'https://api.twilio.com/2010-04-01/Accounts/account-sid/Calls.json' => Http::response(
$this->getMessageSentResponseContent(config('twilio.connections.twilio.from'), $to),
201,
),
]);

$this->assertInstanceOf(CallInstance::class, (new TwilioClient($twilio, $defaultFrom))->call($to, $params));
$this->assertInstanceOf(CallInstance::class, TwilioClient::call($to));
}

public function testACallCanBeCreatedWithACustomFromNumber(): void
{
$to = '+15558675309';
$defaultFrom = '+19418675309';
$customFrom = '+16518675309';
$params = [
'url' => 'https://www.babdev.com',
];

/** @var MockObject&CallList $calls */
$calls = $this->createMock(CallList::class);
$calls->expects($this->once())
->method('create')
->with($to, $customFrom, $params)
->willReturn($this->createMock(CallInstance::class));
$to = '+15558675309';
$customFrom = '+16518675309';

/** @var MockObject&Client $twilio */
$twilio = $this->createMock(Client::class);
$twilio->calls = $calls;
Http::fake([
'https://api.twilio.com/2010-04-01/Accounts/account-sid/Calls.json' => Http::response(
$this->getMessageSentResponseContent($customFrom, $to),
201,
),
]);

$this->assertInstanceOf(CallInstance::class, (new TwilioClient($twilio, $defaultFrom))->call($to, array_merge($params, ['from' => $customFrom])));
$this->assertInstanceOf(CallInstance::class, TwilioClient::call($to));
}

public function testAMessageCanBeSent(): void
{
$to = '+15558675309';
$defaultFrom = '+19418675309';
$message = 'Test Message';

/** @var MockObject&MessageList $messages */
$messages = $this->createMock(MessageList::class);
$messages->expects($this->once())
->method('create')
->with(
$to,
[
'body' => $message,
'from' => $defaultFrom,
]
)
->willReturn($this->createMock(MessageInstance::class));

/** @var MockObject&Client $twilio */
$twilio = $this->createMock(Client::class);
$twilio->messages = $messages;

$this->assertInstanceOf(MessageInstance::class, (new TwilioClient($twilio, $defaultFrom))->message($to, $message));
Http::fake([
'https://api.twilio.com/2010-04-01/Accounts/account-sid/Messages.json' => Http::response(
$this->getMessageSentResponseContent(config('twilio.connections.twilio.from'), $to),
201,
),
]);

$this->assertInstanceOf(MessageInstance::class, TwilioClient::message($to, $message));
}

public function testAMessageCanBeSentWithACustomFromNumber(): void
{
$to = '+15558675309';
$defaultFrom = '+19418675309';
$customFrom = '+16518675309';
$message = 'Test Message';

/** @var MockObject&MessageList $messages */
$messages = $this->createMock(MessageList::class);
$messages->expects($this->once())
->method('create')
->with(
$to,
[
'body' => $message,
'from' => $customFrom,
]
)
->willReturn($this->createMock(MessageInstance::class));

/** @var MockObject&Client $twilio */
$twilio = $this->createMock(Client::class);
$twilio->messages = $messages;

$this->assertInstanceOf(MessageInstance::class, (new TwilioClient($twilio, $defaultFrom))->message($to, $message, ['from' => $customFrom]));
Http::fake([
'https://api.twilio.com/2010-04-01/Accounts/account-sid/Messages.json' => Http::response(
$this->getMessageSentResponseContent($customFrom, $to),
201,
),
]);

$this->assertInstanceOf(MessageInstance::class, TwilioClient::message($to, $message, ['from' => $customFrom]));
}

/**
* @return array<string, mixed>
*/
private function getMessageSentResponseContent(string $from, string $to): array
{
$date = Date::now()->toRfc822String();

return [
'body' => 'Test',
'num_segments' => '1',
'direction' => 'outbound-api',
'from' => $from,
'date_updated' => $date,
'price' => null,
'error_message' => null,
'uri' => '/2010-04-01/Accounts/account-sid/Messages/message-sid.json',
'account_sid' => 'account-sid',
'num_media' => '0',
'to' => $to,
'date_created' => $date,
'status' => 'queued',
'sid' => 'message-sid',
'date_sent' => null,
'messaging_service_sid' => null,
'error_code' => null,
'price_unit' => 'USD',
'api_version' => '2010-04-01',
'subresource_uris' => [
'media' => '/2010-04-01/Accounts/account-sid/Messages/message-sid/Media.json',
],
];
}
}

0 comments on commit 518be6e

Please sign in to comment.