Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
Added some basic tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlap committed Jul 3, 2017
1 parent d19cff7 commit a798824
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 34 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion migrations/create_approvals_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class CreateApprovalsTable extends Migration
*/
public function down()
{
Schema::drop('approvals');
Schema::dropIfExists('approvals');
}
}
43 changes: 31 additions & 12 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 3 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,7 +19,7 @@ public function setUp()
{
parent::setUp();

$this->artisan('migrate');
$this->loadMigrationsFrom(realpath(__DIR__.'/database/migrations'));
}

/**
Expand Down Expand Up @@ -47,7 +49,6 @@ protected function getEnvironmentSetUp($app)
protected function getPackageProviders($app)
{
return [
TestServiceProvider::class,
ApprovableServiceProvider::class,
];
}
Expand Down
19 changes: 0 additions & 19 deletions tests/TestServiceProvider.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateApprovalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('approvals', function (Blueprint $table) {
$table->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');
}
}

0 comments on commit a798824

Please sign in to comment.