Skip to content

Commit

Permalink
added basic twilio model test
Browse files Browse the repository at this point in the history
  • Loading branch information
richardabear committed Jul 17, 2020
1 parent 96b2ea3 commit 0dd56b9
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTwilioAccountsTable extends Migration
class CreateTwilioAccounts extends Migration
{
/**
* Run the migrations.
Expand All @@ -18,6 +18,7 @@ public function up()
$table->string('friendly_name');
$table->string('sid')->nullable();
$table->string('token')->nullable();
$table->timestamps();
});
}

Expand All @@ -28,6 +29,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('users');
Schema::dropIfExists('twilio_accounts');
}
}
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="TWILIO_LIVE_TEST" value="false"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
3 changes: 0 additions & 3 deletions src/Traits/TwilioSubaccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,5 @@ protected function createTwilioSubAccount()
* @var TwilioAdminClient $adminClient
*/
$adminClient = app(TwilioAdminClient::class);
$adminClient->api->v2010->accounts->create([
'friendlyName' =>
]);
}
}
11 changes: 2 additions & 9 deletions src/TwilioServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@

class TwilioServiceProvider extends ServiceProvider
{
public function register()
{
}

public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/../migrations');

$this->publishes([
__DIR__.'../config/twilio.php' => config_path('twilio.php')
]);

$this->app->singleton(TwilioAdminClient::class, function () {
$twilioAdminAdapter = new Twilio(config('twilio.sid'), config('twilio.token'));
return $twilioAdminAdapter->getClient();
});
}
}
16 changes: 13 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

use Orchestra\Testbench\TestCase as TestbenchTestCase;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use RichardAbear\Twilio\TwilioServiceProvider;

class TestCase extends TestbenchTestCase
{
protected function getPackageProviders($app)
{
return ['RichardAbear\Twilio\TwilioServiceProvider'];
return [TwilioServiceProvider::class];
}

/**
Expand All @@ -21,16 +22,25 @@ protected function getEnvironmentSetUp($app)
{
$app->useEnvironmentPath(__DIR__.'/..');
$app->bootstrapWith([LoadEnvironmentVariables::class]);

// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

$app['config']->set('twilio.sid', env('TWILIO_ADMIN_SID'));
$app['config']->set('twilio.token', env('TWILIO_ADMIN_TOKEN'));

parent::getEnvironmentSetUp($app);
}

protected function setUp(): void
{
parent::setUp();

$this->artisan('migrate', ['--database' => 'testing'])->run();
}
}
10 changes: 10 additions & 0 deletions tests/TestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace RichardAbear\Twilio;

use Illuminate\Database\Eloquent\Model;
use RichardAbear\Twilio\Traits\TwilioSubaccount;

class TestModel extends Model {
use TwilioSubaccount;

}
23 changes: 23 additions & 0 deletions tests/Unit/TwilioAccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace RichardAbear\Twilio\Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use RichardAbear\Twilio\Adapters\Twilio;
use RichardAbear\Twilio\Models\TwilioAccount;
use RichardAbear\Twilio\Tests\TestCase;
use Twilio\Rest\Client;

class TwilioAccountTest extends TestCase
{
use RefreshDatabase;

public function testCanCreateTwilioAccount()
{
$twilioAccount = new TwilioAccount([
'friendly_name' => 'test',
]);
$twilioAccount->save();
$this->assertEquals('test', $twilioAccount->friendly_name);
}
}
5 changes: 4 additions & 1 deletion tests/Unit/TwilioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ public function testCanCreateTwilioAdminClient()

public function testCanCreateSubaccountAndClose()
{
if (env('TWILIO_LIVE_TEST')) {
return true;
}
$twilio_administrator = new Twilio(config('twilio.sid'), config('twilio.token'));
$client = $twilio_administrator->getClient();
$friendly_name = "Test Account";
$sub_account = $client->api->v2010->accounts->create(['friendlyName' => $friendly_name]);
$this->assertNotNull($sub_account);

$client->api->v2010->accounts($sub_account->friendlyName)
$client->api->v2010->accounts($sub_account->sid)->update(['status' => 'closed']);
}
}

0 comments on commit 0dd56b9

Please sign in to comment.