From 0dd56b9d7c1ffb509c4062c2e1618c1bf55fcd03 Mon Sep 17 00:00:00 2001 From: Richard Abear Date: Sat, 18 Jul 2020 02:29:40 +0800 Subject: [PATCH] added basic twilio model test --- ...0_07_17_182714_create_twilio_accounts.php} | 5 ++-- phpunit.xml | 4 ++++ src/Traits/TwilioSubaccount.php | 3 --- src/TwilioServiceProvider.php | 11 ++------- tests/TestCase.php | 16 ++++++++++--- tests/TestModel.php | 10 ++++++++ tests/Unit/TwilioAccountTest.php | 23 +++++++++++++++++++ tests/Unit/TwilioTest.php | 5 +++- 8 files changed, 59 insertions(+), 18 deletions(-) rename migrations/{2020_17_07_000000_create_twilio_accounts_table.php => 2020_07_17_182714_create_twilio_accounts.php} (82%) create mode 100644 tests/TestModel.php create mode 100644 tests/Unit/TwilioAccountTest.php diff --git a/migrations/2020_17_07_000000_create_twilio_accounts_table.php b/migrations/2020_07_17_182714_create_twilio_accounts.php similarity index 82% rename from migrations/2020_17_07_000000_create_twilio_accounts_table.php rename to migrations/2020_07_17_182714_create_twilio_accounts.php index a8c3314..620fcc4 100644 --- a/migrations/2020_17_07_000000_create_twilio_accounts_table.php +++ b/migrations/2020_07_17_182714_create_twilio_accounts.php @@ -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. @@ -18,6 +18,7 @@ public function up() $table->string('friendly_name'); $table->string('sid')->nullable(); $table->string('token')->nullable(); + $table->timestamps(); }); } @@ -28,6 +29,6 @@ public function up() */ public function down() { - Schema::dropIfExists('users'); + Schema::dropIfExists('twilio_accounts'); } } diff --git a/phpunit.xml b/phpunit.xml index ab08cf4..bc2cee3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -22,5 +22,9 @@ + + + + \ No newline at end of file diff --git a/src/Traits/TwilioSubaccount.php b/src/Traits/TwilioSubaccount.php index 597a284..584f07e 100644 --- a/src/Traits/TwilioSubaccount.php +++ b/src/Traits/TwilioSubaccount.php @@ -32,8 +32,5 @@ protected function createTwilioSubAccount() * @var TwilioAdminClient $adminClient */ $adminClient = app(TwilioAdminClient::class); - $adminClient->api->v2010->accounts->create([ - 'friendlyName' => - ]); } } diff --git a/src/TwilioServiceProvider.php b/src/TwilioServiceProvider.php index 1d040f0..f5b3c3c 100644 --- a/src/TwilioServiceProvider.php +++ b/src/TwilioServiceProvider.php @@ -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(); - }); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8a4a035..aebdcd1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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]; } /** @@ -21,9 +22,10 @@ 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' => '', @@ -31,6 +33,14 @@ protected function getEnvironmentSetUp($app) $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(); + } } diff --git a/tests/TestModel.php b/tests/TestModel.php new file mode 100644 index 0000000..39d4df3 --- /dev/null +++ b/tests/TestModel.php @@ -0,0 +1,10 @@ + 'test', + ]); + $twilioAccount->save(); + $this->assertEquals('test', $twilioAccount->friendly_name); + } +} diff --git a/tests/Unit/TwilioTest.php b/tests/Unit/TwilioTest.php index 83fbc7e..2544f60 100644 --- a/tests/Unit/TwilioTest.php +++ b/tests/Unit/TwilioTest.php @@ -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']); } }