From a7988247155f1c5bf6af447798c8adfd387df797 Mon Sep 17 00:00:00 2001 From: Victor Lap Date: Mon, 3 Jul 2017 09:41:38 +0200 Subject: [PATCH] Added some basic tests. --- .travis.yml | 1 + migrations/create_approvals_table.php.stub | 2 +- tests/BaseTest.php | 43 +++++++++++++------ tests/TestCase.php | 5 ++- tests/TestServiceProvider.php | 19 -------- ...17_06_28_000000_create_approvals_table.php | 38 ++++++++++++++++ 6 files changed, 74 insertions(+), 34 deletions(-) delete mode 100644 tests/TestServiceProvider.php create mode 100644 tests/database/migrations/2017_06_28_000000_create_approvals_table.php diff --git a/.travis.yml b/.travis.yml index bde4ec6..ca34bff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,7 @@ cache: env: matrix: - COMPOSER_FLAGS="--prefer-lowest" + - COMPOSER_FLAGS="" before_script: - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist diff --git a/migrations/create_approvals_table.php.stub b/migrations/create_approvals_table.php.stub index a1c3a7e..1558cbe 100644 --- a/migrations/create_approvals_table.php.stub +++ b/migrations/create_approvals_table.php.stub @@ -33,6 +33,6 @@ class CreateApprovalsTable extends Migration */ public function down() { - Schema::drop('approvals'); + Schema::dropIfExists('approvals'); } } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 904adee..88740f3 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -2,39 +2,58 @@ namespace Victorlap\Approvable\Tests; -use Illuminate\Support\Facades\DB; +use Victorlap\Approvable\Tests\Models\User; use Victorlap\Approvable\Tests\Models\UserCanApprove; use Victorlap\Approvable\Tests\Models\UserCannotApprove; + class BaseTests extends TestCase { + private function returnUserInstance($model = User::class) + { + $instance = new $model([ + 'name' => 'John Doe', + 'email' => 'john@doe.com', + ]); + $instance::boot(); + + return $instance; + } + public function testApproverCanCreate() { - $user = UserCanApprove::create(['name' => 'John Doe', 'email' => 'john@doe.com']); + $user = $this->returnUserInstance(UserCanApprove::class); + + $user->save(); + $this->assertTrue($user->exists); } public function testRegularCanCreate() { - $user = UserCannotApprove::create(['name' => 'Jane Doe', 'email' => 'jane@doe.com']); + $user = $this->returnUserInstance(UserCannotApprove::class); + + $user->save(); + $this->assertTrue($user->exists); } public function testApproverCanEdit() { - $user = UserCanApprove::find(['email' => 'john@doe.com']); + $user = $this->returnUserInstance(UserCanApprove::class); + $user->save(); - $user->update(['name' => 'Doe John'])->fresh(); + $user->name = 'Doe John'; + $user->save(); - $this->assertEquals('Doe Jonh', $user->name); + $this->assertEquals('Doe John', $user->fresh()->name); } public function testRegularCannotEdit() { - $user = UserCannotApprove::find(['email' => 'jane@doe.com']); - - $this->assertEquals(0, DB::table('approvals')->count()); + $user = $this->returnUserInstance(UserCannotApprove::class); + $user->save(); - $user->update(['name' => 'Doe Jane'])->fresh(); + $user->name = 'Doe John'; + $user->save(); - $this->assertEquals('Jane Doe', $user->name); - $this->assertEquals(1, DB::table('approvals')->count()); + $this->assertEquals('John Doe', $user->fresh()->name); } } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 41de1ff..c6968d7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,11 +2,13 @@ namespace Victorlap\Approvable\Tests; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Orchestra\Testbench\TestCase as Orchestra; use Victorlap\Approvable\ApprovableServiceProvider; class TestCase extends Orchestra { + use DatabaseTransactions; /** * Setup the test environment. @@ -17,7 +19,7 @@ public function setUp() { parent::setUp(); - $this->artisan('migrate'); + $this->loadMigrationsFrom(realpath(__DIR__.'/database/migrations')); } /** @@ -47,7 +49,6 @@ protected function getEnvironmentSetUp($app) protected function getPackageProviders($app) { return [ - TestServiceProvider::class, ApprovableServiceProvider::class, ]; } diff --git a/tests/TestServiceProvider.php b/tests/TestServiceProvider.php deleted file mode 100644 index 249e7c9..0000000 --- a/tests/TestServiceProvider.php +++ /dev/null @@ -1,19 +0,0 @@ -loadMigrationsFrom(realpath(__DIR__.'/database/migrations/migrations')); - $this->loadMigrationsFrom(realpath(__DIR__.'/../migrations')); - } -} \ No newline at end of file diff --git a/tests/database/migrations/2017_06_28_000000_create_approvals_table.php b/tests/database/migrations/2017_06_28_000000_create_approvals_table.php new file mode 100644 index 0000000..1558cbe --- /dev/null +++ b/tests/database/migrations/2017_06_28_000000_create_approvals_table.php @@ -0,0 +1,38 @@ +increments('id'); + $table->string('approvable_type'); + $table->integer('approvable_id'); + $table->integer('user_id')->nullable(); + $table->string('key'); + $table->text('value')->nullable(); + $table->boolean('approved')->default(false); + $table->timestamps(); + $table->index(array('approvable_id', 'approvable_type')); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('approvals'); + } +}